Skip to content

lib: add node:threads/worker and node:threads/messaging - #67801

Open
jasnell wants to merge 2 commits into
nodejs:mainfrom
jasnell:threads-namespace
Open

jasnell wants to merge 2 commits into
nodejs:mainfrom
jasnell:threads-namespace

Conversation

@jasnell

@jasnell jasnell commented Nov 26, 2026

Copy link
Copy Markdown
Member

This is a follow up to the discussion in #67288 on how to organize the worker threads and future green threads modules around a new node:threads/* namespace.

This one moves the exports from node:workers_threads to new node:threads/node:threads/messaging/node:threads/worker modules.

It also adds the IsolateGroup support to worker threads.

/cc @jsumners-nr @ronag

node:worker_threads mixes the APIs of worker threads with ones that
are about messaging between any threads, and with classes that are
globals. Add node:threads/worker for the former and
node:threads/messaging for markAsUntransferable(), markAsUncloneable(),
isMarkedAsUntransferable(), moveMessagePortToContext() and
receiveMessageOnPort(). MessageChannel, MessagePort, BroadcastChannel
and navigator.locks are globals already, so neither module exports
them.

Both modules export the same objects as node:worker_threads, and can
only be loaded with the node: scheme, as `threads` is taken on npm.
node:worker_threads is unchanged, and documented as legacy.

The documentation moves to threads.md. worker_threads.md maps each of
its exports to its new location.

Assisted-by: Claude Opus 5.5
Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell
jasnell requested review from aduh95 and mcollina November 26, 2026 19:15
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/loaders
  • @nodejs/net
  • @nodejs/node-api
  • @nodejs/security-wg
  • @nodejs/single-executable
  • @nodejs/startup
  • @nodejs/userland-migrations

@nodejs-github-bot nodejs-github-bot added errors Issues and PRs related to JavaScript errors originating in Node.js core. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Nov 26, 2026
In builds with pointer compression, every Worker is created in an
isolate group of its own, so each has a 4 GB pointer compression cage
and a read-only heap to itself.

Add node:threads, with an IsolateGroup class: `new IsolateGroup()`
creates a group, `IsolateGroup.current` is the group of the current
thread, and `IsolateGroup.supported` tells whether the build can
create groups. Passing one as the `isolateGroup` option of Worker
creates the worker's isolate in that group, so that workers can share
it with each other or with the thread that creates them. Workers
created without the option are unchanged.

Assisted-by: Claude Opus 5.5
Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell
jasnell requested a review from ljharb November 26, 2026 19:31
@jasnell jasnell added the semver-minor PRs that contain new features and should be released in the next minor version. label Nov 26, 2026
@avivkeller avivkeller added the notable-change PRs with changes that should be highlighted in changelogs. label Nov 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @avivkeller.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 26, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 26, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.85253% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.52%. Comparing base (2dd55c3) to head (f88b09e).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
src/node_worker.cc 85.29% 2 Missing and 3 partials ⚠️
src/node_worker.h 25.00% 3 Missing ⚠️
lib/internal/worker/isolate_group.js 98.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67801      +/-   ##
==========================================
+ Coverage   90.50%   90.52%   +0.02%     
==========================================
  Files         805      809       +4     
  Lines      285818   286021     +203     
  Branches    55182    55204      +22     
==========================================
+ Hits       258666   258914     +248     
+ Misses      17251    17199      -52     
- Partials     9901     9908       +7     
Files with missing lines Coverage Δ
lib/internal/bootstrap/realm.js 97.88% <100.00%> (+0.78%) ⬆️
lib/internal/errors.js 98.12% <100.00%> (+<0.01%) ⬆️
lib/internal/main/worker_thread.js 95.02% <100.00%> (+0.02%) ⬆️
lib/internal/worker.js 97.01% <100.00%> (+0.11%) ⬆️
lib/threads.js 100.00% <100.00%> (ø)
lib/threads/messaging.js 100.00% <100.00%> (ø)
lib/threads/worker.js 100.00% <100.00%> (ø)
src/api/environment.cc 80.73% <100.00%> (+1.06%) ⬆️
src/node_internals.h 82.53% <ø> (ø)
lib/internal/worker/isolate_group.js 98.66% <98.66%> (ø)
... and 2 more

... and 36 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split IsolateGroups out in its own PR?

Also, this is not actually moving anything apart from docs. We would need to keep the old aliases for a long time. Anyway, we should include doc-deprecations for the old module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

errors Issues and PRs related to JavaScript errors originating in Node.js core. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants