Repository navigation
EBADF thrown by stream if file is explicitly closed in v15 #35862
Description
Activity
I think the right way to handle the close it is to wait for the end of the stream:
stream.on('end', () => handle.close());
I confirm that the behavior of Node 15 is different from the previous versions, but I think that the behavior in this situation is indeed undefined (@ronag ?), so it is only normal that it changes across different versions.
As a funny side note, when you use
NODE_DEBUG=stream(it is probably timing dependent), the handle will get closed, than some housekeeping will immediately duplicate thestdintty which will get assigned the same file handle id as the one closed and then reading will continue from stdin (and it will sometimes block the process exit) 😃
It appears to be a buggy behavior, but in fact it is simply undefined behavior as there is no mechanism that could notify aReadStreamthat the simple number you passed as a file descriptor is no longer valid.There is feature request for solving this: #35240
Yea, this is in the realm of undefined behavior. We could avoid the undefined behavior if the read stream took the
FileHandleobject instead of thefddirectly.But I think this is actually the correct behavior once it's no longer undefined.
See #35240
On a second thought, reading from another file descriptor, even if the behavior is officially undefined, this is probably a little bit too much
- it is a silent error
- in a multi-context application it will corrupt not only the affected context, but also another, unrelated context
- if the other context is, let's say, a
require()for lazy-loading a feature, the whole application will crash
There is a (quite complex) solution possible by reusing the
trackUnamagedFdsmechanism- addedstreamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.
on Dec 18, 2020 I think this can be closed right? When I run the supplied code on Node.js 15.0.1 I do get an error, but when I run it on the latest Node.js 15.x (or any newer version), the script exists without any errors. So I assume it's fixed.
An unexpected EBADF error is thrown if a stream is open on a file and then the file is explicitly closed.
The explicit
closecall is required if usingfs/promises, otherwise a warning is emitted.What steps will reproduce the bug?
I've created a simple case that generates the error.
TESTFILE.txtexists and could be empty.How often does it reproduce? Is there a required condition?
Always
What is the expected behavior?
No error.
What do you see instead?
The following error is thrown:
If
autoCloseis omitted ortrue, the thrown error is:This last error is thrown also if
stream.destroy()is called beforehandle.close().The only way to avoid the error is to not call
handle.close, but this generates a deprecation warning in a more complex application (Closing a FileHandle object on garbage collection is deprecated.)Additional information
No error is emitted on previous node versions (10-14).