Skip to content

streaming breaks file handles #1246

@dmurvihill

Description

@dmurvihill

We get EBADF after streaming from a file handle:

import test from 'node:test';
import assert from 'node:assert';
import path from 'node:path';
import * as stream from 'node:stream';

import * as memfsModule from 'memfs';
import * as fs from 'fs/promises';
import { pipeline } from 'node:stream/promises';

const memfs = memfsModule.fs.promises;

await test(
  'fs allows close after streaming', () => testAutoClose(path.resolve(), fs)
);

await test(
  'memfs should allow close after streaming', () => testAutoClose('/', memfs as unknown as typeof fs)
);

async function testAutoClose(root: string, fsp: typeof fs) {
  const p = path.resolve(root, 'testFile');
  await fsp.writeFile(p, 'teststring');
  try {
    const fileHandle = await fsp.open(p, fs.constants.O_RDONLY);
    const s = fileHandle.createReadStream();
    await pipeline(s, new stream.Writable({ write: (c, e, cb) => { cb(); } }))
    await assert.doesNotReject(fileHandle.close());
  } finally {
    await fsp.rm(p);
  }
}
dolan@MacBookPro ops % node test/memfs.test.mts
✔ fs allows close after streaming (3.209875ms)
✖ memfs should allow close after streaming (2.080458ms)
ℹ tests 2
ℹ suites 0
ℹ pass 1
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 7.617667

✖ failing tests:

test at test/memfs.test.mts:35:7
✖ memfs should allow close after streaming (2.080458ms)
  AssertionError [ERR_ASSERTION]: Got unwanted rejection.
  Actual message: "EBADF: bad file descriptor, close"
      at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
      at async testAutoClose (file:///Users/dolan/IdeaProjects/papertrace/code/ops/test/memfs.test.mts:46:5)
      at async Test.run (node:internal/test_runner/test:1113:7)
      at async startSubtestAfterBootstrap (node:internal/test_runner/harness:358:3)
      at async file:///Users/dolan/IdeaProjects/papertrace/code/ops/test/memfs.test.mts:35:1 {
    generatedMessage: false,
    code: 'ERR_ASSERTION',
    actual: [Error],
    expected: undefined,
    operator: 'doesNotReject',
    diff: 'simple'
  }

Node 24.11.0
memfs 4.56.11
Mac OS Sequoia 15.7.3

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions