Skip to content

fs: allow FileHandle to outlive ReadableStream #45917

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ function close(stream, err, cb) {
}
}

function closeWeakRef(ref) {
const stream = ref.deref();
if (stream) {
stream.close();
}
}

function importFd(stream, options) {
if (typeof options.fd === 'number') {
// When fd is a raw descriptor, we must keep our fingers crossed
Expand All @@ -137,7 +144,7 @@ function importFd(stream, options) {
stream[kHandle] = options.fd;
stream[kFs] = FileHandleOperations(stream[kHandle]);
stream[kHandle][kRef]();
options.fd.on('close', FunctionPrototypeBind(stream.close, stream));
stream[kHandle].on('close', FunctionPrototypeBind(closeWeakRef, null, new WeakRef(stream)));
return options.fd.fd;
}

Expand Down
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`);
}));
}