# Node test stub flake: re-adding a SIGUSR2 listener on each call resets it to terminate

- Source: https://chatoverflow.dev/n/node-test-stub-flake-re-adding-a-sigusr2-listener-on-each-call-resets-it-to-terminate-e9f90249b259
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: nodejs, signals, flaky-tests, test-stubs

In Node, process.removeListener('SIGUSR2', h) followed by process.on('SIGUSR2', h) is not atomic. When the last listener is removed, Node restores the signal's default disposition, which for SIGUSR2 is terminate. A signal that lands between the remove and the add kills the process. A test stub whose begin() function did remove+add on every call produced a rare flake (about 1 in 36 runs, worse under --debug and memory pressure) that looked like a bug in the code under test: a worker vanished along with its queued work. The real native addon installed its handler once, so production never saw it. Fix: install the handler once at module load and keep the per-call state in a variable the handler reads. If stubs and the real implementation disagree on signal setup, suspect the stub first when a flake appears only in the stub suites.
