Skip to content

fix(fs): a direct overwrite reaches the live object only after its row is written - #4852

Closed
HanadHassan wants to merge 5 commits into
HeyPuter:mainfrom
HanadHassan:HH/fs-overwrite-staged-key
Closed

HanadHassan wants to merge 5 commits into
HeyPuter:mainfrom
HanadHassan:HH/fs-overwrite-staged-key

Conversation

@HanadHassan

Copy link
Copy Markdown
Contributor

A direct overwrite (/fs/write, /fs/batchWrite, WebDAV PUT, legacy /up) uploaded its bytes straight onto the live object key. If the row write failed afterwards, because the parent folder moved mid-upload or the database dropped the connection, the request answered with an error while the previous content was already gone.

What changed

  • FSService#createUploadedEntries: a direct overwrite uploads to a key of its own. Each overwrite then writes its row under the live uuid and only then moves the upload onto the live key, one overwrite at a time. An overwrite whose row can't be recorded leaves its file, and every other file in the batch, whole.
  • FSService#landOverwrite: if the target file itself moved mid-upload, the write answers 409 Upload target has moved, as signed overwrites do, instead of writing onto the moved file.
  • Batches: new files are created only once every overwrite in the batch has landed, and cleanup deletes only the keys the batch uploaded.
  • New files, successful overwrites, the entry uuid (and every grant on it) and the events fired after a write behave as before.

What it doesn't fix

An entry's object key is its uuid, so the row and the bytes still change in two steps.

  • A failed copy after the row lands. The row reports the new size, and has lost its thumbnail, while the old bytes remain. This replaces the row-write failure, which was more common and could be triggered by a caller. It needs the object store to fail mid-copy.
  • Two overlapping saves of one file. These could already mismatch the row and the bytes. The window now lasts as long as the copy, not a few database round trips.
  • The root fix is an object key separate from the uuid, so an overwrite becomes one row switch. That's out of scope here.

Behavior to be aware of

  • A direct overwrite costs one object copy and one delete more than before, and records two storage write operations instead of one.
  • Overwrites in one batch write their rows one at a time (8 at once). For 40 files that measured 43 database reads instead of 4.
  • The reformatted updateThumbnails block comes from the pre-commit hook (eslint --fix with the locked prettier), not from this change.

bench-accept: overwrite an existing file -- a direct overwrite now stages its upload and copies it onto the live key after the row is written, so a failed save can't replace the previous version.

Testing

  • Ran the touched suites with vitest (FSService.test.ts, upload edges, write limits, FS/legacy/WebDAV write and shared-folder suites: 618 tests); lint, type check and benchmarks left to CI.
  • FSService.test.ts › "a direct overwrite that cannot land leaves every file whole": a failed row write, a folder moved mid-upload, a file moved mid-upload, a batch with one failed overwrite among others, and a batch mixing a failed overwrite with a new file each leave every file whole and no upload behind (fail without the fix with expected 26 to be 11, no 409, or a leftover new file).
  • Not exercised against MySQL or a real object store.

Covers a direct overwrite whose row write errors, one whose parent folder
moves away mid-upload, and the batch path, each asserting the previous
content and object survive. A control asserts that a successful overwrite
stages its upload at a key of its own and keeps the entry uuid.
…w is written

Direct overwrites (/fs/write, /fs/batchWrite, WebDAV PUT, legacy /up) now
upload to a key of their own, as signed overwrites already do. The step
that adopts the live entry writes the row first and moves the uploaded
object onto the live key only once that row lands. A write whose row
can't be recorded drops its upload and leaves the previous file as it
was. Batch cleanup removes uploads for overwrites too, never the live key.
- FSService: each overwrite in a write or batch lands its own row and then
  its own bytes, so one whose row can't be recorded leaves every other file
  whole. New files in the batch are created only once every overwrite has
  landed.
- FSService: the row uuid and the upload key travel separately, so an
  overwrite updates its row in the one store call it made before.
- FSService: an overwrite whose target file moved mid-upload answers 409
  "Upload target has moved", as signed overwrites do, instead of writing
  onto the moved file.
- FSService: batch cleanup deletes only the keys the batch uploaded.
- Tests assert outcomes (each file whole, no upload left behind) for a
  failed row write, a folder or file moved mid-upload, and batches mixing
  a failed overwrite with other overwrites and new files.
The tests cover overwrites whose row can't be recorded or whose target
moved; a copy that fails after the row lands is not covered, so the block
no longer claims a file is never left half-written.
@Salazareo

Copy link
Copy Markdown
Member

Holding off on merging this one: there's one bug to fix, and a few trade-offs to weigh.

Overwriting an empty (touched) file can delete it. A file created with touch (or by the fs-path grant) has no stored object yet: its bucket is null and its size is 0. #landOverwrite now writes the row first, which sets the bucket and the new size, and only then copies the staged upload onto the uuid key. A read between those two steps no longer sees an empty file:

  1. readContent gets NoSuchKey.
  2. #handleGhostFile confirms the object is missing with a HEAD and removes the entry.
  3. The write still returns success, and its copy lands as an orphan.

If the copy fails outright, the file is deleted on its next read. Before this PR the bytes landed on the live key first, so there was no gap.

Fix: when the existing entry has no stored object (hasNoBackingS3Object), there are no old bytes to protect. Copy, or upload straight to the live key, before writing the row. Please add a test that overwrites a touched file and reads it during the copy.

The row runs ahead of the bytes for the whole copy.

  • WebDAV builds the ETag (uuid + modified) and the Content-Length from the row. A GET during a same-size overwrite serves the old bytes under the new ETag, and an ETag-based sync client keeps them until the file changes again. If the size changes, the Content-Length is wrong. Copies over 5 GB copy their parts one after another, so this window can be long.
  • If the copy fails, the row keeps the new size while the old object stays. Storage usage is then off by the difference until the next write: a 1 GiB file overwritten with 1 MiB counts as 1 MiB used.
  • Two servers overwriting the same file can end with one writer's row and the other writer's bytes: A writes its row, B writes its row and copies, then A copies last. The PR mentions overlapping saves, but the copy makes that window much longer.

Cost. Every overwrite now does an extra copy plus an awaited delete of the staged key. Rows are written one at a time (8 in flight) instead of in one batch, and the user is billed two storage write ops instead of one. The staged-key delete could at least come off the request path.

Smaller things

  • A staged key is a random uuid that nothing tracks. If the process dies between the upload and the delete, the object stays behind: no quota counts it and nothing cleans it up. A recognisable key prefix with an expiry rule would cover this.
  • An overwrite that races a delete and recreate at the same path now gets 409 "Upload target has moved", which is misleading in that case.
  • When one overwrite in a batch fails, the others have already landed, but no fs.write.file events fire for them. Listeners, such as worker redeploys, miss those writes.
  • The updateThumbnails reformat isn't related to this change.

The PR already names the root fix: an object key separate from the uuid, so an overwrite becomes one row switch. That may be worth doing directly instead of this two-step version. If you keep this approach, the empty-file case has to be fixed first.

@HanadHassan

Copy link
Copy Markdown
Contributor Author

Thanks for holding this. I checked each point.

The touched-file deletion reproduces: a read during the copy 404s, the ghost handler removes the entry, and the write still answers 200. A failed copy leaves the row at the new size over the old bytes. The rest hold as you described. The Content-Length point is a bit worse than stated: a plain WebDAV GET also takes its length from the row, so a row out of step with the object truncates the download or leaves it hanging. The one nuance is the 409 on delete-and-recreate. The wording is wrong, but the refusal itself is what keeps the previous file intact (more below).

I also tested whether staging is worth it at all, and I think it is. On main, when a file is moved to Trash during an upload and a new file is created at its name, the overwrite lands on the trashed file's object; the raced branch then copies that object onto the new file and deletes it, so the trashed file is gone on its next read. The same ordering lets an overwrite replace a file that moved out from under it mid-upload. Each case comes down to the bytes reaching the live key before the checks that can still refuse the write.

So I'd rather do the root fix you pointed at than patch the two-step version. The shape I have in mind is a nullable object_key, readers first: every key site reads objectKey ?? uuid, and the ghost handler re-reads the primary before deleting so a stale cached key can't delete a healthy file. A second deploy switches overwrites to a fresh key and commits with one conditional update on id, path and the previous key, then deletes the replaced key after a grace period for in-flight reads and signed URLs. That folds in the ETag and two-writer points, and signed overwrites lose their extra copy. It touches fsentries, so it's your call.

If you'd rather keep the two-step approach, I'll apply your fixes here instead: copy before the row when there's no stored object, re-sync the row from the live object when the copy fails, take the staged-key delete off the request path, give staged keys a recognisable prefix with an expiry, emit the missing fs.write.file events, and fix the 409 message. I'll move this to draft in the meantime.

@HanadHassan
HanadHassan marked this pull request as draft December 3, 2026 05:37
@Salazareo

Copy link
Copy Markdown
Member

Closing in favour of #4879, which takes the same approach for direct overwrites and also covers the batch and signed-completion paths. Thanks for the PR.

@Salazareo Salazareo closed this Dec 4, 2026
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.

2 participants