-
Notifications
You must be signed in to change notification settings - Fork 24
forward signals to children #9
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15f9a1f
bump promise-spawn dependency
nlf 41c1cf5
add signal manager with tests
nlf 71844d8
only attach signal manager when stdio is inherit
nlf d18415d
address review comments
nlf 9b26137
put back the dangly comma
nlf 8264ee4
add .js to require for consistency
nlf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const runningProcs = new Set() | ||
let handlersInstalled = false | ||
|
||
const forwardedSignals = [ | ||
'SIGINT', | ||
'SIGTERM' | ||
] | ||
|
||
const handleSignal = signal => { | ||
for (const proc of runningProcs) { | ||
proc.kill(signal) | ||
} | ||
} | ||
|
||
const setupListeners = () => { | ||
for (const signal of forwardedSignals) { | ||
process.on(signal, handleSignal) | ||
} | ||
handlersInstalled = true | ||
} | ||
|
||
const cleanupListeners = () => { | ||
if (runningProcs.size === 0) { | ||
for (const signal of forwardedSignals) { | ||
process.removeListener(signal, handleSignal) | ||
} | ||
handlersInstalled = false | ||
} | ||
} | ||
|
||
const add = proc => { | ||
runningProcs.add(proc) | ||
if (!handlersInstalled) | ||
setupListeners() | ||
|
||
proc.once('exit', ({ code }) => { | ||
process.exitCode = process.exitCode || code | ||
runningProcs.delete(proc) | ||
cleanupListeners() | ||
}) | ||
} | ||
|
||
module.exports = { | ||
add, | ||
handleSignal, | ||
forwardedSignals | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { EventEmitter } = require('events') | ||
const { test } = require('tap') | ||
|
||
const signalManager = require('../lib/signal-manager') | ||
|
||
test('adds only one handler for each signal, removes handlers when children have exited', t => { | ||
const procOne = new EventEmitter() | ||
const procTwo = new EventEmitter() | ||
|
||
for (const signal of signalManager.forwardedSignals) { | ||
t.equal(process.listeners(signal).includes(signalManager.handleSignal), false, 'does not have a listener yet') | ||
} | ||
signalManager.add(procOne) | ||
|
||
for (const signal of signalManager.forwardedSignals) { | ||
t.equal(process.listeners(signal).includes(signalManager.handleSignal), true, 'has a listener for forwarded signals') | ||
} | ||
|
||
signalManager.add(procTwo) | ||
for (const signal of signalManager.forwardedSignals) { | ||
const handlers = process.listeners(signal).filter((fn) => fn === signalManager.handleSignal) | ||
t.equal(handlers.length, 1, 'only has one handler') | ||
} | ||
|
||
procOne.emit('exit', { code: 0 }) | ||
|
||
for (const signal of signalManager.forwardedSignals) { | ||
t.equal(process.listeners(signal).includes(signalManager.handleSignal), true, 'did not remove listeners yet') | ||
} | ||
|
||
procTwo.emit('exit', { code: 0 }) | ||
|
||
for (const signal of signalManager.forwardedSignals) { | ||
t.equal(process.listeners(signal).includes(signalManager.handleSignal), false, 'listener has been removed') | ||
} | ||
|
||
t.done() | ||
}) | ||
|
||
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', { code: 0 }) | ||
for (const signal of signalManager.forwardedSignals) { | ||
t.equal(process.listeners(signal).includes(signalManager.handleSignal), false, 'listener has been removed') | ||
} | ||
t.done() | ||
} | ||
|
||
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]) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.