diff --git a/src/types.ts b/src/types.ts index 94c2ac747..052f1bb28 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ export type ArgvOrCommandLine = string[] | string; export interface IExitEvent { - exitCode: number; + exitCode: number | null; signal: number | undefined; } diff --git a/src/unixTerminal.test.ts b/src/unixTerminal.test.ts index a666e91aa..191102708 100644 --- a/src/unixTerminal.test.ts +++ b/src/unixTerminal.test.ts @@ -117,12 +117,21 @@ if (process.platform !== 'win32') { const term = new UnixTerminal('node'); it('should exit when terminal is destroyed programmatically', (done) => { term.on('exit', (code, signal) => { - assert.strictEqual(code, 0); + assert.strictEqual(code, null); assert.strictEqual(signal, constants.signals.SIGHUP); done(); }); term.destroy(); }); + it('should report a null exit code when killed by a signal', (done) => { + const killed = new UnixTerminal('node'); + killed.onExit(({ exitCode, signal }) => { + assert.strictEqual(exitCode, null); + assert.strictEqual(signal, constants.signals.SIGKILL); + done(); + }); + killed.kill('SIGKILL'); + }); }); describe('signals in parent and child', () => { it('SIGINT - custom in parent and child', done => { diff --git a/src/unixTerminal.ts b/src/unixTerminal.ts index 2776d501e..d53cfafea 100644 --- a/src/unixTerminal.ts +++ b/src/unixTerminal.ts @@ -75,7 +75,10 @@ export class UnixTerminal extends Terminal { const encoding = (opt.encoding === undefined ? 'utf8' : opt.encoding); - const onexit = (code: number, signal: number): void => { + const onexit = (rawCode: number, signal: number): void => { + // Align with child_process: a process terminated by a signal has no + // exit code, so report null instead of a misleading 0. + const code = signal ? null : rawCode; // XXX Sometimes a data event is emitted after exit. Wait til socket is // destroyed. if (!this._emittedClose) { diff --git a/typings/node-pty.d.ts b/typings/node-pty.d.ts index a6fc5dd0b..110b3b608 100644 --- a/typings/node-pty.d.ts +++ b/typings/node-pty.d.ts @@ -151,9 +151,11 @@ declare module 'node-pty' { /** * Adds an event listener for when an exit event fires. This happens when the pty exits. + * On Unix, `exitCode` is `null` when the process was terminated by a signal, in which case + * `signal` holds the signal number. * @returns an `IDisposable` to stop listening. */ - readonly onExit: IEvent<{ exitCode: number, signal?: number }>; + readonly onExit: IEvent<{ exitCode: number | null, signal?: number }>; /** * Resizes the dimensions of the pty.