From ede0f4ff774f8ceb0f5c0ba2650a7ce0bd39c118 Mon Sep 17 00:00:00 2001 From: Sean McCollum Date: Mon, 30 Dec 2024 10:13:29 -0800 Subject: [PATCH] fix: don't fail on closing fd after reset has been called (#550) (#1081) * fix: don't fail on closing fd after reset has been called (#550) * fix: only skip EBADF errors on async close and only when aborted * fix: remove abortControllers and just call closeFile asynchronously from close --- src/__tests__/volume.test.ts | 17 +++++++++++++++++ src/volume.ts | 4 +++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index 278e23e3..e70f624e 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -361,6 +361,23 @@ describe('volume', () => { '/good': 'bye', }); }); + it('Open streams should not be affected', async () => { + const vol = new Volume(); + const json = { + '/hello': 'world', + }; + vol.fromJSON(json); + await new Promise((resolve, reject) => { + vol + .createReadStream('/hello') + .on('data', () => null) + .on('close', resolve) + .on('end', () => { + vol.reset(); + }) + .on('error', reject); + }); + }); }); describe('.openSync(path, flags[, mode])', () => { const vol = new Volume(); diff --git a/src/volume.ts b/src/volume.ts index 0d941e9c..77f3067d 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -825,7 +825,9 @@ export class Volume implements FsCallbackApi, FsSynchronousApi { close(fd: number, callback: TCallback) { validateFd(fd); - this.wrapAsync(this.closeSync, [fd], callback); + const file = this.getFileByFdOrThrow(fd, 'close'); + // NOTE: not calling closeSync because we can reset in between close and closeSync + this.wrapAsync(this.closeFile, [file], callback); } private openFileOrGetById(id: TFileId, flagsNum: number, modeNum?: number): File {