Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
abhishekSavani committed Nov 27, 2025
commit 6d5c9d19d00a2d7011304bb1574964506f447ce6
25 changes: 19 additions & 6 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ function runTestFile(path, filesWatcher, opts) {
finished(child.stdout, { __proto__: null, signal: t.signal }),
]);

// Close readline interface to prevent memory leak
rl.close();

if (watchMode) {
filesWatcher.runningProcesses.delete(path);
filesWatcher.runningSubtests.delete(path);
Expand Down Expand Up @@ -506,7 +509,7 @@ function watchFiles(testFiles, opts) {
}

// Watch for changes in current filtered files
watcher.on('changed', ({ owners, eventType }) => {
const onChanged = ({ owners, eventType }) => {
if (!opts.hasFiles && (eventType === 'rename' || eventType === 'change')) {
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd);
const newFileName = ArrayPrototypeFind(updatedTestFiles, (x) => !ArrayPrototypeIncludes(testFiles, x));
Expand Down Expand Up @@ -547,19 +550,29 @@ function watchFiles(testFiles, opts) {
triggerUncaughtException(error, true /* fromPromise */);
}));
}
});
};

watcher.on('changed', onChanged);

// Cleanup function to remove event listener and prevent memory leak
const cleanup = () => {
watcher.removeListener('changed', onChanged);
opts.root.harness.watching = false;
opts.root.postRun();
};

if (opts.signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
opts.signal.addEventListener(
'abort',
() => {
opts.root.harness.watching = false;
opts.root.postRun();
},
cleanup,
{ __proto__: null, once: true, [kResistStopPropagation]: true },
);
}

// Expose cleanup method for proper resource management
filesWatcher.cleanup = cleanup;

return filesWatcher;
}

Expand Down