Skip to content

Commit

Permalink
Wait for Worker in test to initialize before terminating
Browse files Browse the repository at this point in the history
This fixes a sporadic panic caused by a race condition between a Worker
loading our test-specific Rust extension and the worker being shut down.
This shouldn't panic in principle, but fixing it would require
introducing a leak (see 2bf1525 for the change we could have made and
decided not to).
  • Loading branch information
rf- committed Nov 4, 2022
1 parent aeb3ab8 commit c0330a3
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions test/napi/lib/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
isMainThread,
parentPort,
threadId,
workerData,
} = require("worker_threads");

const addon = require("..");
Expand All @@ -19,6 +20,7 @@ if (!isMainThread) {
let boxed_channels = addon.box_channels();

addon.get_or_init_thread_id(threadId);

parentPort.once("message", (message) => {
try {
switch (message) {
Expand Down Expand Up @@ -47,6 +49,10 @@ if (!isMainThread) {
}
});

if (workerData === "notify_when_startup_complete") {
parentPort.postMessage("startup_complete");
}

return;
}

Expand Down Expand Up @@ -197,9 +203,13 @@ describe("Instance-local storage", () => {
});

it("should be able to exit a worker without a crash", (cb) => {
const worker = new Worker(__filename);
const worker = new Worker(__filename, {
workerData: "notify_when_startup_complete",
});

setTimeout(() => worker.terminate(), 50);
setTimeout(cb, 100);
worker.once("message", async () => {
await worker.terminate();
cb();
});
});
});

0 comments on commit c0330a3

Please sign in to comment.