You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2020/11/09 08:03:59 UTC

[qpid-proton] branch master updated: PROTON-2293: Stop raw connection wakes from crashing the application - Ensure that the raw connectino is not freed is there is a pending wake. - Ensure that a wake will not be delivered after the application closes the raw connection. - Ensure that wake events will be delivered before disconnected events if both are scheduled to happen.

This is an automated email from the ASF dual-hosted git repository.

astitcher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git


The following commit(s) were added to refs/heads/master by this push:
     new ce8a409  PROTON-2293: Stop raw connection wakes from crashing the application - Ensure that the raw connectino is not freed is there is a pending wake. - Ensure that a wake will not be delivered after the application closes   the raw connection. - Ensure that wake events will be delivered before disconnected events   if both are scheduled to happen.
ce8a409 is described below

commit ce8a40912d07560ee4008c97988b19fdde0c4d6f
Author: Andrew Stitcher <as...@apache.org>
AuthorDate: Thu Oct 15 02:04:09 2020 -0400

    PROTON-2293: Stop raw connection wakes from crashing the application
    - Ensure that the raw connectino is not freed is there is a pending wake.
    - Ensure that a wake will not be delivered after the application closes
      the raw connection.
    - Ensure that wake events will be delivered before disconnected events
      if both are scheduled to happen.
---
 c/src/proactor/epoll_raw_connection.c    | 44 +++++++++++++++++++++++---------
 c/src/proactor/raw_connection-internal.h |  1 +
 c/src/proactor/raw_connection.c          |  6 ++---
 c/tests/raw_connection_test.cpp          |  2 +-
 4 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/c/src/proactor/epoll_raw_connection.c b/c/src/proactor/epoll_raw_connection.c
index ab14b6a..abbd15e 100644
--- a/c/src/proactor/epoll_raw_connection.c
+++ b/c/src/proactor/epoll_raw_connection.c
@@ -268,19 +268,35 @@ const pn_netaddr_t *pn_raw_connection_remote_addr(pn_raw_connection_t *rc) {
 void pn_raw_connection_wake(pn_raw_connection_t *rc) {
   bool notify = false;
   praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
-  if (prc) {
-    lock(&prc->context.mutex);
-    if (!prc->context.closing) {
-      prc->waking = true;
-      notify = wake(&prc->context);
-    }
-    unlock(&prc->context.mutex);
+  lock(&prc->context.mutex);
+  if (!prc->context.closing) {
+    prc->waking = true;
+    notify = wake(&prc->context);
   }
+  unlock(&prc->context.mutex);
   if (notify) wake_notify(&prc->context);
 }
 
+void pn_raw_connection_close(pn_raw_connection_t *rc) {
+  praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
+  lock(&prc->context.mutex);
+  prc->context.closing = true;
+  unlock(&prc->context.mutex);
+  pni_raw_close(rc);
+}
+
 static pn_event_t *pni_raw_batch_next(pn_event_batch_t *batch) {
-  pn_raw_connection_t *raw = &containerof(batch, praw_connection_t, batch)->raw_connection;
+  praw_connection_t *rc = containerof(batch, praw_connection_t, batch);
+  pn_raw_connection_t *raw = &rc->raw_connection;
+
+  // Check wake status every event processed
+  bool waking = false;
+  lock(&rc->context.mutex);
+  waking = rc->waking;
+  rc->waking = false;
+  unlock(&rc->context.mutex);
+  if (waking) pni_raw_wake(raw);
+
   return pni_raw_event_next(raw);
 }
 
@@ -330,9 +346,11 @@ pn_event_batch_t *pni_raw_connection_process(pcontext_t *c, bool sched_wake) {
   bool wake = false;
   lock(&c->mutex);
   c->working = true;
-  if (sched_wake) wake_done(c);
-  wake = sched_wake || rc->waking;
-  rc->waking = false;
+  if (sched_wake) {
+    wake_done(c);
+    wake = rc->waking;
+    rc->waking = false;
+  }
   unlock(&c->mutex);
 
   if (wake) pni_raw_wake(&rc->raw_connection);
@@ -343,11 +361,13 @@ pn_event_batch_t *pni_raw_connection_process(pcontext_t *c, bool sched_wake) {
 
 void pni_raw_connection_done(praw_connection_t *rc) {
   bool self_notify = false;
+  bool wake_pending = false;
   lock(&rc->context.mutex);
   pn_proactor_t *p = rc->context.proactor;
   tslot_t *ts = rc->context.runner;
   rc->context.working = false;
   self_notify = rc->waking && wake(&rc->context);
+  wake_pending = rc->waking;
   unlock(&rc->context.mutex);
   if (self_notify) wake_notify(&rc->context);
 
@@ -359,7 +379,7 @@ void pni_raw_connection_done(praw_connection_t *rc) {
     rc->psocket.epoll_io.wanted = wanted;
     rearm_polling(&rc->psocket.epoll_io, p->epollfd);  // TODO: check for error
   } else {
-    bool finished_disconnect = raw->rclosed && raw->wclosed && !raw->disconnectpending;
+    bool finished_disconnect = raw->rclosed && raw->wclosed && !wake_pending && !raw->disconnectpending;
     if (finished_disconnect) {
       // If we're closed and we've sent the disconnect then close
       pni_raw_finalize(raw);
diff --git a/c/src/proactor/raw_connection-internal.h b/c/src/proactor/raw_connection-internal.h
index 113568c..2e183a0 100644
--- a/c/src/proactor/raw_connection-internal.h
+++ b/c/src/proactor/raw_connection-internal.h
@@ -94,6 +94,7 @@ bool pni_raw_validate(pn_raw_connection_t *conn);
 void pni_raw_connected(pn_raw_connection_t *conn);
 void pni_raw_connect_failed(pn_raw_connection_t *conn);
 void pni_raw_wake(pn_raw_connection_t *conn);
+void pni_raw_close(pn_raw_connection_t *conn);
 void pni_raw_read(pn_raw_connection_t *conn, int sock, long (*recv)(int, void*, size_t), void (*set_error)(pn_raw_connection_t *, const char *, int));
 void pni_raw_write(pn_raw_connection_t *conn, int sock, long (*send)(int, const void*, size_t), void (*set_error)(pn_raw_connection_t *, const char *, int));
 bool pni_raw_can_read(pn_raw_connection_t *conn);
diff --git a/c/src/proactor/raw_connection.c b/c/src/proactor/raw_connection.c
index 8c80925..c88f602 100644
--- a/c/src/proactor/raw_connection.c
+++ b/c/src/proactor/raw_connection.c
@@ -351,7 +351,7 @@ void pni_raw_read(pn_raw_connection_t *conn, int sock, long (*recv)(int, void*,
         // Detected an error
         default:
           set_error(conn, "recv error", errno);
-          pn_raw_connection_close(conn);
+          pni_raw_close(conn);
           return;
       }
     }
@@ -418,7 +418,7 @@ void pni_raw_write(pn_raw_connection_t *conn, int sock, long (*send)(int, const
         default:
           set_error(conn, "send error", errno);
           pni_raw_release_buffers(conn);
-          pn_raw_connection_close(conn);
+          pni_raw_close(conn);
           return;
       }
     }
@@ -524,7 +524,7 @@ pn_event_t *pni_raw_event_next(pn_raw_connection_t *conn) {
   } while (true);
 }
 
-void pn_raw_connection_close(pn_raw_connection_t *conn) {
+void pni_raw_close(pn_raw_connection_t *conn) {
   // TODO: Do we need different flags here?
   // TODO: What is the precise semantics for close?
   bool rclosed = conn->rclosed;
diff --git a/c/tests/raw_connection_test.cpp b/c/tests/raw_connection_test.cpp
index ab404d3..04f2cc3 100644
--- a/c/tests/raw_connection_test.cpp
+++ b/c/tests/raw_connection_test.cpp
@@ -545,7 +545,7 @@ TEST_CASE("raw connection") {
       // No need buffers event as we already gave buffers
       REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_EVENT_NONE);
 
-      pn_raw_connection_close(p);
+      pni_raw_close(p);
 
       size_t rgiven = pn_raw_connection_take_read_buffers(p, &read[0], rtaken);
       REQUIRE(pni_raw_validate(p));


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org