fix: EBADF when calling fileHandle.close() after streaming via pipeline#1249
Merged
fix: EBADF when calling fileHandle.close() after streaming via pipeline#1249
Conversation
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com> Agent-Logs-Url: https://github.com/streamich/memfs/sessions/6f11788f-01c1-4401-a7eb-0f830336480c
Copilot
AI
changed the title
[WIP] Fix EBADF error after streaming from file handle
fix: EBADF when calling fileHandle.close() after streaming via pipeline
Mar 21, 2026
There was a problem hiding this comment.
Pull request overview
Fixes a EBADF double-close scenario when a ReadStream/WriteStream is created from a FileHandle and consumed via pipeline(), ensuring the stream’s auto-close path updates the FileHandle lifecycle state correctly.
Changes:
- Update
FsReadStream/FsWriteStreamto retain the originatingFileHandle(when provided viaoptions.fd) and close viafileHandle.close()rather thanvol.close(fd). - Make
IFileHandle.createReadStream/createWriteStreamoptions parameters optional to align with Node.js semantics. - Add regression tests covering
pipeline()+ subsequent explicitfileHandle.close()for both read and write streams.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/fs-node/src/volume.ts | Routes stream closure through FileHandle.close() when constructed from a FileHandle, preventing descriptor state mismatch. |
| packages/fs-node/src/FileHandle.ts | Aligns createReadStream/createWriteStream signatures with optional options. |
| packages/fs-node-utils/src/types/misc.ts | Updates IFileHandle interface so stream creation options are optional. |
| packages/fs-node/src/tests/volume/FileHandle.test.ts | Adds regression tests verifying handle.close() resolves after streaming via pipeline(). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fileHandle.close()throwsEBADFafter consuming aReadStreamcreated from thatFileHandle, because the stream'sautoClosepath calledvol.close(numericFd)directly — bypassing theFileHandle's ref-counted lifecycle. TheFileHandlestill held itsfd, so its ownclose()attempted to close an already-closed descriptor.Changes
FsReadStream/FsWriteStream— store the originatingFileHandleasthis._fileHandlewhenoptions.fdis aFileHandleobject. Inclose(), route throughfileHandle.close()instead ofvol.close(numericFd). This marks theFileHandle's internalfdas-1, so any subsequent explicitfileHandle.close()call returnsPromise.resolve()immediately without error.IFileHandleinterface +FileHandleimplementation — madecreateReadStream/createWriteStreamoptions parameters optional (?), consistent with Node.js's own API and the already-optionalreadableWebStreamoptions.Example
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.