Skip to content

Commit 6d5c9d1

Browse files
test_runner: fix memory leaks in runner
- Close readline interface after child process exits Prevents accumulation of event listeners on stderr stream - Extract watch mode event handler to named function Allows proper cleanup when watch mode is aborted These changes prevent unbounded memory growth in long-running test suites and watch mode sessions.
1 parent 4451309 commit 6d5c9d1

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

lib/internal/test_runner/runner.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ function runTestFile(path, filesWatcher, opts) {
444444
finished(child.stdout, { __proto__: null, signal: t.signal }),
445445
]);
446446

447+
// Close readline interface to prevent memory leak
448+
rl.close();
449+
447450
if (watchMode) {
448451
filesWatcher.runningProcesses.delete(path);
449452
filesWatcher.runningSubtests.delete(path);
@@ -506,7 +509,7 @@ function watchFiles(testFiles, opts) {
506509
}
507510

508511
// Watch for changes in current filtered files
509-
watcher.on('changed', ({ owners, eventType }) => {
512+
const onChanged = ({ owners, eventType }) => {
510513
if (!opts.hasFiles && (eventType === 'rename' || eventType === 'change')) {
511514
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd);
512515
const newFileName = ArrayPrototypeFind(updatedTestFiles, (x) => !ArrayPrototypeIncludes(testFiles, x));
@@ -547,19 +550,29 @@ function watchFiles(testFiles, opts) {
547550
triggerUncaughtException(error, true /* fromPromise */);
548551
}));
549552
}
550-
});
553+
};
554+
555+
watcher.on('changed', onChanged);
556+
557+
// Cleanup function to remove event listener and prevent memory leak
558+
const cleanup = () => {
559+
watcher.removeListener('changed', onChanged);
560+
opts.root.harness.watching = false;
561+
opts.root.postRun();
562+
};
563+
551564
if (opts.signal) {
552565
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
553566
opts.signal.addEventListener(
554567
'abort',
555-
() => {
556-
opts.root.harness.watching = false;
557-
opts.root.postRun();
558-
},
568+
cleanup,
559569
{ __proto__: null, once: true, [kResistStopPropagation]: true },
560570
);
561571
}
562572

573+
// Expose cleanup method for proper resource management
574+
filesWatcher.cleanup = cleanup;
575+
563576
return filesWatcher;
564577
}
565578

0 commit comments

Comments
 (0)