-
-
Couldn't load subscription status.
- Fork 33.6k
Description
Version
v21.3.0
Platform
Linux new-hope 6.5.0-2-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.5.6-1 (2023-10-07) x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
Consider this code (the idea is to set up a fallback SIGINT handler that will auto-remove if something else sets a handler):
// sigTest.js
const fallbackHandler = () => console.debug("in fallback handler");
process.on("SIGINT", fallbackHandler);
process.on("newListener", (event) => event === "SIGINT" && process.off("SIGINT", fallbackHandler));
process.on("SIGINT", () => console.debug("in real handler"));
process.stdin.resume();Run node sigTest.js and press Ctrl+C.
How often does it reproduce? Is there a required condition?
This happens regardless of whether the new listener is installed immediately or at a later point.
What is the expected behavior? Why is that the expected behavior?
$ node sigintTest.mjs
^Cin real handler
^Cin real handlerNote the process shouldn't terminate. Adding the "real" handler should cause the "fallback" handler to be removed by the newListener event listener, leaving only the "real" handler as the SIGINT listener.
What do you see instead?
$ node sigintTest.mjs ; printf "\nExit code: $?\n"
^C
Exit code: 1
$Note the process terminates with exit code 1. Neither SIGINT handler runs.
Additional information
Wrapping process.off in a setImmediate makes the code work as expected.