saghul / txiki.js

A tiny JavaScript runtime
MIT License
2.37k stars 157 forks source link

Signals don't work if not bound to a variable #476

Closed typeless closed 1 month ago

typeless commented 1 month ago
async function main() {
  //const h = tjs.signal('SIGINT', () => {
  tjs.signal('SIGINT', () => {
    console.log("SIGINT received");
    tjs.exit(0);
  });

  await new Promise((resolve) => { setTimeout(resolve, 10 * 1000) });
  console.log("done waiting");
}

await main();

When entering 'ctrl+c' Expected output (when tjs.singal(...) replaced with const h = tjs.signal(..))

^CSIGINT received

Actual output

^C
saghul commented 1 month ago

Yes, that's kinda by design.

The returned handle allows you to close the handler for example.

If it gets garbage collected, it will be auto-closed and removed.

Not sure how to best handle this without the potential for resource leaks.

typeless commented 1 month ago

Got it. That makes sense.

saghul commented 1 month ago

On a second thought, something like what Deno does (https://docs.deno.com/runtime/tutorials/os_signals) would be better.