Skip to content

Commit

Permalink
fix(vfs): race condition on temp file cleanup
Browse files Browse the repository at this point in the history
There was a race condition relating to streams in the
VFS pipeline that could lead to unhandled rejection
whenever a cleanup is performed on an endpoint with
pre-check errors (like readonly).
  • Loading branch information
andersevenrud committed Dec 28, 2023
1 parent c217913 commit ff816d2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const parseRangeHeader = (range, size) => {
const onDone = (req, res) => {
if (req.files) {
for (let fieldname in req.files) {
fs.unlink(req.files[fieldname].path, () => ({}));
try {
fs.removeSync(req.files[fieldname].path);
} catch (e) {
console.warn('Failed to unlink temporary file', e);
}
}
}
};
Expand All @@ -75,16 +79,21 @@ const onDone = (req, res) => {
const wrapper = fn => (req, res, next) => fn(req, res)
.then(result => {
if (result instanceof Stream) {
result.on('error', error => {
next(error);
});

result.on('end', () => {
onDone(req, res);
});

result.pipe(res);
} else {
res.json(result);
onDone(req, res);
}

onDone(req, res);
})
.catch(error => {
onDone(req, res);

next(error);
});

Expand Down

0 comments on commit ff816d2

Please sign in to comment.