Skip to content

Commit 9778ea3

Browse files
Nitzan UzielyLinkgoron
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.
1 parent bb35b6e commit 9778ea3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/internal/fs/promises.js

+11
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const {
6666
const { opendir } = require('internal/fs/dir');
6767
const {
6868
parseFileMode,
69+
validateAbortSignal,
6970
validateBoolean,
7071
validateBuffer,
7172
validateInteger,
@@ -668,11 +669,17 @@ async function writeFile(path, data, options) {
668669
data = Buffer.from(data, options.encoding || 'utf8');
669670
}
670671

672+
validateAbortSignal(options.signal);
671673
if (path instanceof FileHandle)
672674
return writeFileHandle(path, data, options.signal);
673675

676+
if (options.signal?.aborted) {
677+
throw lazyDOMException('The operation was aborted', 'AbortError');
678+
}
679+
674680
const fd = await open(path, flag, options.mode);
675681
if (options.signal?.aborted) {
682+
await fd.close();
676683
throw lazyDOMException('The operation was aborted', 'AbortError');
677684
}
678685
return PromisePrototypeFinally(writeFileHandle(fd, data), fd.close);
@@ -692,6 +699,10 @@ async function readFile(path, options) {
692699
if (path instanceof FileHandle)
693700
return readFileHandle(path, options);
694701

702+
if (options.signal?.aborted) {
703+
throw lazyDOMException('The operation was aborted', 'AbortError');
704+
}
705+
695706
const fd = await open(path, flag, 0o666);
696707
return PromisePrototypeFinally(readFileHandle(fd, options), fd.close);
697708
}

0 commit comments

Comments
 (0)