Repository navigation
fix(tus): remove the upload when its finalization fails - #1744
Open
Indian-boult wants to merge 4 commits into
Open
Indian-boult wants to merge 4 commits into
Indian-boult wants to merge 4 commits into
Conversation
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix. Fixes #1743.
What is the current behavior?
When
onUploadFinishthrows, the TUS store still reports the upload as complete. The next HEAD answers 200 withUpload-Offsetequal toUpload-Length, and tus-js-client callsonSuccessfor 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?
onUploadFinishchecksisCommittedVersionfor the upload's version, the same checkcompleteUploadmakes 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.removeFinishedUploaddeletes the object, its.infoand its.partkey with oneDeleteObjectscall and clears the cache. Upstreamremove()does not work here. It aborts the multipart upload first, and when S3 answersNoSuchUploadit throwsFILE_NOT_FOUNDbefore deleting any key. S3 answersNoSuchUploadfor every completed upload. I checked that against rustfs 1.0.0.FileStore.removeFinishedUploadcallsremove(), which deletes the file and its configstore entry, and ignoresFILE_NOT_FOUND.createTusServerpasses the datastore toonUploadFinishthe same way it already does foronIncomingRequest.completedflag in its configstore entry, andFileStore.writechecks 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.
Additional context
Scope
onUploadHeadis 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.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.isCommittedVersionlookup itself fails, the upload is kept, as incompleteUpload. A HEAD in that state still reports the upload as complete.@tus/servercallsonUploadFinishafter 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.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.tsgets aTUS finalization failureblock that runs on the S3 and file backends.ObjectAdminDelete.sendstubbed 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.src/http/routes/tus/lifecycle.test.tscovers 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.tschecks 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
src/test/tus.test.tshad 7 failed and 47 passed. The four upload cases failed withexpected undefined to be definedon the row lookup. On the file backend the replay case and both race cases got a 204 where a 400 was expected. Thelifecycle.test.tsandfile-store.test.tscases had 6 failed and 33 passed.src/test/tus.test.tspassed 54 of 54, and the finalization block passed 20 of 20 repeated runs.src/test/tus-timeout.test.tspassed 2 of 2. Theapp,error-connections,jwt-roleandrlsintegration suites passed 63 of 63.npm run test:unitpassed 4190 tests in 215 files.npm run lintandnpm run buildpassed..docker/docker-compose-infra.yml.