Skip to content

Removing a signal handler from a process newListener event handler doesn't work correctly #51016

@dividedmind

Description

@dividedmind

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.

Metadata

Metadata

Assignees

Labels

confirmed-bugIssues with confirmed bugs.eventsIssues and PRs related to the events subsystem / EventEmitter.processIssues and PRs related to the process subsystem.wontfixIssues that will not be fixed.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions