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: Permanently fixes #69 by forcefully shutting down hanging child process #324

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Look up flags and options can be used [in ts-node's docs](https://github.com/Typ
* `--rs` - Allow to restart with "rs" line entered in stdio, disabled by default.
* `--notify` - to display desktop-notifications (Notifications are only displayed if `node-notifier` is installed).
* `--cache-directory` - tmp dir which is used to keep the compiled sources (by default os tmp directory is used)
* `--shutdown-timeout` - A graceful shutdown is attempted. If it does not complete before this timeout it will be force shutdown. (ms, default: 30000)

If you need to detect that you are running with `ts-node-dev`, check if `process.env.TS_NODE_DEV` is set.

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-node-dev",
"version": "2.0.0",
"name": "@sunsama/ts-node-dev",
"version": "2.0.1",
"description": "Compiles your TS app and restarts when files are modified.",
"keywords": [
"restart",
Expand All @@ -11,7 +11,7 @@
],
"repository": {
"type": "git",
"url": "http://github.com/whitecolor/ts-node-dev.git"
"url": "http://github.com/sunsama/ts-node-dev.git"
},
"license": "MIT",
"bin": {
Expand Down
3 changes: 3 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const devFlags = {
'dir',
'deps-level',
'compile-timeout',
'shutdown-timeout',
'ignore-watch',
'interval',
'debounce',
Expand Down Expand Up @@ -124,6 +125,7 @@ type DevOptions = {
'error-recompile': boolean
quiet: boolean
'tree-kill': boolean
'shutdown-timeout': string
}

export type Options = {
Expand All @@ -142,6 +144,7 @@ const opts = minimist(devArgs, {
},
default: {
fork: true,
['shutdown-timeout']: '30000',
},
unknown: function (arg) {
unknown.push(arg)
Expand Down
26 changes: 23 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,34 @@ export const runDev = (
})
compiler.writeReadyFile()
}
const killChild = () => {
const killChild = (signal: 'SIGINT' | 'SIGTERM' = 'SIGTERM') => {
if (!child) return
log.debug('Sending SIGTERM kill to child pid', child.pid)
log.debug(`Sending ${signal} kill to child pid`, child.pid)

const cancelForceKill = function (args: any) {
if (child) {
child.off('exit', cancelForceKill);
if (forceKillChildTimeout) {
clearTimeout(forceKillChildTimeout);
}
}
};

child.on('exit', cancelForceKill);

const forceKillChildTimeout = setTimeout(function () {
if (child) {
log.info('Failed to shutdown gracefully. Forcing a shutdown...');
child.off('exit', cancelForceKill);
killChild('SIGINT')
}
}, parseInt(opts['shutdown-timeout']));

if (opts['tree-kill']) {
log.debug('Using tree-kill')
kill(child.pid)
} else {
child.kill('SIGTERM')
child.kill(signal)
}
}
function stop(willTerminate?: boolean) {
Expand Down