Skip to content

Commit

Permalink
improved safety when joining various threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlalis committed Dec 8, 2023
1 parent 4cca4ab commit 9ed5585
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 1 addition & 3 deletions integration-tests/speed-test/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion integration-tests/speed-test/source/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 10 additions & 2 deletions source/handy_httpd/components/worker_pool.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ 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);
}
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.");
}

Expand Down
6 changes: 5 additions & 1 deletion source/handy_httpd/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down

0 comments on commit 9ed5585

Please sign in to comment.