Skip to content

Commit 8ad78f3

Browse files
committed
fix(cleanup): ensure cleanup gets run on process.exit calls
1 parent 9e0ffab commit 8ad78f3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/command.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ class Command {
163163
if (commandInstance.cleanup) {
164164
debug(`cleanup handler found for ${commandName}`);
165165
const cleanup = commandInstance.cleanup.bind(commandInstance);
166-
// process.on('exit') is unreliable apparently, so we do this
167-
process.removeAllListeners('SIGINT').on('SIGINT', cleanup)
168-
.removeAllListeners('SIGTERM').on('SIGTERM', cleanup);
166+
// bind cleanup handler to SIGINT, SIGTERM, and exit events
167+
process.removeAllListeners('SIGINT').on('SIGINT', cleanup) // handle ctrl + c from keyboard
168+
.removeAllListeners('SIGTERM').on('SIGTERM', cleanup) // handle kill signal from something like `kill`
169+
.removeAllListeners('exit').on('exit', cleanup); // handle process.exit calls from within CLI codebase
169170
}
170171

171172
try {

test/unit/command-spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,10 @@ describe('Unit: Command', function () {
362362
expect(loadOsInfo.calledOnce).to.be.true;
363363
expect(runStub.calledOnce).to.be.true;
364364
expect(runStub.calledWithExactly({verbose: false, prompt: false, development: false, auto: true})).to.be.true;
365-
expect(onStub.calledTwice).to.be.true;
365+
expect(onStub.calledThrice).to.be.true;
366366
expect(onStub.calledWith('SIGINT')).to.be.true;
367367
expect(onStub.calledWith('SIGTERM')).to.be.true;
368+
expect(onStub.calledWith('exit')).to.be.true;
368369
});
369370

370371
it('runs updateCheck if checkVersion property is true on command class', async function () {

0 commit comments

Comments
 (0)