Skip to content

Commit 936160f

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 04fb597 commit 936160f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/internal/fs/promises.js

+8-1
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,13 +669,15 @@ 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

674-
const fd = await open(path, flag, options.mode);
675676
if (options.signal?.aborted) {
676677
throw lazyDOMException('The operation was aborted', 'AbortError');
677678
}
679+
680+
const fd = await open(path, flag, options.mode);
678681
return PromisePrototypeFinally(writeFileHandle(fd, data), fd.close);
679682
}
680683

@@ -692,6 +695,10 @@ async function readFile(path, options) {
692695
if (path instanceof FileHandle)
693696
return readFileHandle(path, options);
694697

698+
if (options.signal?.aborted) {
699+
throw lazyDOMException('The operation was aborted', 'AbortError');
700+
}
701+
695702
const fd = await open(path, flag, 0o666);
696703
return PromisePrototypeFinally(readFileHandle(fd, options), fd.close);
697704
}

0 commit comments

Comments
 (0)