summaryrefslogtreecommitdiff
path: root/runtime/js/99_main.js
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-03-13 22:52:25 +0530
committerGitHub <noreply@github.com>2024-03-13 17:22:25 +0000
commit0fd8f549e2194223eca2d4b17f4e96cd5a0f5fd5 (patch)
tree76181b2a5f2991134f7343cfc6d4b8b755dbc333 /runtime/js/99_main.js
parentb3ca3b2f25931afb350027bde87dc3d4f9a741b0 (diff)
fix(ext/node): allow automatic worker_thread termination (#22647)
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
Diffstat (limited to 'runtime/js/99_main.js')
-rw-r--r--runtime/js/99_main.js13
1 files changed, 12 insertions, 1 deletions
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 27ba488e7..585128ba8 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -279,6 +279,7 @@ function postMessage(message, transferOrOptions = {}) {
let isClosing = false;
let globalDispatchEvent;
+let closeOnIdle;
async function pollForMessages() {
if (!globalDispatchEvent) {
@@ -288,7 +289,14 @@ async function pollForMessages() {
);
}
while (!isClosing) {
- const data = await op_worker_recv_message();
+ const op = op_worker_recv_message();
+ // In a Node.js worker, unref() the op promise to prevent it from
+ // keeping the event loop alive. This avoids the need to explicitly
+ // call self.close() or worker.terminate().
+ if (closeOnIdle) {
+ core.unrefOpPromise(op);
+ }
+ const data = await op;
if (data === null) break;
const v = messagePort.deserializeJsMessageData(data);
const message = v[0];
@@ -803,6 +811,8 @@ function bootstrapWorkerRuntime(
6: argv0,
7: shouldDisableDeprecatedApiWarning,
8: shouldUseVerboseDeprecatedApiWarning,
+ 9: _future,
+ 10: closeOnIdle_,
} = runtimeOptions;
deprecatedApiWarningDisabled = shouldDisableDeprecatedApiWarning;
@@ -864,6 +874,7 @@ function bootstrapWorkerRuntime(
location.setLocationHref(location_);
+ closeOnIdle = closeOnIdle_;
globalThis.pollForMessages = pollForMessages;
// TODO(bartlomieju): deprecate --unstable