Repository navigation
test: keep the worker alive until terminate() in the work-in-flight tests of worker_threads.test.ts - #46647
test: keep the worker alive until terminate() in the work-in-flight tests of worker_threads.test.ts#46647robobun wants to merge 3 commits into
Conversation
…ests of worker_threads.test.ts Three "terminate with work in flight" tests start a worker that queues one thread pool job, posts "go", and has nothing else to do. The parent calls terminate() when "go" arrives and expects exit code 1. If the job completes before terminate() lands, the worker drains its event loop and exits on its own. terminate() then resolves that exit's code (0), or undefined if the parent already handled the exit. Node does the same. The assertion failed whenever the parent reacted late. Each of the three workers now holds an interval, so only terminate() can stop it and the exit code is always 1.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Keep workers active during pending work test/js/node/worker_threads/worker_threads.test.ts |
The transpilation, SubtleCrypto, and zlib worker scripts start an interval before their work, keeping the workers active for the termination tests. |
Suggested reviewers: dylan-conway
Priority:
Merge Risk: 
Minimal · up to c2f3c
-
The test-only change has no identified merge-blocking risk and is ready to merge.
Pre-merge checks: 
4
-
Full details: Description check
-
Explanation
The description explains the intermittent failure, the interval-based fix, test verification, reproduction details, and unrelated failures. It does not use the exact template headings, but it provides the required change and verification information in equivalent sections.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
|
Updated 1:44 AM PT - Oct 24th, 2026
❌ @robobun, your commit c2f3c34 has 2 failures in
🧪 To try this PR locally: bunx bun-pr 46647That installs a local version of the PR into your bun-46647 --bun |
|
Status Reproduced on a release build of main (6292c65), Linux x64, with 12 busy-loop processes in the background: for i in $(seq 1 300); do bun test ./test/js/node/worker_threads/worker_threads.test.ts -t "a transpile queued on the thread pool" > /dev/null 2>&1 || echo fail; done | wc -l
CI The diff is green. The three changed tests pass on every lane that ran them, in all three CI runs. Each run has one red job, and no red job fails a test that this PR changes:
In build 131727 the ASAN lane ran The head commit differs from the commit of build 131561 only by a shorter code comment. The PR is ready for a maintainer to merge. PR: #46647 |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
LGTM — a small test-only de-flake: a ref'd setInterval in each worker removes the race between the pool job finishing and terminate() landing, while the parent still terminates on the same "go" signal so the in-flight teardown path these tests exist for is unchanged.
Extended reasoning...
The diff adds eight lines to test/js/node/worker_threads/worker_threads.test.ts, interpolating a shared setInterval keepAlive into the three worker sources under "terminate with work in flight"; no runtime or native code changes. It touches no security-sensitive surface. The tests' stated purpose (per the describe-block header) is to exercise VM teardown with pool work pending, where a broken build crashes or trips ASAN rather than failing an assertion, and the keepAlive leaves that path intact since the parent still calls terminate() as soon as "go" arrives; the exit-code assertion now holds because only terminate() can stop a ref'd timer, matching Node. The one inline finding is a comment-style nit, and no third-party objections are outstanding.
There was a problem hiding this comment.
LGTM — the latest push collapses the keep-alive comment to a single line, which was the only point raised last time, and the test change itself is sound.
What was reviewed:
- The
keepAlivesplice in all three workers: it sits beforepostMessage("go")in each, so the parent still callsterminate()only after the pool/crypto/zlib job has been queued; the in-flight teardown path these tests exist for is unchanged. expect(await w.terminate()).toBe(1)can still fail: with a ref'd interval the worker cannot reach a natural exit, so a0/undefinednow meansterminate()failed to stop a running worker rather than a scheduling race.- The fourth test (fetch streaming, subprocess) and the rest of the file are untouched; nothing is skipped or weakened.
Extended reasoning...
Test-only change confined to the "terminate with work in flight" describe block of test/js/node/worker_threads/worker_threads.test.ts: a shared setInterval(() => {}, 1e6) snippet is interpolated into three worker sources so the worker's event loop stays open until the parent's terminate() lands, removing the race where a finished job let the worker exit 0 first. It touches no runtime code and no security-sensitive surface. The only prior review feedback (a three-line narrating comment) was addressed by the follow-up commit, the changed file is not covered by CODEOWNERS, and the hunt ran dry with no findings, so the change is small and self-contained enough to approve.
Behaviour change: none
Problem
terminate with work in flight > a transpile queued on the thread pool that starts after terminate()intest/js/node/worker_threads/worker_threads.test.tsfails intermittently.expect(await w.terminate()).toBe(1)reportsReceived: 0, less oftenReceived: undefined."go", and has no other work. If the job completes before the parent'sterminate()lands, the worker exits on its own andterminate()resolves its code, 0.Fix
setInterval(() => {}, 1e6). Onlyterminate()can stop it, so the exit code is always 1."go"as before.Background
worker.terminate()resolves the worker's exit code: 1 when the parent stops a running worker. A worker that already began its own exit keeps its own code.big.ts(it narrows the race) and accepting 0 or 1 (it hides aterminate()that fails to stop a running worker).Notes
Reproduction. Release build of this branch (
src/is the same as main at 6292c65), Linux x64, 12 cores:Received: 0, 1Received: undefined)The sibling tests on the release build of this branch, under the same load, interleaved:
a SubtleCrypto digest still on the work queue at terminate()an async zlib job on the thread pool at terminate()Cause. A probe ran the transpile worker with two markers: one message from the
import().then()callback and one fromprocess.on("beforeExit"). In 9000 workers across 6 parallel processes, 20 exited with code 0. Each of the 20 had posted thebeforeExitmarker, so its event loop drained before the termination request arrived. No worker exited 0 without that marker.Runtime path.
WebWorker::shutdownfirst removes the published VM handle (src/jsc/web_worker.rs:1024).request_terminationsetsterminated_by_parentonly while the handle is published (src/jsc/web_worker.rs:559), and only that flag turns the exit code into 1.Received: undefinedis the later stage of the same schedule: the parent already handled the exit, andterminate()on an exited worker resolvesundefined(src/js/node/worker_threads.ts:1070), as in Node.Node parity. The parent blocks in
Atomics.waitafter"go", so each job completes beforeterminate()is called. Five workers per case:[0,0,0,0,0]each[0,0,0,0,0]each[1,1,1,1,1]each[1,1,1,1,1]eachHow late the parent must be. Time in the worker from
postMessage("go")tobeforeExit, 25 samples: transpile 11.4 to 22.3 ms, digest 40.1 to 102.7 ms, zlib 45.2 to 63.1 ms.Job in flight at
terminate(). The same probe with the interval: 0 of 7200 workers (6000 transpile, 600 digest, 600 zlib) exited 0. The job was already complete whenterminate()landed in 20 of the 6000 transpile workers. Without the interval it was 22 of 9000.Whole file. Release build: 161 pass. Debug ASAN build, three runs: the three changed tests pass in each run. Runs two and three: 161 pass. Run one: 160 pass and one failure in a test that this PR does not touch,
worker stop ordering as seen by the worker's own handlers > terminate(): nothing of the worker's runs after the request(Received [12, 0]). #38194 describes that failure and covers it.Related. #39067 keeps these three tests serial because of this race. With the interval they do not depend on an idle parent.
[auto-merge] gate passed · iteration 1 · 1 files touched
passes on PR (with fix)
diff hotspot
gate history · 2 passed · 0 rejected · iteration 1
evidence per changed file