Skip to content

Commit

Permalink
fix(node): wrong worker_threads.terminate() return value (#23803)
Browse files Browse the repository at this point in the history
<!--
Before submitting a PR, please read
https://docs.deno.com/runtime/manual/references/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
   all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->

Fixes #23801

---------

Signed-off-by: Marvin Hagemeister <marvinhagemeister50@gmail.com>
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
  • Loading branch information
marvinhagemeister and bartlomieju committed May 15, 2024
1 parent e661591 commit e02d0fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ext/node/polyfills/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import process from "node:process";
const { JSONParse, JSONStringify, ObjectPrototypeIsPrototypeOf } = primordials;
const {
Error,
PromiseResolve,
Symbol,
SymbolFor,
SymbolIterator,
Expand Down Expand Up @@ -280,7 +281,8 @@ class NodeWorker extends EventEmitter {
this.#status = "TERMINATED";
op_host_terminate_worker(this.#id);
}
this.emit("exit", 1);
this.emit("exit", 0);
return PromiseResolve(0);
}

ref() {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit_node/worker_threads_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,32 @@ Deno.test({
},
});

Deno.test({
name: "[node/worker_threads] Returns terminate promise with exit code",
async fn() {
const deferred = Promise.withResolvers<void>();
const worker = new workerThreads.Worker(
`
import { parentPort } from "node:worker_threads";
parentPort.postMessage("ok");
`,
{
eval: true,
},
);

worker.on("message", (data) => {
assertEquals(data, "ok");
deferred.resolve();
});

await deferred.promise;
const promise = worker.terminate();
assertEquals(typeof promise.then, "function");
assertEquals(await promise, 0);
},
});

Deno.test({
name:
"[node/worker_threads] MessagePort.on all message listeners are invoked",
Expand Down

0 comments on commit e02d0fa

Please sign in to comment.