cleanup results in impossible to debug EBADF error if file descriptor is already closed
See original GitHub issueOperating System
- Linux
- Windows 7
- Windows 10
- MacOS
- other:
NodeJS Version
- 14.12
Tmp Version
0.2.1
Expected Behavior
safely handle cleanup for streams even if the file descriptor is already closed.
Experienced Behavior
<rejected> [Error: EBADF: bad file descriptor, close] {
errno: -9,
code: 'EBADF',
syscall: 'close'
}
import { createWriteStream } from 'fs';
import { pipeline } from 'stream';
import _tmp from 'tmp';
import { promisify } from 'util';
const ppipeline = promisify(pipeline);
function tmpFile(options = {}) {
options = Object.assign(options, { dir: 'blah' });
return new Promise((resolve, reject) => {
_tmp.file(options, (err, path, fd, cleanup) => {
if (err) {
reject(err);
} else {
resolve({ path, fd, cleanup });
}
});
});
}
async function tmpFileStream(options) {
const { path, fd, cleanup } = await tmpFile(options),
stream = createWriteStream(path, { fd });
return {
path,
stream,
// this causes unhanded promise exceptions EBADF: bad file descriptor
cleanup
};
}
async main() {
const streams = [],
temporary = await tmpFileStream({ postfix: `archive.${archiver.config.format}` });
streams.pipeline(/*useless empty stream that's paused*/, temporary.stream);
/*aborted useless stream - which cshould/does trigger all other streams to close (because of pipeline)*/
temporary.cleanup(); // generated a nasty error in its own stack that's untracable
}
main();
Issue Analytics
- State:
- Created 3 years ago
- Comments:13
Top Results From Across the Web
TestMultipleVMs: "bad file descriptor" cleaning up task bundle ...
Saw an intermittent failure when deleting a task in TestMultipleVMs here (see the failed "runtime isolated tests" ) ...
Read more >Handling the posix_spawn() file descriptor hell - Google Groups
closing a file descriptor which was not already opened just breaks your process (2) ... "If this error occurs after the calling process...
Read more >How to be sure if a file descriptor has already been closed?
The way I see it: EBADF means it has already been closed and ENOCONN means there is not connection behind this descriptor (not...
Read more >Dangers of closing file descriptors - unix - Server Fault
Writing to a closed file descriptor normally results in a simple error code from write() call. Many programs won't even check this error...
Read more >Getpeername Failed: Bad File Descriptor - ITALCOLLAUDI
The parent is still alive and doesn't close any file descriptor it should not. ... shall fail if: EBADF The socket argument is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@benjamingr thank you for your invaluable contribution, I really appreciated this!
@benjamingr I invited you to this in order for @rijnhard to realize that there is the
detachDescriptor
option, which seems to be lost on @rijnhard. Maybe we should work on the existing documentation to make it more clear.