Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle signals more correctly #142

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ const runScriptPkg = async options => {
return p.catch(er => {
const { signal } = er
if (stdio === 'inherit' && signal) {
// by the time we reach here, the child has already exited. we send the
// signal back to ourselves again so that npm will exit with the same
// status as the child
process.kill(process.pid, signal)

// just in case we don't die, reject after 500ms
// this also keeps the node process open long enough to actually
// get the signal, rather than terminating gracefully.
Expand Down
14 changes: 8 additions & 6 deletions lib/signal-manager.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const runningProcs = new Set()
let handlersInstalled = false

// NOTE: these signals aren't actually forwarded anywhere. they're trapped and
// ignored until all child processes have exited. in our next breaking change
// we should rename this
const forwardedSignals = [
'SIGINT',
'SIGTERM',
]

const handleSignal = signal => {
for (const proc of runningProcs) {
proc.kill(signal)
}
}
Comment on lines -9 to -13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this assumes npm is the process group leader and that signals to npm's pid therefore get propagated to all other pids in the group.

That's a bad assumption though. It's a breaking - and, I suspect, unintentional - change for processes that switch process groups. Those stay behind now.

Copy link
Contributor

@wiktor-obrebski wiktor-obrebski Dec 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the author might have confused terminating the program with CTRL + C (which still works) with sending a SIGTERM signal, for example, by using kill -SIGTERM $PID. This particular functionality seems to have been broken by this commit.


// no-op, this is so receiving the signal doesn't cause us to exit immediately
// instead, we exit after all children have exited when we re-send the signal
// to ourselves. see the catch handler at the bottom of run-script-pkg.js
// istanbul ignore next - this function does nothing
const handleSignal = () => {}
const setupListeners = () => {
for (const signal of forwardedSignals) {
process.on(signal, handleSignal)
Expand Down
21 changes: 0 additions & 21 deletions test/signal-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,3 @@ test('adds only one handler for each signal, removes handlers when children have

t.end()
})

test('forwards signals to child process', t => {
const proc = new EventEmitter()
proc.kill = (signal) => {
t.equal(signal, signalManager.forwardedSignals[0], 'child receives correct signal')
proc.emit('exit', 0)
for (const forwarded of signalManager.forwardedSignals) {
t.equal(
process.listeners(forwarded).includes(signalManager.handleSignal),
false, 'listener has been removed')
}
t.end()
}

signalManager.add(proc)
// passing the signal name here is necessary to fake the effects of actually
// receiving the signal per nodejs documentation signal handlers receive the
// name of the signal as their first parameter
// https://nodejs.org/api/process.html#process_signal_events
process.emit(signalManager.forwardedSignals[0], signalManager.forwardedSignals[0])
})