From b847b3b4d9df93f99d02cd1b33e71d02ad7a317f Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 22 Aug 2018 19:23:10 -0500 Subject: [PATCH] BIT-1885: fix input framework memory leak For input threads that get joined during run-time instead of being signalled to stop at termination-time as typical (e.g. an error occurs or process exits w/ non-zero status) messages could remain in the thread's queue and leak. This patches threads to ensure they enter the proper "finished" state so that the thread manager can attempt to fully process and empty out their queues before joining them. --- src/threading/Manager.cc | 1 + src/threading/MsgThread.cc | 16 +++++++++++++++- src/threading/MsgThread.h | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/threading/Manager.cc b/src/threading/Manager.cc index 449f2a8ad1..b07ab0d3d5 100644 --- a/src/threading/Manager.cc +++ b/src/threading/Manager.cc @@ -160,6 +160,7 @@ void Manager::Process() for ( all_thread_list::iterator i = to_delete.begin(); i != to_delete.end(); i++ ) { BasicThread* t = *i; + t->WaitForStop(); all_threads.remove(t); diff --git a/src/threading/MsgThread.cc b/src/threading/MsgThread.cc index c603f20625..bd16206bb4 100644 --- a/src/threading/MsgThread.cc +++ b/src/threading/MsgThread.cc @@ -21,6 +21,8 @@ public: network_time(network_time) { } virtual bool Process() { + if ( Object()->child_finished ) + return true; bool result = Object()->OnFinish(network_time); Object()->Finished(); return result; @@ -90,7 +92,19 @@ public: KillMeMessage(MsgThread* thread) : OutputMessage("ReporterMessage", thread) {} - virtual bool Process() { thread_mgr->KillThread(Object()); return true; } + virtual bool Process() + { + auto rval = true; + + if ( ! Object()->child_finished ) + { + rval = Object()->OnFinish(network_time); + Object()->Finished(); + } + + thread_mgr->KillThread(Object()); + return rval; + } }; #ifdef DEBUG diff --git a/src/threading/MsgThread.h b/src/threading/MsgThread.h index 60da2d9668..1a5834c021 100644 --- a/src/threading/MsgThread.h +++ b/src/threading/MsgThread.h @@ -181,6 +181,7 @@ protected: friend class HeartbeatMessage; friend class FinishMessage; friend class FinishedMessage; + friend class KillMeMessage; /** * Pops a message sent by the child from the child-to-main queue.