dropbox: detach empty-upload cleanup from the request context - #28
Merged
Conversation
When a chunked (unknown-length) request carries an empty body, the handler finalizes a 0-byte object and then deletes it before returning 'body empty'. That delete used r.Context(), which is canceled the moment the client disconnects — and a client that sends an empty body and drops the connection is exactly the case that triggers this path. A canceled-context delete fails, stranding a rowless 0-byte object that the DB-driven GC can never reclaim. Use the existing deleteObject helper, which detaches via context.WithoutCancel + a 10s timeout — the same pattern already used by the mid-stream io.Copy error path and the signed-upload rejection paths. No behavior change for the success path. go build / vet clean; existing TestUpload_EmptyChunkedBody (and the rest of the upload suite) still green.
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
In
uploadHandler, when a chunked (unknown-length) request carries an empty body, the handler finalizes a 0-byte object and then deletes it before returningbody empty. That delete used the request context:r.Context()is canceled the instant the client disconnects — and a client that streams an empty body and drops the connection is exactly what reaches this branch. A canceled-context delete fails, stranding a rowless 0-byte object that the DB-driven GC can never reclaim (it only deletes objects that have an expired DB row).The mid-stream
io.Copyerror path right above (handler.go:119) and the signed-upload rejection paths already guard against this by deleting on a detached context. This applies the same fix to the empty-upload path via the existing helper:Why
It's a (narrow) resource leak and an inconsistency: every other "delete a just-written object" site in this service already detaches from the request context for precisely this reason; the empty-upload site was the one that didn't.
Verification
go build ./...andgo vet ./...clean;gofmtclean.TestUpload_EmptyChunkedBody(asserts no bucket object and no DB row after an empty chunked upload) and the fullTestUpload*suite stay green.delete rejected upload, shared with the other rejection paths).Note
The exact client-disconnect race isn't unit-reproducible with
httptest(the test context isn't canceled mid-handler), so this reuses the already-testeddeleteObjecthelper rather than adding a contrived test. Theio.Copyerror path at line 119 still inlines the equivalent detached-cleanup logic with a more specific log message; folding it onto the helper too is a reasonable follow-up, left out here to keep this a focused fix.