mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
fix(ext/node): allow automatic worker_thread termination (#22647)
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
This commit is contained in:
parent
b3ca3b2f25
commit
0fd8f549e2
11 changed files with 64 additions and 25 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue