Add `cleanup` option (#53)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
As spawned processes are not cleaned up when the main process dies this option adds the abilitiy to kill those when the spawning process dies. Closes #51
authored by Friedel Ziegelmayer · committed by Sindre Sorhus · parent 03964c1 · Browse files · 6 files·+69 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+69 added−1 removed
- other:+69 added−1 removed
Provenance
- Commit
dbd9acd- Tree
ec3e922- Author
Friedel Ziegelmayer <dignifiedquire@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
03964c1
6 files changed+69 added−1 removed
added:fixtures/sub-process+5 added
1+#!/usr/bin/env node2+'use strict';3+var m = require('../');4+5+console.log(m('forever').pid);added:fixtures/sub-process-false+5 added
1+#!/usr/bin/env node2+'use strict';3+var m = require('../');4+5+console.log(m('forever', {cleanup: false}).pid);modified:index.js+14 added-1 removed
6 unmodified lines7 var npmRunPath = require('npm-run-path');8 var isStream = require('is-stream');9 var _getStream = require('get-stream');10 var pathKey = require('path-key')();11+var onExit = require('signal-exit');12 var errname = require('./lib/errname');13 14 var TEN_MEBIBYTE = 1024 * 1024 * 10;15 17 unmodified lines33 maxBuffer: TEN_MEBIBYTE,34 stripEof: true,35 preferLocal: true,36 encoding: 'utf8',36- reject: true37+ reject: true,38+ cleanup: true39 }, parsed.options);40 41 if (opts.preferLocal) {42 opts.env = objectAssign({}, opts.env || process.env);110 unmodified lines153 } catch (err) {154 return Promise.reject(err);155 }156 157+ var removeExitHandler;158+ if (parsed.opts.cleanup) {159+ removeExitHandler = onExit(function () {160+ spawned.kill();161+ });162+ }163+164 var promise = Promise.all([165 processDone(spawned),166 getStream(spawned, 'stdout', encoding, maxBuffer),167 getStream(spawned, 'stderr', encoding, maxBuffer)5 unmodified lines173 var err = result.err;174 var code = result.code;175 var signal = result.signal;176 177+ if (removeExitHandler) {178+ removeExitHandler();179+ }180+181 if (err || code !== 0 || signal !== null) {182 if (!err) {183 err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);184 80 unmodified linesmodified:package.json+3 added
48 unmodified lines495051525354555657585960616263646511 unmodified lines48 unmodified lines "is-stream": "^1.1.0", "npm-run-path": "^1.0.0", "object-assign": "^4.0.1", "path-key": "^1.0.0", "signal-exit": "^3.0.0", "strip-eof": "^1.0.0" }, "devDependencies": { "ava": "*", "cat-names": "^1.0.2", "coveralls": "^2.11.9", "delay": "^1.3.1", "is-running": "^2.0.0", "nyc": "^6.4.0", "xo": "*" }, "nyc": {11 unmodified linesmodified:readme.md+7 added
9 unmodified lines10 - Supports [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries cross-platform.11 - [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn#why)12 - Higher max buffer. 10 MB instead of 200 KB.13 - [Executes locally installed binaries by name.](#preferlocal)14+- [Cleans up spawned processes when the parent process dies.](#cleanup)15 16 17 ## Install18 119 unmodified lines138 Default: `true`139 140 Setting this to `false` resolves the promise with the error instead of rejecting it.141 142+#### cleanup143 144+Type: `boolean`<br>145+Default: `true`146+147+Keep track of the spawned process and `kill` it when the parent process exits.148+149 ## License150 151 MIT © [Sindre Sorhus](https://sindresorhus.com)modified:test.js+35 added
1 unmodified line2 import stream from 'stream';3 import childProcess from 'child_process';4 import test from 'ava';5 import getStream from 'get-stream';6+import isRunning from 'is-running';7+import delay from 'delay';8 import m from './';9 10 process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;11 272 unmodified lines284 285 test(cmd, ' foo bar', 'foo', 'bar');286 test(cmd, ' baz quz', 'baz', 'quz');287 test(cmd, '');288+289+async function spawnAndKill(t, signal, cleanup) {290+ const name = cleanup ? 'sub-process' : 'sub-process-false';291+ const cp = m(name);292+ let pid;293+294+ cp.stdout.setEncoding('utf8');295+ cp.stdout.on('data', chunk => {296+ pid = parseInt(chunk, 10);297+ t.is(typeof pid, 'number');298+299+ setTimeout(() => {300+ process.kill(cp.pid, signal);301+ }, 100);302+ });303+304+ await t.throws(cp);305+306+ // Give everybody some time to breath and kill things307+ await delay(200);308+309+ t.false(isRunning(cp.pid));310+ t.is(isRunning(pid), !cleanup);311+}312+313+test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);314+test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);315+316+if (process.platform !== 'win32') {317+ // On Windows the subprocesses are actually always killed318+ test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);319+ test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);320+}