From c0cdb83433a64f6d0df9f094043a2745ce5f85dd 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. PR-URL: https://github.com/nodejs/node/pull/37402 Reviewed-By: Robert Nagy Reviewed-By: Darshan Sen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Antoine du Hamel --- lib/fs.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 4e48d08f07eaab..b85c10f97eff29 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); @@ -1453,7 +1458,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)