Make storage upload atomic - #325
Open
damilolaedwards wants to merge 1 commit into
Open
Conversation
Upload opened the destination directly with O_TRUNC and streamed the request body straight into it. The source is a live sandbox HTTP stream that can break mid-transfer, which is a normal occurrence, not an edge case, but a mid-copy failure had already truncated whatever was at that key. A re-upload to the same filename, which happens any time a script or session step reruns and writes the same output name again, could destroy a prior good file and replace it with a partial one while the caller was told the upload failed and nothing happened. Write to a temp file in the same directory instead and rename it over the destination only once the copy and close both succeed. Any failure along the way removes the temp file and leaves the existing key untouched.
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.
Summary
Upload opened the destination directly with O_TRUNC and streamed the
request body straight into it. The source is a live sandbox HTTP stream
that can break mid-transfer, which is a normal occurrence, not an edge
case, but a mid-copy failure had already truncated whatever was at that
key. A re-upload to the same filename, which happens any time a script or
session step reruns and writes the same output name again, could destroy a
prior good file and replace it with a partial one while the caller was
told the upload failed and nothing happened.
Writes to a temp file in the same directory instead and renames it over
the destination only once the copy and close both succeed. Any failure
along the way removes the temp file and leaves the existing key untouched.
Test plan
go test -race ./pkg/storage/...must survive) and a failed upload to a brand-new key (nothing should
be left behind); confirmed both fail on the old code
go build ./...,go vet ./...