From d2146e38810e3d012859325901cf0548d9264eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 22 Feb 2018 20:09:31 +0100 Subject: [PATCH] Fix shutdown of the worker thread pool. Shuts down the pool before terminating the threads to avoid accessing an event thread waiter slot that is not associated with a valid eventcore driver anymore. --- source/vibe/core/core.d | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/source/vibe/core/core.d b/source/vibe/core/core.d index 83bef51..998971e 100644 --- a/source/vibe/core/core.d +++ b/source/vibe/core/core.d @@ -254,6 +254,7 @@ void exitEventLoop(bool shutdown_all_threads = false) assert(s_eventLoopRunning || shutdown_all_threads, "Exiting event loop when none is running."); if (shutdown_all_threads) { () @trusted nothrow { + shutdownWorkerPool(); atomicStore(st_term, true); st_threadsSignal.emit(); } (); @@ -1349,13 +1350,8 @@ static ~this() } if (is_main_thread) { - shared(TaskPool) tpool; - synchronized (st_threadsMutex) swap(tpool, st_workerPool); - if (tpool) { - logDiagnostic("Main thread still waiting for worker threads."); - tpool.terminate(); - } logDiagnostic("Main thread exiting"); + shutdownWorkerPool(); } synchronized (st_threadsMutex) { @@ -1373,6 +1369,19 @@ static ~this() st_threadShutdownCondition.notifyAll(); } +private void shutdownWorkerPool() +nothrow { + shared(TaskPool) tpool; + + try synchronized (st_threadsMutex) swap(tpool, st_workerPool); + catch (Exception e) assert(false, e.msg); + + if (tpool) { + logDiagnostic("Still waiting for worker threads to exit."); + tpool.terminate(); + } +} + private void shutdownDriver() { if (ManualEvent.ms_threadEvent != EventID.init) {