Skip to content

Auto-closing streams throw EBADF errors when the volume is cleared in the meantime #550

@falkenhawk

Description

@falkenhawk

Hi,
I have a test which downloads a file with supertest and the file is served by setting the response body to read stream:

ctx.body = fs.createReadStream(filePath);

Then after the test finishes, I have a hook where I reset the memfs volume:

afterEach(() => {
  vol.reset();
});

It looks like when the stream is being destroyed, it asynchronously calls the method to close the file, but the volume is already reset at that point, and it throws the error:

  Error: EBADF: bad file descriptor, close
      at createError ((...)/node_modules/memfs/lib/volume.js:121:17)
      at Volume.getFileByFdOrThrow ((...)/node_modules/memfs/lib/volume.js:650:19)
      at Volume.closeSync ((...)/node_modules/memfs/lib/volume.js:878:25)
      at Immediate._onImmediate ((...)/node_modules/memfs/lib/volume.js:682:39)
      at processImmediate (internal/timers.js:456:21)

Can it be avoided somehow in this case, by adding a check somewhere along those lines if the volume was reset in the meantime? Or do you maybe have any suggestion for me to work around it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions