question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cleanup results in impossible to debug EBADF error if file descriptor is already closed

See original GitHub issue

Operating 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:closed
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
silkentrancecommented, Oct 31, 2020

@benjamingr thank you for your invaluable contribution, I really appreciated this!

0reactions
silkentrancecommented, Oct 31, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found