Skip to content

Commit f0a165d

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 <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 0e82f72 commit f0a165d

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎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+
constcommon=require('../common');
4+
constrepl=require('node:repl');
5+
conststream=require('node:stream');
6+
constassert=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+
constclose$=Promise.withResolvers();
14+
consteval$=Promise.withResolvers();
15+
16+
constinput=newstream.PassThrough();
17+
constoutput=newstream.PassThrough();
18+
19+
constreplServer=repl.start({
20+
input,
21+
output,
22+
eval(_cmd,_context,_file,cb){
23+
close$.promise.then(()=>{
24+
cb(newError('Error returned from the eval callback'));
25+
eval$.resolve();
26+
});
27+
},
28+
});
29+
30+
letoutputStr='';
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+
awaiteval$.promise;
43+
44+
assert.match(outputStr,/UncaughtError:Errorreturnedfromtheevalcallback/);
45+
})().then(common.mustCall());

0 commit comments

Comments
 (0)