DEV Community

WH yang
WH yang

Posted on Edited on

Update on Deno Watch Mode Signal Issue.

There is an issue in Deno where the signal handler doesn't work in watch mode. I believe this happens because the Deno file watcher doesn't send the INT signal to the worker, causing it to just stop instead. I'm confident about this now.

source

 select! {
      _ = receiver_future => {},
      _ = deno_signals::ctrl_c() => {
        return Ok(()); // this line handled the INT signal and fulfilled select!
      },
      _ = restart_rx.recv() => {
        print_after_restart();
        continue;
      },
      success = operation_future => {...}
Enter fullscreen mode Exit fullscreen mode

In the Deno code, the select! statement acts like JavaScript's Promise.any. It completes as soon as the INT signal is ignored. The operation_future runs the user's JavaScript or TypeScript files, but it doesn't get a chance to activate the user-defined listener.

I also looked at how Node.js handles signals in watch mode. It sends the signal to the worker and waits for the worker to finish.

source

async function killAndWait(signal = kKillSignal, force = false) {
  child?.removeAllListeners();
  if (!child) {
    return;
  }
  if ((child.killed || exited) && !force) {
    return;
  }
  const onExit = once(child, 'exit');
  child.kill(signal);
  const { 0: exitCode } = await onExit;
  return exitCode;
}
Enter fullscreen mode Exit fullscreen mode

I plan to use a similar approach: send the signal to the worker to trigger the listener and then wait for it to complete.

Top comments (0)