Skip to content

Commit 39e50ac

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 43e2941 commit 39e50ac

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
@@ -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,14 +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

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

681685
async function appendFile(path, data, options) {
@@ -692,6 +696,10 @@ async function readFile(path, options) {
692696
if (path instanceof FileHandle)
693697
return readFileHandle(path, options);
694698

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

0 commit comments

Comments
 (0)