ensure termination of child dtrace on CLI term by a signal

This commit is contained in:
Trent Mick 2012-11-02 12:15:29 -07:00
parent 77b556d245
commit 4d0f6aeb96
2 changed files with 20 additions and 1 deletions

View file

@ -2,7 +2,9 @@
## bunyan 0.16.1 (not yet released)
(nothing yet)
- Ensure that a possible dtrace child process (with using `bunyan -p PID`) is
terminated on signal termination of the bunyan CLI (at least for SIGINT,
SIGQUIT, SIGTERM, SIGHUP).
## bunyan 0.16.0

View file

@ -65,6 +65,9 @@ Object.keys(levelFromName).forEach(function (name) {
// The current raw input line being processed. Used for `uncaughtException`.
var currLine = null;
// Child dtrace process, if any. Used for signal-handling.
var child = null;
//---- support functions
@ -905,6 +908,7 @@ function processPid(opts, stylize, callback) {
var argv = ['dtrace', '-Z', '-x', 'strsize=4k', '-qn',
format('bunyan%d:::log-*{printf("%s", copyinstr(arg0))}', opts.pid)];
var dtrace = spawn(argv[0], argv.slice(1));
child = dtrace; // intentionall global
dtrace.stderr.pipe(process.stderr);
@ -1023,6 +1027,19 @@ function asyncForEach(arr, iterator, callback) {
//---- mainline
// Try to ensure we close a child 'dtrace' process on signalled exit.
function signalCleanupAndExit(signal) {
if (child) {
child.kill(signal);
}
process.exit();
}
process.on('SIGINT', function () { signalCleanupAndExit('SIGINT'); });
process.on('SIGQUIT', function () { signalCleanupAndExit('SIGQUIT'); });
process.on('SIGTERM', function () { signalCleanupAndExit('SIGTERM'); });
process.on('SIGHUP', function () { signalCleanupAndExit('SIGHUP'); });
process.on('uncaughtException', function (err) {
function indent(s) {
var lines = s.split(/\r?\n/);