Skip to content

Commit 2b54300

Browse files
dario-piotrowiczaduh95
authored andcommitted
repl: fix eval errors thrown after close throwing ERR_USE_AFTER_CLOSE
prevent incorrect throws of `ERR_USE_AFTER_CLOSE` errors when the eval function of a repl server returns an error after the repl server has been closed PR-URL: #58791 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 6da713e commit 2b54300

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lib/repl.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,9 @@ function REPLServer(prompt,
741741
process.emit('uncaughtException', e);
742742
self.clearBufferedCommand();
743743
self.lines.level = [];
744-
self.displayPrompt();
744+
if (!self.closed) {
745+
self.displayPrompt();
746+
}
745747
});
746748
} else {
747749
if (errStack === '') {
@@ -771,7 +773,9 @@ function REPLServer(prompt,
771773
self.output.write(errStack);
772774
self.clearBufferedCommand();
773775
self.lines.level = [];
774-
self.displayPrompt();
776+
if (!self.closed) {
777+
self.displayPrompt();
778+
}
775779
}
776780
});
777781

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const repl = require('node:repl');
5+
const stream = require('node:stream');
6+
const assert = require('node:assert');
7+
8+
// This test checks that an eval function returning an error in its callback
9+
// after the repl server has been closed doesn't cause an ERR_USE_AFTER_CLOSE
10+
// error to be thrown (reference: https://github.com/nodejs/node/issues/58784)
11+
12+
(async () => {
13+
const close$ = Promise.withResolvers();
14+
const eval$ = Promise.withResolvers();
15+
16+
const input = new stream.PassThrough();
17+
const output = new stream.PassThrough();
18+
19+
const replServer = repl.start({
20+
input,
21+
output,
22+
eval(_cmd, _context, _file, cb) {
23+
close$.promise.then(() => {
24+
cb(new Error('Error returned from the eval callback'));
25+
eval$.resolve();
26+
});
27+
},
28+
});
29+
30+
let outputStr = '';
31+
output.on('data', (data) => {
32+
outputStr += data;
33+
});
34+
35+
input.write('\n');
36+
37+
replServer.close();
38+
close$.resolve();
39+
40+
process.on('uncaughtException', common.mustNotCall());
41+
42+
await eval$.promise;
43+
44+
assert.match(outputStr, /Uncaught Error: Error returned from the eval callback/);
45+
})().then(common.mustCall());

0 commit comments

Comments
 (0)