Skip to content

Commit 8fb035a

Browse files
Nitzan Uzielytargos
Nitzan Uziely
authored andcommitted
fs: fix pre-aborted writeFile AbortSignal file leak
Fix an issue in writeFile where a file is opened, and not closed if the abort signal is aborted after the file was opened but before writing began. PR-URL: nodejs#37393 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 70f1f8d commit 8fb035a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/internal/fs/promises.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const {
6363
const { opendir } = require('internal/fs/dir');
6464
const {
6565
parseFileMode,
66+
validateAbortSignal,
6667
validateBuffer,
6768
validateInteger,
6869
validateUint32
@@ -646,14 +647,17 @@ async function writeFile(path, data, options) {
646647
data = Buffer.from(data, options.encoding || 'utf8');
647648
}
648649

650+
validateAbortSignal(options.signal);
649651
if (path instanceof FileHandle)
650652
return writeFileHandle(path, data, options.signal);
651653

652-
const fd = await open(path, flag, options.mode);
653654
if (options.signal?.aborted) {
654655
throw new lazyDOMException('The operation was aborted', 'AbortError');
655656
}
656-
return PromisePrototypeFinally(writeFileHandle(fd, data), fd.close);
657+
658+
const fd = await open(path, flag, options.mode);
659+
const { signal } = options;
660+
return PromisePrototypeFinally(writeFileHandle(fd, data, signal), fd.close);
657661
}
658662

659663
async function appendFile(path, data, options) {
@@ -670,6 +674,10 @@ async function readFile(path, options) {
670674
if (path instanceof FileHandle)
671675
return readFileHandle(path, options);
672676

677+
if (options.signal?.aborted) {
678+
throw lazyDOMException('The operation was aborted', 'AbortError');
679+
}
680+
673681
const fd = await open(path, flag, 0o666);
674682
return PromisePrototypeFinally(readFileHandle(fd, options), fd.close);
675683
}

0 commit comments

Comments
 (0)