Skip to content

Commit 5405fe4

Browse files
authored
fix(cli-repl): account for possibility of process.exit() throwing under coverage MONGOSH-1943 (#2298)
When running under `nyc` for coverage generation, the process's `process.exit()` internals get monkey-patched to give `nyc` the opportunity to write coverage data as part of that operation. However, for processes running under changed working directories, `nyc` may try to write to an incorrect directory, making the `fs.writeFile()` call fail with an exception, and so `process.exit()` may not actually stop the process. This commit adds a `process.abort()` call to make that accounts for this situation, as well as improved debugging for it.
1 parent 1bbd86c commit 5405fe4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

packages/cli-repl/src/run.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,7 @@ async function main() {
232232
process.env.TEST_USE_STDOUT_FOR_PASSWORD || process.stdout.isTTY
233233
? process.stdout
234234
: process.stderr,
235-
// Node.js 20.0.0 made p.exit(undefined) behave as p.exit(0) rather than p.exit()
236-
onExit: (code?: number | undefined) =>
237-
code === undefined ? process.exit() : process.exit(code),
235+
onExit,
238236
shellHomePaths: shellHomePaths,
239237
globalConfigPaths: globalConfigPaths,
240238
});
@@ -341,3 +339,25 @@ function suppressExperimentalWarnings() {
341339
};
342340
}
343341
}
342+
343+
function onExit(code?: number): never {
344+
// Node.js 20.0.0 made p.exit(undefined) behave as p.exit(0) rather than p.exit(): (code?: number | undefined): never => {
345+
try {
346+
try {
347+
if (code === undefined) process.exit();
348+
else process.exit(code);
349+
} finally {
350+
if (code === undefined) process.exit();
351+
else process.exit(code);
352+
}
353+
} catch (err) {
354+
process.stderr.write(String(err) + '\n');
355+
} finally {
356+
// Should never be reachable anyway, but nyc monkey-patches process.exit()
357+
// internals so that it can always write coverage files as part of the application
358+
// shutdown, and that can throw an exception if the filesystem call to write
359+
// the coverage file fails.
360+
process.stderr.write('process.exit() returned -- aborting\n');
361+
process.abort();
362+
}
363+
}

0 commit comments

Comments
 (0)