fix(ext/node): allow automatic worker_thread termination (#22647)

Co-authored-by: Matt Mastracci <matthew@mastracci.com>
This commit is contained in:
Satya Rohith 2024-03-13 22:52:25 +05:30 committed by GitHub
parent b3ca3b2f25
commit 0fd8f549e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 64 additions and 25 deletions

View file

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