From 9ed558574edcc436aec1f5b19c032d426b4a99aa Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 8 Dec 2023 08:29:57 -0500 Subject: [PATCH] improved safety when joining various threads. --- integration-tests/speed-test/source/app.d | 4 +--- integration-tests/speed-test/source/test.d | 9 ++++++++- source/handy_httpd/components/worker_pool.d | 12 ++++++++++-- source/handy_httpd/server.d | 6 +++++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/integration-tests/speed-test/source/app.d b/integration-tests/speed-test/source/app.d index aad6191..8f7aaf1 100644 --- a/integration-tests/speed-test/source/app.d +++ b/integration-tests/speed-test/source/app.d @@ -2,15 +2,13 @@ import slf4d; import slf4d.default_provider; import handy_httpd; -import std.datetime; import core.cpuid; -import core.thread; import requester; import test; int main() { - auto prov = new shared DefaultProvider(false, Levels.INFO); + auto prov = new shared DefaultProvider(true, Levels.INFO); prov.getLoggerFactory().setModuleLevelPrefix("handy_httpd", Levels.WARN); prov.getLoggerFactory().setModuleLevelPrefix("requester-", Levels.INFO); configureLoggingProvider(prov); diff --git a/integration-tests/speed-test/source/test.d b/integration-tests/speed-test/source/test.d index d78cad5..f9ca790 100644 --- a/integration-tests/speed-test/source/test.d +++ b/integration-tests/speed-test/source/test.d @@ -34,10 +34,17 @@ class SpeedTest { const SysTime testStartTime = Clock.currTime(); foreach (r; requesters) r.start(); info("Started requester threads."); - foreach (r; requesters) r.join(); + foreach (r; requesters) { + try { + r.join(); + } catch (Exception e) { + error("Failed to join requester thread "~r.name, e); + } + } const Duration testDuration = Clock.currTime() - testStartTime; info("Joined all requester threads."); server.stop(); + serverThread.join(); info("Stopped the server."); return showStats(testDuration); } diff --git a/source/handy_httpd/components/worker_pool.d b/source/handy_httpd/components/worker_pool.d index 49e2ef4..ce3c52b 100644 --- a/source/handy_httpd/components/worker_pool.d +++ b/source/handy_httpd/components/worker_pool.d @@ -60,7 +60,11 @@ class WorkerPool { this.managerThread.notify(); synchronized(this.workersMutex.writer) { this.server.notifyWorkerThreads(); - this.workerThreadGroup.joinAll(); + try { + this.workerThreadGroup.joinAll(); + } catch (Exception e) { + error("An exception was thrown by a joined worker thread.", e); + } debug_("All worker threads have terminated."); foreach (worker; this.workers) { this.workerThreadGroup.remove(worker); @@ -68,7 +72,11 @@ class WorkerPool { this.workers = []; this.nextWorkerId = 1; } - this.managerThread.join(); + try { + this.managerThread.join(); + } catch (Exception e) { + error("An exception was thrown when the managerThread was joined.", e); + } debug_("The manager thread has terminated."); } diff --git a/source/handy_httpd/server.d b/source/handy_httpd/server.d index ab91066..c571053 100644 --- a/source/handy_httpd/server.d +++ b/source/handy_httpd/server.d @@ -154,7 +154,11 @@ class HttpServer { this.workerPool.stop(); if (this.websocketManager !is null) { this.websocketManager.stop(); - this.websocketManager.join(); + try { + this.websocketManager.join(); + } catch (Exception e) { + error("Failed to join websocketManager thread because an exception was thrown.", e); + } } info("Server shut down."); }