fix(ext/node): make worker_threads ref/unref idempotent like Node.js#32161
Merged
bartlomieju merged 7 commits intodenoland:mainfrom Feb 14, 2026
Merged
Conversation
Previously only stderr was forwarded from worker threads to the parent. This adds stdout forwarding using the same mechanism, enabling worker.stdout to emit data events when the worker writes to process.stdout. Also adds the stdin property (null by default) to match Node.js Worker API surface. Enables test-worker-no-stdin-stdout-interaction.js node_compat test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…able for worker stdio The execArgv validation was treating all array elements as flags, but items like "node" and "development" are arguments to preceding flags (e.g. "--conditions node"). Skip items that don't start with '-'. Also upgrade worker.stdout and worker.stderr from EventEmitter to Readable streams to match the Node.js API (supporting pipe/unpipe). Close stdio streams when the worker exits. This fixes vitest 4 compatibility where threads pool passes "--conditions node --conditions development" in execArgv and calls worker.stdout.pipe(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The ref()/unref() methods on Worker were implemented as a reference counter, but Node.js treats them as idempotent boolean toggles. This caused issues where libraries like comlink that call ref() for each pending request but only call unref() once would leave the ref count elevated, preventing the process from exiting. Closes denoland#31871 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Is the test test-worker-unref-from-message-during-exit.js enabled? |
Member
Author
|
@fraidev enabled now. |
2 tasks
littledivy
approved these changes
Feb 14, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #31871
Summary
Worker.ref()/Worker.unref()from a reference counter to an idempotent boolean toggle, matching Node.js semanticsref()/unref()are idempotent: callingref()multiple times is the same as calling it once, and a singleunref()is sufficient to allow the process to exit#refCount) that started at 1 and incremented/decremented on eachref()/unref()call. Libraries like comlink callref()for each pending RPC request but onlyunref()once when all pending requests complete — this left the counter elevated and prevented the process from exitingTest plan
unref()callcargo test --test node_compat parallel -- --filter test-worker-ref-onexit(passes)cargo test --test node_compat parallel -- --filter test-worker-terminate-unrefed(passes)cargo test --test node_compat parallel -- --filter test-worker-unref-from-message(passes)tools/lint.jsandtools/format.jspass🤖 Generated with Claude Code