DISPATCH-739: Fix memory leak in http server showing in unit tests

Maybe not a leak in a deployed broker but fixed it anyway to keep tests clean.


Project: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/commit/4c98f541
Tree: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/tree/4c98f541
Diff: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/diff/4c98f541

Branch: refs/heads/master
Commit: 4c98f54136c7e5c306be540d53be24423ece714e
Parents: 5738079
Author: Alan Conway <[email protected]>
Authored: Tue Jan 2 10:39:09 2018 -0500
Committer: Alan Conway <[email protected]>
Committed: Tue Jan 2 11:18:35 2018 -0500

----------------------------------------------------------------------
 src/http-libwebsockets.c | 7 ++++++-
 src/http.h               | 5 ++++-
 src/server.c             | 3 ++-
 3 files changed, 12 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/4c98f541/src/http-libwebsockets.c
----------------------------------------------------------------------
diff --git a/src/http-libwebsockets.c b/src/http-libwebsockets.c
index 0b71997..11d7ffe 100644
--- a/src/http-libwebsockets.c
+++ b/src/http-libwebsockets.c
@@ -514,7 +514,7 @@ static void* http_thread_run(void* v) {
     return NULL;
 }
 
-void qd_http_server_free(qd_http_server_t *hs) {
+void qd_http_server_stop(qd_http_server_t *hs) {
     if (!hs) return;
     if (hs->thread) {
         /* Thread safe, stop via work queue then clean up */
@@ -524,6 +524,11 @@ void qd_http_server_free(qd_http_server_t *hs) {
         sys_thread_free(hs->thread);
         hs->thread = NULL;
     }
+}
+
+void qd_http_server_free(qd_http_server_t *hs) {
+    if (!hs) return;
+    qd_http_server_stop(hs);
     work_queue_destroy(&hs->work);
     if (hs->context) lws_context_destroy(hs->context);
     free(hs);

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/4c98f541/src/http.h
----------------------------------------------------------------------
diff --git a/src/http.h b/src/http.h
index a169998..ac02876 100644
--- a/src/http.h
+++ b/src/http.h
@@ -31,7 +31,10 @@ struct qd_log_source_t;
 /* Create a HTTP server */
 qd_http_server_t *qd_http_server(struct qd_server_t *server, struct 
qd_log_source_t *log);
 
-/* Free the HTTP server */
+/* Stop the HTTP server threads */
+void qd_http_server_stop(qd_http_server_t*);
+
+/* Free the HTTP server (stops threads if still running) */
 void qd_http_server_free(qd_http_server_t*);
 
 /* Listening for HTTP, thread safe. */

http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/4c98f541/src/server.c
----------------------------------------------------------------------
diff --git a/src/server.c b/src/server.c
index c990dbd..9fe09f4 100644
--- a/src/server.c
+++ b/src/server.c
@@ -1154,6 +1154,7 @@ qd_server_t *qd_server(qd_dispatch_t *qd, int 
thread_count, const char *containe
 void qd_server_free(qd_server_t *qd_server)
 {
     if (!qd_server) return;
+    qd_http_server_free(qd_server->http); /* Shut down in reverse order of 
start-up */
     qd_connection_t *ctx = DEQ_HEAD(qd_server->conn_list);
     while (ctx) {
         DEQ_REMOVE_HEAD(qd_server->conn_list);
@@ -1201,7 +1202,7 @@ void qd_server_run(qd_dispatch_t *qd)
         sys_thread_free(threads[i]);
     }
     free(threads);
-    qd_http_server_free(qd_server->http);
+    qd_http_server_stop(qd_server->http); /* Stop HTTP threads immediately */
 
     qd_log(qd_server->log_source, QD_LOG_NOTICE, "Shut Down");
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to