Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in parallelMerge #41

Open
dko-slapdash opened this issue Jan 14, 2020 · 0 comments
Open

Memory leak in parallelMerge #41

dko-slapdash opened this issue Jan 14, 2020 · 0 comments

Comments

@dko-slapdash
Copy link

dko-slapdash commented Jan 14, 2020

Hi.

parallelMerge() uses Promise.race(), and this call has a memory leak: if the same Promise is passed to race() multiple times, its .then() handler is also attached multiple times.

The details are here: nodejs/node#17469 (it's a Node's wontfix issue).

The easiest way to reproduce is to run the following:

const fileStream = fs.createReadStream("...", { highWaterMark: 1 });
const idleStream = new Transform();
let n = 0;
for await (const _ of parallelMerge(fileStream, idleStream)) {
  n++;
  if (n % 100000 === 0) console.log(n);
}
await delay(100000);
idleStream.destroy();

Internally, for every single byte in fileStream, a Promise.race() is called for promises from fileStream's and idleStream's AsyncIterator, and since idleStream does not emit anything, its promise is reused over and over, and then() handlers are stacked on each other. So if we run the profiler and compare memory footprints before the cycle and right before idleStream.destroy() call, we'll see thousands/millions of dangling promises.

I don't quite have a better alternative approach for parallelMerge() to propose... what's more or less clear is that it's not a proper thing to use Promise.race() to merge streams/iterables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant