Skip to content

worker: emit worker exit notifications on BroadcastChannel - #65575

Open
SudhansuBandha wants to merge 1 commit into
nodejs:mainfrom
SudhansuBandha:worker-lifecycle-broadcast-channel
Open

worker: emit worker exit notifications on BroadcastChannel#65575
SudhansuBandha wants to merge 1 commit 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 Worker support. labels Aug 27, 2026

@Labeeb2339 Labeeb2339 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The current lint-cpp check reports 10 style errors in the new C++ code, so this PR is not yet lint-clean. Notably: trailing whitespace at src/node_messaging.cc:855 and src/node_messaging.h:194/233/235, an over-80-character line at src/node_messaging.cc:896, missing spaces in if(environment->is_stopping()) at :970, and incorrect indentation around WorkerExitNotification in src/node_messaging.h:226-229. Running the repository C++ formatter/linter and committing its output should address these before the functional review proceeds.

@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.

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 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

Comment thread src/node_messaging.cc

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)

Comment thread src/node_messaging.cc
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.

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

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

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 code anyway

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 Worker support.

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