Skip to content

Commit

Permalink
signals: ignore them
Browse files Browse the repository at this point in the history
signals are passed to the spawned child process; this means
that the main process should not react to them, and instead
wait for the spawned process to exit as a result of receiving
the forwarded signal.
  • Loading branch information
stelcheck committed Sep 9, 2017
1 parent 2ee9575 commit 7a6b94a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ v8flags(function (err, v8flags) {
{ stdio: 'inherit' }
)

proc.on('exit', function (code: number, signal: string) {
process.on('exit', function () {
if (signal) {
process.kill(process.pid, signal)
} else {
process.exit(code)
}
})
})
// Ignore the following signals; the child process will be receiving them,
// and should exit by itself
process.on('SIGINT', () => proc.kill('SIGINT'))
process.on('SIGTERM', () => proc.kill('SIGTERM'))

// On exit, exit this process with the same exit code
proc.on('exit', (code: number) => process.exit(code))

// If this process is exited with any other signals,
// kill the subprocess with the same signal
process.on('exit', (_code: number, signal: string) => proc.kill(signal))
})
15 changes: 15 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ describe('ts-node', function () {
})

describe('cli', function () {
this.slow(1000)

it('should not react to signals, but forward them to the child process', function (done) {
const proc = exec(`${BIN_EXEC} tests/signals`, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('exited\n')

return done()
})

// Leave enough time for node to fully start
// the process, then send a signal
setTimeout(() => proc.kill('SIGINT'), 1000)
})

it('should execute cli', function (done) {
exec(`${BIN_EXEC} tests/hello-world`, function (err, stdout) {
expect(err).to.equal(null)
Expand Down
8 changes: 8 additions & 0 deletions tests/signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
process.on('SIGINT', () => {
setTimeout(() => {
console.log('exited')
process.exit(0)
}, 2000)
})

setInterval(() => console.log('should not be reached'), 4000)

0 comments on commit 7a6b94a

Please sign in to comment.