Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
test: ensure ReadableStream is not leaked by FileHandle
  • Loading branch information
Leonardo Ivo Julca authored and Slayer95 committed Dec 20, 2022
commit 51623c430ec5d0eee5ddcc3b15da4c557d5440b4
47 changes: 47 additions & 0 deletions test/fixtures/stream-readable-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const { mustCall, mustNotCall } = require('../common');
const { open } = require('node:fs/promises');
const { setImmediate } = require('node:timers/promises');

function getHugeVector() {
return new Array(1e5).fill(0).map((e, i) => i);
}

const registry = new FinalizationRegistry(() => {
process.exitCode = 0;
process.send(`success`);
});

async function run() {
process.exitCode = 1;

// Long-lived file handle. Ensure we keep a reference to it.
const fh = await open(__filename)
fh.on('close', mustNotCall())

const stream = fh.createReadStream({ autoClose: false })
// Keeping the file handle open shouldn't prevent the stream from being GCed.
registry.register(stream, `[GC] Collected readable ${fh.fd}`);

for await (const chunk of stream.iterator({ destroyOnReturn: false })) {
break
}

// Keep a reference to the file handle
return { fh };
}

async function forceGC() {
gc();
const hugeMatrix = [];
for (let i = 0; i < 0x40; i++) {
hugeMatrix.push(getHugeVector());
gc();
await setImmediate();
}
}

run().then(async function (result) {
await forceGC();
});
21 changes: 21 additions & 0 deletions test/parallel/test-stream-readable-fh-hold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const { mustCall, mustNotCall } = require('../common');
const { strictEqual, notStrictEqual } = require('assert');
const fixtures = require('../common/fixtures');
const { fork } = require('child_process');

{
const cp = fork(fixtures.path('stream-readable-leak.js'), {
execArgv: ['--expose-gc', '--max-old-space-size=16', '--max-semi-space-size=2'],
silent: true,
});
cp.on('exit', mustCall((code, killSignal) => {
notStrictEqual(code, 1);
strictEqual(killSignal, null);
}));
cp.on('error', mustNotCall());
cp.on('message', mustCall(message => {
strictEqual(message, `success`);
}));
}