Skip to content

Commit 5ff338f

Browse files
committed
debugger: fix event listener leak in the run command
It should remove both the error and the ready event listeners attached when either of them fires, instead of removing only the one whose corresponding event fires, otherwise the other event listener will always get leaked.
1 parent 3c8c1ef commit 5ff338f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/internal/debugger/inspect_client.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,20 @@ class Client extends EventEmitter {
344344
};
345345

346346
return new Promise((resolve, reject) => {
347-
this.once('error', reject);
348-
this.once('ready', resolve);
347+
const tearDown = () => {
348+
this.removeListener('ready', onReady);
349+
this.removeListener('error', onError);
350+
};
351+
const onError = (err) => {
352+
tearDown();
353+
reject(err);
354+
};
355+
const onReady = () => {
356+
tearDown();
357+
resolve();
358+
};
359+
this.once('error', onError);
360+
this.once('ready', onReady);
349361

350362
httpReq.on('upgrade', handshakeListener);
351363
httpReq.end();

test/common/debugger.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ function startCLI(args, flags = [], spawnOpts = {}, opts = { randomPort: true })
141141
return getOutput();
142142
},
143143

144+
get stderrOutput() {
145+
return stderrOutput;
146+
},
147+
144148
get rawOutput() {
145149
return outputBuffer.join('').toString();
146150
},

test/parallel/test-debugger-restart-message.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const startCLI = require('../common/debugger');
3131
} finally {
3232
await cli.quit();
3333
}
34+
35+
assert.doesNotMatch(cli.stderrOutput, /MaxListenersExceededWarning/);
3436
}
3537

3638
onWaitForInitialBreak();

0 commit comments

Comments
 (0)