Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker.terminate() does not force code to halt. #4134

Closed
paperdave opened this issue Aug 12, 2023 · 1 comment
Closed

worker.terminate() does not force code to halt. #4134

paperdave opened this issue Aug 12, 2023 · 1 comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs

Comments

@paperdave
Copy link
Collaborator

paperdave commented Aug 12, 2023

What version of Bun is running?

any release after #4114

What platform is your computer?

No response

What steps can reproduce the bug?

In #4114, there is one skipped test which is basically trying to terminate a worker like

while(1) {}

The .terminate() will never actually happen as it only is handled in the web_worker.zig event loop. We need to half the current JS execution.

I tried doing this (read VMTraps.h, i exposed some code on zig VM struct to send these) but it either did not work or segfaulted. The workaround only unrefs the worker, which if there is no extra work on the main thread or message listeners, this will be enough to let the main thread die on it's own, but that's not a real solution.

We need to properly kill the JS execution and then emit the close event on the worker.

@paperdave paperdave added bug Something isn't working node.js Compatibility with Node.js APIs labels Aug 12, 2023
@Jarred-Sumner
Copy link
Collaborator

Stop all JavaScript execution in the worker thread as soon as possible. Returns a Promise for the exit code that is fulfilled when the 'exit' event is emitted.

Despite the name, it's a graceful exit once the event loop closes

const Worker = require("worker_threads").Worker;
const worker = new Worker(__dirname + "/worky.js");
worker.ref();
worker.on("message", (msg) => {
  console.log(msg);
});
worker.on("error", (err) => {
  console.log(err);
});
worker.on("exit", (code) => {
  console.log(`Worker exited with code ${code}`);
});
worker.terminate();

setTimeout(() => {}, 1000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs
Projects
None yet
Development

No branches or pull requests

2 participants