Skip to content

fix: clean up uploaded files after flush failure - #1505

Open
Kase390 wants to merge 2 commits into
expressjs:mainfrom
Kase390:fix/disk-storage-fsync-cleanup
Open

Kase390 wants to merge 2 commits into
expressjs:mainfrom
Kase390:fix/disk-storage-fsync-cleanup

Conversation

@Kase390

@Kase390 Kase390 commented Nov 3, 2026

Copy link
Copy Markdown

Summary

Fixes #1504.

When DiskStorage is configured with flush: true, failures during fs.open(), fs.fsync(), or fs.close() can leave uploaded files on disk.

This PR uses the existing _removeFile() method to clean up the stored file before propagating the original flush error.

It also avoids duplicate cleanup when a request abort has already initiated file removal.

Changes

  • Clean up stored files after flush failures.
  • Preserve the original flush error.
  • Avoid duplicate file removal during request aborts.
  • Add four regression tests covering fs.open(), fs.fsync(), fs.close(), and abort-related cleanup.

Testing

  • npm test — 175 passing, 2 pending
  • npm run lint — passed
  • git diff --check — passed

Tested locally on macOS with Node.js v24.22.1.

Known limitation

If file removal itself fails, the original flush error is preserved, but the cleanup error is not included in storageErrors. This change attempts file removal but cannot guarantee cleanup when the underlying filesystem rejects the operation.

@yxshee yxshee left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this out at 34cf949, which is based on current main (2c380d0, 2.5.0), on Node 26.7.0 / macOS.

  • npm test: 175 passing, 2 pending. npm run lint: clean.
  • With storage/disk.js reverted to main, the open, fsync and close tests fail: the file stays in the upload dir. The abort test passes on main, which is expected since it guards the new branch.
  • With the fix but without the if (!file.path) return cb(err) guard, the abort test fails with ERR_INVALID_ARG_TYPE from fs.unlink(undefined). So that guard is covered.

The guard is correct as far as I can tell. finishAbort calls _removeFile on the same file object the engine holds, and _removeFile deletes file.path synchronously. It then defers the unlink through flushingFiles until endFlush runs, which happens before done(err). So whichever side runs second sees no path and skips.

One small thing you could do in this PR: the comment at lib/make-middleware.js:396 ("On error the engine stored nothing and the file has no path") is now true for disk storage only because the engine cleans up after itself. A short note there would help the next reader.

@Kase390

Kase390 commented Nov 7, 2026

Copy link
Copy Markdown
Author

Thanks for the thorough review and for validating the regression tests and the file.path guard!

I've updated the comment in lib/make-middleware.js (ba5334d) to clarify that the storage engine is responsible for partial-file cleanup on error, and that DiskStorage clears file.path when cleanup is claimed even if unlink is still pending.

This is a comment-only change; no behavior was modified.

@NgoQuocViet2001

Copy link
Copy Markdown

Checked ba5334d on Windows x64 with Node.js 24.18.0, adding Windows coverage to the macOS results above.

The full suite passed (175 passing, 2 pending), and lint was clean. All 16 disk-storage tests passed, including the abort-during-flush case with exactly one unlink and an empty upload directory.

I also ran the PR's test/disk-storage.js against base 2c380d0. The three flush-error cases (open, fsync, close) failed because the file remained on disk; the abort case passed. With the PR implementation, all three failed-flush cases removed the file and preserved the original error.

@Kase390

Kase390 commented Nov 16, 2026

Copy link
Copy Markdown
Author

Thanks for taking the time to test this on Windows and compare the results against the base commit!

I really appreciate the additional platform coverage and the thorough verification of the cleanup behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiskStorage does not remove uploaded files after fsync failure

3 participants