From f2279f856ec7af755f871e66683fb0cc47f745d2 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 3d6dc93354b71c..a19f84b4f404bd 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -334,6 +334,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); @@ -1440,7 +1445,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)