-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix lost wakeup when a worker message arrives during a nested event-loop wait #37190
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
6
commits into
main
Choose a base branch
from
farm/d809f9df/worker-drain-wakeup
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.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42d9ee4
Fix lost wakeup when a worker message arrives during a nested event-l…
robobun 956640b
[autofix.ci] apply automated fixes
autofix-ci[bot] baf23c3
Track Dispatching ownership per drain invocation; drain stderr in test
robobun 2e5bc02
Drop undelivered inbox tail on worker termination; cover the budget-y…
robobun e322254
ci: retrigger
robobun e215131
Initialize ownsDispatching
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
Some comments aren't visible on the classic Files Changed page.
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
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
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,101 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/37189 | ||
| // Regression in 1.3.14: a Worker/MessagePort message arriving while the | ||
| // receiver was parked in a nested event-loop wait (expect().rejects) reached | ||
| // from a previous message's continuation was enqueued without posting a | ||
| // wakeup, deadlocking the process. The first awaited call below makes the | ||
| // second call's assertion run as a continuation of the message dispatch, | ||
| // with the drain loop still on the native stack. | ||
|
|
||
| const channelMain = `import { expect } from "bun:test"; | ||
| const { port1, port2 } = new MessageChannel(); | ||
| port2.onmessage = e => { | ||
| port2.postMessage({ type: "reply", id: e.data.id, error: "boom" }); | ||
| }; | ||
| const pending = new Map(); | ||
| port1.onmessage = e => { | ||
| const msg = e.data; | ||
| if (msg.type !== "reply") return; | ||
| const reject = pending.get(msg.id); | ||
| pending.delete(msg.id); | ||
| reject?.(new Error(msg.error)); | ||
| }; | ||
| let seq = 0; | ||
| const call = () => new Promise((_resolve, reject) => { | ||
| const id = ++seq; | ||
| pending.set(id, reject); | ||
| port1.postMessage({ id }); | ||
| }); | ||
|
|
||
| await call().catch(() => {}); | ||
| await expect(call()).rejects.toThrow("boom"); | ||
| port1.close(); | ||
| port2.close(); | ||
| console.log("OK");`; | ||
|
|
||
| const workerMain = `import { expect } from "bun:test"; | ||
| const worker = new Worker(new URL("./worker.js", import.meta.url).href); | ||
| const pending = new Map(); | ||
| worker.onmessage = e => { | ||
| const msg = e.data; | ||
| if (msg.type !== "reply") return; | ||
| const reject = pending.get(msg.id); | ||
| pending.delete(msg.id); | ||
| reject?.(new Error(msg.error)); | ||
| }; | ||
| let seq = 0; | ||
| const call = () => new Promise((_resolve, reject) => { | ||
| const id = ++seq; | ||
| pending.set(id, reject); | ||
| worker.postMessage({ id }); | ||
| }); | ||
|
|
||
| await call().catch(() => {}); | ||
| await expect(call()).rejects.toThrow("boom"); | ||
| worker.terminate(); | ||
| console.log("OK");`; | ||
|
|
||
| async function expectExitsCleanly(proc: Bun.Subprocess<"ignore", "pipe", "pipe">) { | ||
| const [stdout, exitCode] = await Promise.all([proc.stdout.text(), proc.exited]); | ||
| expect(stdout).toBe("OK\n"); | ||
| expect(exitCode).toBe(0); | ||
|
robobun marked this conversation as resolved.
Outdated
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test.concurrent( | ||
| "expect().rejects settles when the rejection arrives from a Worker message during a nested wait", | ||
| async () => { | ||
| using dir = tempDir("issue-37189-worker", { | ||
| "worker.js": `self.onmessage = e => { | ||
| self.postMessage({ type: "reply", id: e.data.id, error: "boom" }); | ||
| };`, | ||
| "main.js": workerMain, | ||
| }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "main.js"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| timeout: 15_000, | ||
| killSignal: "SIGKILL", | ||
| }); | ||
| await expectExitsCleanly(proc); | ||
| }, | ||
| ); | ||
|
|
||
| test.concurrent( | ||
| "expect().rejects settles when the rejection arrives from a MessageChannel message during a nested wait", | ||
| async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", channelMain], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| timeout: 15_000, | ||
| killSignal: "SIGKILL", | ||
| }); | ||
| await expectExitsCleanly(proc); | ||
| }, | ||
| ); | ||
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.