Skip to content

fix(ext/node): make worker_threads ref/unref idempotent like Node.js#32161

Merged
bartlomieju merged 7 commits intodenoland:mainfrom
bartlomieju:fix/worker-threads-unref-hang
Feb 14, 2026
Merged

fix(ext/node): make worker_threads ref/unref idempotent like Node.js#32161
bartlomieju merged 7 commits intodenoland:mainfrom
bartlomieju:fix/worker-threads-unref-hang

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

@bartlomieju bartlomieju commented Feb 13, 2026

Closes #31871

Summary

  • Changes Worker.ref()/Worker.unref() from a reference counter to an idempotent boolean toggle, matching Node.js semantics
  • In Node.js, ref()/unref() are idempotent: calling ref() multiple times is the same as calling it once, and a single unref() is sufficient to allow the process to exit
  • Deno was using a counter (#refCount) that started at 1 and incremented/decremented on each ref()/unref() call. Libraries like comlink call ref() for each pending RPC request but only unref() once when all pending requests complete — this left the counter elevated and prevented the process from exiting

Test plan

  • Verified with the comlink reproduction repo — process exits correctly after single unref() call
  • cargo 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.js and tools/format.js pass

🤖 Generated with Claude Code

bartlomieju and others added 4 commits February 13, 2026 20:05
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>
@fraidev
Copy link
Copy Markdown
Contributor

fraidev commented Feb 13, 2026

cargo test --test node_compat parallel -- --filter test-worker-unref-from-message (passes)

Is the test test-worker-unref-from-message-during-exit.js enabled?

@bartlomieju
Copy link
Copy Markdown
Member Author

@fraidev enabled now.

@bartlomieju bartlomieju merged commit c3fe993 into denoland:main Feb 14, 2026
86 checks passed
@bartlomieju bartlomieju deleted the fix/worker-threads-unref-hang branch February 14, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Need to call .unref() on a worker up to 5 times to actually unref it

3 participants