Don't call process.exit in exit callback (#1892)

Node calls 'exit' event callbacks when a process is preparing to exit,
via process.exit or otherwise, but it does not execute the next callback
in the chain if a callback calls process.exit.
This commit is contained in:
Sean Gillespie 2018-09-06 10:52:05 -07:00 committed by GitHub
parent 30fccc0773
commit 9acafcfe11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View file

@ -185,7 +185,7 @@ export function run(argv: minimist.ParsedArgs): void {
// If we don't already have an exit code, and we had an unhandled error, exit with a non-success.
if (code === 0 && uncaught) {
process.exit(1);
process.exitCode = 1;
}
});

View file

@ -46,7 +46,10 @@ export function debuggablePromise<T>(p: Promise<T>, ctx?: any): Promise<T> {
process.on("exit", (code: number) => {
// Only print leaks if we're exiting normally. Otherwise, it could be a crash, which of
// course yields things that look like "leaks".
if (code === 0 && !log.hasErrors()) {
//
// process.exitCode is undefined unless set, in which case it's the exit code that was
// passed to process.exit.
if ((process.exitCode === undefined || process.exitCode === 0) && !log.hasErrors()) {
const leakedCount = leakCandidates.size;
if (leakedCount === 0) {
// No leaks - proceed with the exit.