Skip to content

worker: emit worker exit notifications on BroadcastChannel - #65575

Open
SudhansuBandha wants to merge 4 commits into
nodejs:mainfrom
SudhansuBandha:worker-lifecycle-broadcast-channel
Open

SudhansuBandha wants to merge 4 commits into
nodejs:mainfrom
SudhansuBandha:worker-lifecycle-broadcast-channel

Conversation

@SudhansuBandha

Copy link
Copy Markdown
Contributor

Expose worker termination notifications through BroadcastChannel so consumers can observe when a worker exits and inspect its thread ID and exit code.

Fixes: #59053

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. worker Issues and PRs related to the worker_threads module and Worker API. labels Aug 27, 2026
@codecov

codecov Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.43750% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.07%. Comparing base (9f37c77) to head (d8a79bf).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/node_messaging.cc 98.27% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65575      +/-   ##
==========================================
+ Coverage   90.05%   90.07%   +0.02%     
==========================================
  Files         751      751              
  Lines      254868   254944      +76     
  Branches    48107    48122      +15     
==========================================
+ Hits       229531   229653     +122     
+ Misses      16511    16471      -40     
+ Partials     8826     8820       -6     
Files with missing lines Coverage Δ
lib/internal/worker/io.js 99.20% <100.00%> (+<0.01%) ⬆️
src/node_messaging.h 63.15% <ø> (ø)
src/node_messaging.cc 83.51% <98.27%> (+0.47%) ⬆️

... and 40 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.

@SudhansuBandha
SudhansuBandha force-pushed the worker-lifecycle-broadcast-channel branch from 4dd297f to d8a79bf Compare August 27, 2026 15:04
Comment thread doc/api/worker_threads.md
* Type: {Function} Invoked with a received message cannot be
deserialized.

### `broadcastChannel.onworkerexited`

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.

This needs a YAML tag for version history tracking

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the fix in latest commit

Comment thread src/node_messaging.cc Outdated

if (worker_exit_notifications_.empty()) return false;

*notification = worker_exit_notifications_.front();

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.

Prefer std::optional<> instead of assigning to out parameters (esp. if they have non-trivial types)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the fix in latest commit

Comment thread src/node_messaging.cc Outdated
return;
}
continue;
}

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.

This is duplicating a significant amount of logic – is there a reason that this needs to be a new message type, and cannot be something that would be conveyed through normal messages in the queue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried to implement a separate pipeline since this notification is related to worker lifecycle instead of inter thread messages of Broadcast Channel. If this is not something to be done then I will update the PR to use existing pipeline for emitting these lifecycle events.

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.

I think that would significantly simplify the implementation and make it easier to write consumers -- events will happen in order from their perspective, rather than seeing a worker exit before all its messages have been sent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will implement the suggested changes. Thanks for your feedback!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the implementation to use existing queue for these events

Comment thread src/node_messaging.cc Outdated
const ExitCode exit_code = environment->exit_code(ExitCode::kNoFailure);

Debug(this,
"Worker exiting: thread_id=%" PRIu64 ", exit_code=%d",

@addaleax addaleax Aug 27, 2026 •

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.

Suggested change
"Worker exiting: thread_id=%" PRIu64 ", exit_code=%d",
"Worker exiting: thread_id=%d, exit_code=%d",

Debug() doesn't care about the actual type anyway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the fix in latest commit

@SudhansuBandha

Copy link
Copy Markdown
Contributor Author

HandleScope handle_scope(env()->isolate());

HandleScope handle_scope(env()->isolate());

I have observed the same line being present twice in OnMessage handler I have kept the first and removed the second. If this is not to be done then please let me know @addaleax I will revert it.

@SudhansuBandha
SudhansuBandha force-pushed the worker-lifecycle-broadcast-channel branch from f23efdb to b1c9fb9 Compare September 3, 2026 15:22
@MikeMcC399

Copy link
Copy Markdown
Contributor

The GitHub Actions CI workflow had previously failed due to problems with GitHub.

I triggered re-runs, however there are now other failures.

The PR branch is now more that 300 commits behind the main branch, and some issues that caused CI failures have been fixed in the meantime.

Please rebase the branch in this PR according to the Pull requests documentation to make sure that all available fixes are included.

@SudhansuBandha
SudhansuBandha force-pushed the worker-lifecycle-broadcast-channel branch from b1c9fb9 to f3007df Compare September 18, 2026 08:23
@MikeMcC399

Copy link
Copy Markdown
Contributor

GitHub Actions CI workflow tests are hanging up and will probably time out after the default 6 hour maximum duration is reached.

It does not look like other current PRs have this issue.

See Pull requests > Step 6: Test

Before submitting your changes in a pull request, always run the full Node.js test suite.

with further details under BUILDING > Running tests.

@SudhansuBandha

Copy link
Copy Markdown
Contributor Author

@MikeMcC399
I ran the full Windows test suite against my PR. The suite completed with 7346 passing and 46 failures. Most of the failures are Windows environment-specific: symlink tests fail with EPERM, and some tests invoking Unix utilities failed because cat/head were unavailable in my development Windows environment.

I have not received any failure on my Windows machine because of my changes in PR. Can you provide me a suitable approach where I can recreate the CI failure on Windows machine. Will WSL/MSYS2 help in this regards?

@MikeMcC399

Copy link
Copy Markdown
Contributor

That does sounds suspicious! I have retriggered all the tests once again. If you still get failures, then I suggest once again that you rebase (see instructions in #65575 (comment)). Since you are no longer a "First-time Contributor" the workflows should trigger automatically, after you force push, without them needing to be triggered manually by a team member.

@MikeMcC399

Copy link
Copy Markdown
Contributor

The tests are timing out again, so I don't think that rebasing will solve the issue judging by your comment that you are testing on Windows.

Although Node.js supports both Windows and Unix (Linux & macOS), Windows is in practice secondary for development. If you test only on Windows, then you will not see problems that only affect Unix.

I can't advise you in detail how to set up a Unix environment on your Windows machine. There are various options available, including dual-booting with Ubuntu, running Ubuntu in WSL2 under Windows, running Ubuntu in VMware Workstation or using a Docker image.

@SudhansuBandha

Copy link
Copy Markdown
Contributor Author

Thank you @MikeMcC399 for your help. I believe I have missed a failing test in my changes which I am currently looking at it. Hopefully CI issues will get resolved after this.

@SudhansuBandha
SudhansuBandha force-pushed the worker-lifecycle-broadcast-channel branch from f3007df to c1b0497 Compare September 28, 2026 14:11
Expose worker termination notifications through BroadcastChannel
so consumers can observe when a worker exits andinspect
its thread ID and exit code.

Fixes: nodejs#59053
Signed-off-by: SudhansuBandha <bandhasudhansu@gmail.com>
@SudhansuBandha
SudhansuBandha force-pushed the worker-lifecycle-broadcast-channel branch from c1b0497 to ccc39ae Compare September 28, 2026 15:47
@SudhansuBandha

SudhansuBandha commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@MikeMcC399 I have fixed the failing test but still the CI issue is not getting resolved. If it is possible, can you or anyone else debug these failing CI issues against the test suite on Linux/MacOs. I am not able to reproduce on Windows and due to limited hardware resources it is difficult for me to have a dual setup of Windows and Linux on my machine

@MikeMcC399

Copy link
Copy Markdown
Contributor

I'm sorry, I won't be able to help you further with the test problems.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. worker Issues and PRs related to the worker_threads module and Worker API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BroadcastChannel doesn't fill in useful MessageEvent fields like source, or ports

4 participants