Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading