From b2faef59052ae310a0fdcd1ad1a0b920e9372634 Mon Sep 17 00:00:00 2001 From: Nitzan Uziely Date: Tue, 16 Feb 2021 20:08:58 +0200 Subject: [PATCH] fs: fix writeFile signal does not close file Fix an issue where the writeFile does not close the file when the signal is aborted --- lib/fs.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 31aff87d4bec86..77393b93c6aa42 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -342,6 +342,11 @@ function readFile(path, options, callback) { return; } + if (options.signal?.aborted) { + callback(lazyDOMException('The operation was aborted', 'AbortError')); + return; + } + const flagsNumber = stringToFlags(options.flag); path = getValidatedPath(path); @@ -1452,7 +1457,13 @@ function lutimesSync(path, atime, mtime) { function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) { if (signal?.aborted) { - callback(lazyDOMException('The operation was aborted', 'AbortError')); + if (isUserFd) { + callback(lazyDOMException('The operation was aborted', 'AbortError')); + } else { + fs.close(fd, function() { + callback(lazyDOMException('The operation was aborted', 'AbortError')); + }); + } return; } // write(fd, buffer, offset, length, position, callback)