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 29, 2023
1 parent c217913 commit 29882c7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 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,17 +79,23 @@ 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);
onDone(req, res);
});

/**
Expand Down

0 comments on commit 29882c7

Please sign in to comment.