Skip to content

fix(tus): remove the upload when its finalization fails - #1744

Open
Indian-boult wants to merge 4 commits into
supabase:masterfrom
Indian-boult:fix/tus-finalize-failure
Open

Indian-boult wants to merge 4 commits into
supabase:masterfrom
Indian-boult:fix/tus-finalize-failure

Conversation

@Indian-boult

@Indian-boult Indian-boult commented Nov 22, 2026 •

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix. Fixes #1743.

What is the current behavior?

When onUploadFinish throws, the TUS store still reports the upload as complete. The next HEAD answers 200 with Upload-Offset equal to Upload-Length, and tus-js-client calls onSuccess for an object that was never stored. The issue has the traces for a failed backend HEAD and for a row write timeout.

What is the new behavior?

  • The catch in onUploadFinish checks isCommittedVersion for the upload's version, the same check completeUpload makes before it queues its own cleanup. When no row references the version, it removes the upload through the datastore right away. The next HEAD answers 404, and tus-js-client starts a new upload.
  • S3Store.removeFinishedUpload deletes the object, its .info and its .part key with one DeleteObjects call and clears the cache. Upstream remove() does not work here. It aborts the multipart upload first, and when S3 answers NoSuchUpload it throws FILE_NOT_FOUND before deleting any key. S3 answers NoSuchUpload for every completed upload. I checked that against rustfs 1.0.0.
  • FileStore.removeFinishedUpload calls remove(), which deletes the file and its configstore entry, and ignores FILE_NOT_FOUND.
  • createTusServer passes the datastore to onUploadFinish the same way it already does for onIncomingRequest.
  • A failed removal is logged, and the request still fails with the original error.
  • The file store rejects a write to an upload that an earlier write completed, with a 400. The write that completes an upload sets a completed flag in its configstore entry, and FileStore.write checks the flag under the upload lock. Before this, a replayed PATCH at the final offset ran the finish hook a second time, and that run could commit a row while a failing finalization of the same upload was removing the file. The S3 store already fails such a PATCH with a 500, because the multipart upload is gone by then.

With the fix, the repro from the issue gives this output.

{"trace":["POST 201","PATCH 500","HEAD 404","POST 201","PATCH 204 offset=37/-"],"outcome":"onSuccess","headObject":"object found"}
{"trace":["POST 201","PATCH 544","HEAD 404","POST 201","PATCH 204 offset=37/-"],"outcome":"onSuccess","headObject":"object found"}

Additional context

Scope

  • onUploadHead is unchanged. Answering 404 or 410 there whenever the row is missing would also hit an upload whose committed version was later replaced, and restarting that upload would write old bytes over newer data.
  • I did not use ObjectAdminDelete.invoke. On the file backend it deletes <key>.info, while the file store keeps its entry in <key>.json, so that entry would stay behind. The datastore already knows its own keys on both backends.
  • When the isCommittedVersion lookup itself fails, the upload is kept, as in completeUpload. A HEAD in that state still reports the upload as complete.
  • The removal runs after the upload lock is released, because @tus/server calls onUploadFinish after it unlocks. A second finalization of the same upload can't start in that window, because both stores now reject a write to a completed upload.
  • A replayed empty final PATCH on a committed file upload used to answer 204 through the replayed branch of completeUpload. It now gets a 400, as it already failed on S3. tus-js-client and Uppy never send one. The empty last PATCH of a deferred length upload and the write that the POST makes for a zero length upload still work, because the write that completes the upload is the one that sets the flag.

Tests

  • src/test/tus.test.ts gets a TUS finalization failure block that runs on the S3 and file backends.
    • Two cases make the backend HEAD fail once and the row write fail once, with ObjectAdminDelete.send stubbed so the queued cleanup has not run yet. They upload with tus-js-client and expect the row and the bytes to exist, a second upload URL, and the first upload to answer 404 with its keys or its file gone.
    • A replay case sends an empty PATCH at the final offset of a committed upload and expects it to be rejected without running the finish hook, with the object kept.
    • A race case holds the first finalization once its PATCH has completed the upload, holds a replayed finalization right before its commit, and then fails the first one. It runs with a declared length and with a deferred length that ends at zero bytes, and expects no row left without its bytes.
  • src/http/routes/tus/lifecycle.test.ts covers removing the upload, keeping it when a row references the version, and keeping the original error when the removal fails.
  • src/storage/protocols/tus/file-store.test.ts checks that a write after the completing one is rejected for a declared length, a deferred length, a declared length of zero and a deferred length of zero.

Results

  • With the master sources, src/test/tus.test.ts had 7 failed and 47 passed. The four upload cases failed with expected undefined to be defined on the row lookup. On the file backend the replay case and both race cases got a 204 where a 400 was expected. The lifecycle.test.ts and file-store.test.ts cases had 6 failed and 33 passed.
  • At ea50520, before the file store check, the race case on the file backend ended with a row whose file was gone.
  • With the fix, src/test/tus.test.ts passed 54 of 54, and the finalization block passed 20 of 20 repeated runs. src/test/tus-timeout.test.ts passed 2 of 2. The app, error-connections, jwt-role and rls integration suites passed 63 of 63.
  • npm run test:unit passed 4190 tests in 215 files. npm run lint and npm run build passed.
  • I ran on Node 26.7.0 with Postgres 15, pgbouncer and rustfs 1.0.0 from a local compose file based on .docker/docker-compose-infra.yml.

A failed finalization leaves the upload complete in the store, so the
next HEAD answers with Upload-Offset equal to Upload-Length and
tus-js-client reports success for an object that was never stored.

The new cases fail the backend HEAD once and the row write once, on the
S3 and file backends, and expect the client to finish a new upload and
the failed one to be gone. A file backend case replays the final PATCH
of a committed upload and checks that its object is kept.
When onUploadFinish throws and no row references the upload's version,
the hook now deletes the upload through the datastore. On S3 that is
the object with its .info and .part keys. On the file backend it is the
file and its configstore entry. The next HEAD then answers 404 and the
client starts a new upload instead of reporting success for an object
that was never stored.

The check is isCommittedVersion, the one completeUpload uses before it
queues its own cleanup, so a version that a row references is kept.
@Indian-boult
Indian-boult requested a review from a team as a code owner November 22, 2026 18:23
Comment thread src/http/routes/tus/lifecycle.ts Resolved
A PATCH at the final offset of a completed upload made the file store
write nothing and run the finish hook again. If the first finalization
failed while that second one was committing, the first one could remove
the file right after a row started to reference it, which left an
object without its bytes.

The file store now records the offset in the configstore entry once a
write completes the upload and rejects later writes with 400, so only
one finalization runs per upload. The S3 store already fails such a
PATCH, because the multipart upload is gone by then. The empty last
PATCH of a deferred length upload is still accepted, since it declares
the length in the same request.

The replay test now expects the rejection on both backends, and a new
test races a replayed final PATCH against a failing finalization.
248c223 marked completion by writing the final offset into the
configstore entry and skipped zero length uploads, because the entry
already holds offset 0 from creation. A deferred length upload that
ends at zero bytes was then never marked, so a replayed PATCH at
offset 0 still ran the finish hook again and could race a failing
finalization.

The entry now gets a completed flag, written only by the write that
completes the upload, and later writes are rejected once it is set.
The POST that creates a zero length upload still writes once and sets
the flag. The race test now also runs a deferred upload that ends at
zero bytes, and the store tests cover both zero length paths.

This branch has not been deployed

No deployments
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.

TUS upload reports success when finalization fails and the object is never stored

1 participant