From d8a79bff3ce13cec82d014b179c3f324aea2cf86 Mon Sep 17 00:00:00 2001 From: SudhansuBandha Date: Thu, 27 Aug 2026 11:32:58 +0530 Subject: [PATCH] worker: emit worker exit notifications on BroadcastChannel Expose worker termination notifications through BroadcastChannel so consumers can observe when a worker exits andinspect its thread ID and exit code. Fixes: https://github.com/nodejs/node/issues/59053 Signed-off-by: SudhansuBandha --- doc/api/worker_threads.md | 19 ++++ lib/internal/worker/io.js | 6 ++ src/node_messaging.cc | 89 ++++++++++++++++++- src/node_messaging.h | 16 ++++ test/parallel/test-worker-broadcastchannel.js | 27 ++++++ 5 files changed, 156 insertions(+), 1 deletion(-) diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index e7b6b19b355c..9e96840b13b3 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -987,6 +987,25 @@ added: v15.4.0 * Type: {Function} Invoked with a received message cannot be deserialized. +### `broadcastChannel.onworkerexited` + +* Type: {Function} Invoked when worker associated with the + `BroadcastChannel` terminates. + +The callback receives an object with the following properties: + +* `threadId` {number} The ID of the worker thread that terminated. +* `exitCode` {number} The exit code with which the worker terminated. + +The `exitCode` is the value passed to `process.exit()` when the worker +explicitly exits. If the worker terminates without explicitly specifying +an exit code, the corresponding exit code is reported. + +The `workerexited` event is emitted only when the worker's execution +environment is stopping. Closing a `BroadcastChannel` or its underlying +`MessagePort` does not by itself indicate that a worker has exited and +does not emit this event. + ### `broadcastChannel.postMessage(message)`