From 6e6229e657efd9fa3c5ce7b4ec6ffe8f48a26bbd 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 2bb629d58af081..69d86a11b0463f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -338,6 +338,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); @@ -1448,7 +1453,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)