Skip to content

Commit

Permalink
Merge pull request #281 from wclr/revert-269-aalayo-fix/graceful-shut…
Browse files Browse the repository at this point in the history
…down

Revert "Forwarding signals correctly to child process"
  • Loading branch information
wclr committed Jul 1, 2021
2 parents 534de06 + 1ef356f commit 801cca8
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions src/index.ts
Expand Up @@ -16,7 +16,6 @@ import { makeLog } from './log'
const version = require('../package.json').version
const tsNodeVersion = require('ts-node').VERSION
const tsVersion = require('typescript').version
const signals: NodeJS.Signals[] = ['SIGINT', 'SIGTERM']

export const runDev = (
script: string,
Expand Down Expand Up @@ -119,7 +118,6 @@ export const runDev = (
child = fork(cmd[0], cmd.slice(1), {
cwd: process.cwd(),
env: process.env,
detached: true,
})

starting = false
Expand Down Expand Up @@ -169,15 +167,11 @@ export const runDev = (
compiler.compile(message)
})

child.on('close', (code: number, signal: string) => {
log.debug('Child closed with code %s', code)
if (signal) {
log.debug(`Exiting process with signal ${signal}`)
process.kill(process.pid, signal)
} else {
log.debug(`Exiting process with code ${code}`)
process.exit(code)
}
child.on('exit', function (code) {
log.debug('Child exited with code %s', code)
if (!child) return
if (!child.respawn) process.exit(code || 0)
child = undefined
})

if (cfg.respawn) {
Expand Down Expand Up @@ -209,14 +203,14 @@ export const runDev = (
})
compiler.writeReadyFile()
}
const killChild = (signal: NodeJS.Signals) => {
const killChild = () => {
if (!child) return
log.debug(`Sending ${signal} to child pid`, child.pid)
log.debug('Sending SIGTERM kill to child pid', child.pid)
if (opts['tree-kill']) {
log.debug('Using tree-kill')
kill(child.pid)
} else {
child.kill(signal)
child.kill('SIGTERM')
}
}
function stop(willTerminate?: boolean) {
Expand All @@ -229,8 +223,8 @@ export const runDev = (
log.debug('Disconnecting from child')
child.disconnect()
if (!willTerminate) {
killChild()
}
killChild('SIGTERM')
}
}

Expand Down Expand Up @@ -267,17 +261,11 @@ export const runDev = (
}
}

signals.forEach((signal: NodeJS.Signals) =>
process.on(signal, () => {
log.debug(`Process got ${signal}, killing child`)
killChild(signal)
})
)

process.on('exit', () => {
if(child) {
child.kill()
}
// Relay SIGTERM
process.on('SIGTERM', function () {
log.debug('Process got SIGTERM')
killChild()
process.exit(0)
})

const compiler = makeCompiler(opts, {
Expand Down

0 comments on commit 801cca8

Please sign in to comment.