Skip to content

Commit

Permalink
fs: aggregate errors in fsPromises to avoid error swallowing
Browse files Browse the repository at this point in the history
Add AggregateError support to fsPromises, instead of
swallowing errors if fs.close throws.
  • Loading branch information
Linkgoron committed Apr 23, 2021
1 parent 369f239 commit cd68f9c
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
PromisePrototypeFinally,
PromisePrototypeThen,
PromiseResolve,
PromiseReject,
SafeArrayIterator,
Symbol,
Uint8Array,
Expand All @@ -33,6 +34,7 @@ const {
ERR_METHOD_NOT_IMPLEMENTED,
},
AbortError,
aggregateTwoErrors,
} = require('internal/errors');
const { isArrayBufferView } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');
Expand Down Expand Up @@ -250,6 +252,19 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
}
}

async function handleFdClose(fileOpPromise, closeFunc) {
return PromisePrototypeThen(
fileOpPromise,
(result) => PromisePrototypeThen(closeFunc(), () => result),
(opError) =>
PromisePrototypeThen(
closeFunc(),
() => PromiseReject(opError),
(closeError) => PromiseReject(aggregateTwoErrors(closeError, opError))
)
);
}

async function fsCall(fn, handle, ...args) {
if (handle[kRefs] === undefined) {
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle);
Expand Down Expand Up @@ -498,7 +513,7 @@ async function rename(oldPath, newPath) {

async function truncate(path, len = 0) {
const fd = await open(path, 'r+');
return PromisePrototypeFinally(ftruncate(fd, len), fd.close);
return handleFdClose(ftruncate(fd, len), fd.close);
}

async function ftruncate(handle, len = 0) {
Expand Down Expand Up @@ -629,7 +644,7 @@ async function lchmod(path, mode) {
throw new ERR_METHOD_NOT_IMPLEMENTED('lchmod()');

const fd = await open(path, O_WRONLY | O_SYMLINK);
return PromisePrototypeFinally(fchmod(fd, mode), fd.close);
return handleFdClose(fchmod(fd, mode), fd.close);
}

async function lchown(path, uid, gid) {
Expand Down Expand Up @@ -708,7 +723,7 @@ async function writeFile(path, data, options) {
checkAborted(options.signal);

const fd = await open(path, flag, options.mode);
return PromisePrototypeFinally(
return handleFdClose(
writeFileHandle(fd, data, options.signal, options.encoding), fd.close);
}

Expand All @@ -733,7 +748,7 @@ async function readFile(path, options) {
checkAborted(options.signal);

const fd = await open(path, flag, 0o666);
return PromisePrototypeFinally(readFileHandle(fd, options), fd.close);
return handleFdClose(readFileHandle(fd, options), fd.close);
}

module.exports = {
Expand Down
72 changes: 72 additions & 0 deletions test/parallel/test-fs-promises-file-handle-aggregate-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
const tmpdir = require('../common/tmpdir');

// The following tests validate aggregate errors are thrown correctly
// when both an operation and close throw.

const fs = require('fs');
const path = require('path');
const {
readFile,
writeFile,
truncate,
lchmod,
} = fs.promises;
const {
FileHandle,
} = require('internal/fs/promises');

const assert = require('assert');
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd');

let count = 0;
async function createFile() {
const filePath = path.join(tmpdir.path, `aggregate_errors_${++count}.txt`);
await writeFile(filePath, 'content');
return filePath;
}

async function checkAggregateError(op) {
try {
const filePath = await createFile();
Object.defineProperty(FileHandle.prototype, 'fd', {
get: function() {
// Close is set by using a setter,
// so it needs to be set on the instance.
const originalClose = this.close;
this.close = async () => {
// close the file
await originalClose.call(this);
const closeError = new Error('CLOSE_ERROR');
closeError.code = 456;
throw closeError;
};
const opError = new Error('INTERNAL_ERROR');
opError.code = 123;
throw opError;
}
});

await op(filePath).catch(common.mustCall((err) => {
assert.strictEqual(err.constructor.name, 'AggregateError');
assert.strictEqual(err.code, 123);
assert.strictEqual(err.errors.length, 2);
assert.strictEqual(err.errors[0].message, 'INTERNAL_ERROR');
assert.strictEqual(err.errors[1].message, 'CLOSE_ERROR');
}));
} finally {
Object.defineProperty(FileHandle.prototype, 'fd', originalFd);
}
}
(async function() {
tmpdir.refresh();
await checkAggregateError((filePath) => truncate(filePath));
await checkAggregateError((filePath) => readFile(filePath));
await checkAggregateError((filePath) => writeFile(filePath, '123'));
if (common.isOSX) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());
68 changes: 68 additions & 0 deletions test/parallel/test-fs-promises-file-handle-close-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
const tmpdir = require('../common/tmpdir');

// The following tests validate aggregate errors are thrown correctly
// when both an operation and close throw.

const fs = require('fs');
const path = require('path');
const {
readFile,
writeFile,
truncate,
lchmod,
} = fs.promises;
const {
FileHandle,
} = require('internal/fs/promises');

const assert = require('assert');
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd');

let count = 0;
async function createFile() {
const filePath = path.join(tmpdir.path, `close_errors_${++count}.txt`);
await writeFile(filePath, 'content');
return filePath;
}

async function checkAggregateError(op) {
try {
const filePath = await createFile();
Object.defineProperty(FileHandle.prototype, 'fd', {
get: function() {
// Close is set by using a setter,
// so it needs to be set on the instance.
const originalClose = this.close;
this.close = async () => {
// close the file
await originalClose.call(this);
const closeError = new Error('CLOSE_ERROR');
closeError.code = 456;
throw closeError;
};
return originalFd.get.call(this);
}
});

await op(filePath).catch(common.mustCall((err) => {
assert.strictEqual(err.constructor.name, 'Error');
assert.strictEqual(err.message, 'CLOSE_ERROR');
assert.strictEqual(err.code, 456);
}));
} finally {
Object.defineProperty(FileHandle.prototype, 'fd', originalFd);
}
}
(async function() {
tmpdir.refresh();
await checkAggregateError((filePath) => truncate(filePath));
await checkAggregateError((filePath) => readFile(filePath));
await checkAggregateError((filePath) => writeFile(filePath, '123'));
if (common.isOSX) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());
66 changes: 66 additions & 0 deletions test/parallel/test-fs-promises-file-handle-op-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
const tmpdir = require('../common/tmpdir');

// The following tests validate aggregate errors are thrown correctly
// when both an operation and close throw.

const fs = require('fs');
const path = require('path');
const {
readFile,
writeFile,
truncate,
lchmod,
} = fs.promises;
const {
FileHandle,
} = require('internal/fs/promises');

const assert = require('assert');
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd');

let count = 0;
async function createFile() {
const filePath = path.join(tmpdir.path, `op_errors_${++count}.txt`);
await writeFile(filePath, 'content');
return filePath;
}

async function checkAggregateError(op) {
try {
const filePath = await createFile();
Object.defineProperty(FileHandle.prototype, 'fd', {
get: function() {
// Close is set by using a setter,
// so it needs to be set on the instance.
const originalClose = this.close;
this.close = common.mustCall(function(...args) {
return originalClose.apply(this, args);
});
const opError = new Error('INTERNAL_ERROR');
opError.code = 123;
throw opError;
}
});

await op(filePath).catch(common.mustCall((err) => {
assert.strictEqual(err.constructor.name, 'Error');
assert.strictEqual(err.message, 'INTERNAL_ERROR');
assert.strictEqual(err.code, 123);
}));
} finally {
Object.defineProperty(FileHandle.prototype, 'fd', originalFd);
}
}
(async function() {
tmpdir.refresh();
await checkAggregateError((filePath) => truncate(filePath));
await checkAggregateError((filePath) => readFile(filePath));
await checkAggregateError((filePath) => writeFile(filePath, '123'));
if (common.isOSX) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());

0 comments on commit cd68f9c

Please sign in to comment.