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 handler
Note 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.
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):
Run
node sigTest.jsand 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?
Note the process shouldn't terminate. Adding the "real" handler should cause the "fallback" handler to be removed by the
newListenerevent listener, leaving only the "real" handler as the SIGINT listener.What do you see instead?
Note the process terminates with exit code 1. Neither SIGINT handler runs.
Additional information
Wrapping
process.offin asetImmediatemakes the code work as expected.