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

Revert "Forwarding signals correctly to child process" #281

Merged
merged 1 commit into from Jul 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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