Repository navigation
Close a MessagePort transferred to a worker that never starts #43374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
12
commits into
main
Choose a base branch
from
robobun/196ff3e4/close-ports-on-worker-startup-failure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−3
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
03ba56b
fix(worker_threads): close ports when worker startup fails
steipete a75c99e
test(worker_threads): cover startup workerData ports
steipete 2e666e0
test(worker): cover the Web Worker inbox and terminate() before startup
robobun ffd7d16
worker: keep two comments accurate about who drops an undelivered mes…
robobun c32f42d
test(worker): pin the Web Worker terminate() test with an entry that …
robobun d5e720d
worker: cut the new proxy comments to one line each
robobun 242c6ed
test(worker_threads): move the transferred port tests to a node:test …
robobun 969aa0e
test(worker_threads): say why the shared test imports only Node modules
robobun fc9f99e
test(worker): move the Web Worker inbox tests next to the other inbox…
robobun 8003adb
ci: retrigger
robobun 96bd38f
Merge branch 'main' into robobun/196ff3e4/close-ports-on-worker-start…
alii b0ac5ed
Merge remote-tracking branch 'origin/main' into robobun/196ff3e4/clos…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
test/js/node/worker_threads/worker-transferred-port-close.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Also runs in Node.js (`node --test <file>`), so it uses node:test and imports only Node modules. | ||
| import assert from "node:assert"; | ||
| import { once } from "node:events"; | ||
| import { join } from "node:path"; | ||
| import { describe, test } from "node:test"; | ||
| import { MessageChannel, Worker } from "node:worker_threads"; | ||
|
|
||
| describe("a MessagePort transferred to a worker that never runs is closed", () => { | ||
| // Stays referenced, as a Worker in a pool does: a collected Worker drops its ports too. | ||
| let worker: Worker; | ||
|
|
||
| for (const route of ["workerData", "postMessage"]) { | ||
| // Node reports 'exit' and the port's 'close' in either order, so that order is not asserted. | ||
| test(`the entry does not resolve, port transferred through ${route}`, async () => { | ||
| const { port1, port2 } = new MessageChannel(); | ||
| worker = new Worker( | ||
| join(import.meta.dirname, "worker-transferred-port-close-missing-entry.cjs"), | ||
| route === "workerData" ? { workerData: { port: port2 }, transferList: [port2] } : undefined, | ||
| ); | ||
| const events: string[] = []; | ||
| worker.on("online", () => events.push("online")); | ||
| worker.on("error", (error: NodeJS.ErrnoException) => events.push(`error:${error.code}`)); | ||
| const portClosed = once(port1, "close").then(() => events.push("port-close")); | ||
| const exited = new Promise(resolve => worker.on("exit", code => resolve(events.push(`exit:${code}`)))); | ||
| if (route === "postMessage") worker.postMessage({ port: port2 }, [port2]); | ||
|
|
||
| await Promise.all([exited, portClosed]); | ||
| assert.deepStrictEqual( | ||
| { first: events.slice(0, 2), rest: events.slice(2).sort() }, | ||
| { first: ["online", "error:MODULE_NOT_FOUND"], rest: ["exit:1", "port-close"] }, | ||
| ); | ||
| }); | ||
|
|
||
| // terminate() in the same tick as the constructor stops the thread before it takes its ports. | ||
| // The thread takes them before any user code runs, so nothing can hold it back, and a thread | ||
| // that wins that race closes them as it exits. The exit code depends on that timing too. | ||
| test(`terminate() stops the worker before it starts, port transferred through ${route}`, async () => { | ||
| const { port1, port2 } = new MessageChannel(); | ||
| worker = new Worker( | ||
| "setInterval(() => {}, 1000)", | ||
| route === "workerData" ? { eval: true, workerData: { port: port2 }, transferList: [port2] } : { eval: true }, | ||
| ); | ||
| const portClosed = once(port1, "close").then(() => "port-close"); | ||
| if (route === "postMessage") worker.postMessage({ port: port2 }, [port2]); | ||
|
|
||
| await worker.terminate(); | ||
| assert.strictEqual(await portClosed, "port-close"); | ||
| }); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.