Skip to content

Commit ef2b5e5

Browse files
committed
Fix for issue mochajs#2713
* Eagerly set the process exitcode in case the write callback isn't executed before the process terminates. * Remove the extraneous call to `done()` at the bottom of the function. * Cleanup the `draining` reduction and value check in `done` into separate lines for easier reading.
1 parent a2fc76c commit ef2b5e5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

bin/_mocha

+9-4
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,19 @@ function exitLater (code) {
466466
}
467467

468468
function exit (code) {
469+
var clampedCode = Math.min(code, 255);
470+
471+
// Eagerly set the process's exit code in case stream.write doesn't
472+
// execute its callback before the process terminates.
473+
process.exitCode = clampedCode;
474+
469475
// flush output for Node.js Windows pipe bug
470476
// https://github.com/joyent/node/issues/6247 is just one bug example
471477
// https://github.com/visionmedia/mocha/issues/333 has a good discussion
472478
function done () {
473-
if (!(draining--)) {
474-
process.exit(Math.min(code, 255));
479+
draining--;
480+
if (draining <= 0) {
481+
process.exit(clampedCode);
475482
}
476483
}
477484

@@ -483,8 +490,6 @@ function exit (code) {
483490
draining += 1;
484491
stream.write('', done);
485492
});
486-
487-
done();
488493
}
489494

490495
process.on('SIGINT', function () {

0 commit comments

Comments
 (0)