fix: handle potential DB conflict due to concurrent upload requests in postFile#19005
Merged
Conversation
Signed-off-by: Callum Styan <callumstyan@gmail.com>
Signed-off-by: Callum Styan <callumstyan@gmail.com>
Signed-off-by: Callum Styan <callumstyan@gmail.com>
brettkolodny
approved these changes
Jul 24, 2025
Contributor
brettkolodny
left a comment
There was a problem hiding this comment.
One thought abut logging but otherwise lgtm 👍
Comment on lines
+121
to
+127
| if database.IsUniqueViolation(err, database.UniqueFilesHashCreatedByKey) { | ||
| // The file was uploaded by some concurrent process since the last time we checked for it, fetch it again. | ||
| file, err = api.Database.GetFileByHashAndCreator(ctx, database.GetFileByHashAndCreatorParams{ | ||
| Hash: hash, | ||
| CreatedBy: apiKey.UserID, | ||
| }) | ||
| } |
Contributor
There was a problem hiding this comment.
Even though we suspect that this won't happen often enough to be an issue, would it be worth adding a log here so that we can verify how often this happens later, or would that be needless clutter to the logs?
Contributor
Author
There was a problem hiding this comment.
Absolutely, added one 👍
Signed-off-by: Callum Styan <callumstyan@gmail.com>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
This issue manifests when users have multiple templates which rely on the same files, for example see: #17442
In the
filestable we have a constraint to enforce that there can only be one entry perhash, created_bycombo. When runningterraform applyto update templates, this can mean that the file upload can fail for some of the templates as they hit a race condition all trying to insert at the same time.The fix here is to detect presence of the conflict error and run another
GetFileByHashAndCreator. This should happen infrequently enough to not cause significant extra load on the DB. If in the future we notice that it does, we could change the underlying SQL for file insertion to run a CAS like call via theON CONFLICTsyntax.Note that the test added here will fail without the change from the first commit. I also tested this manually via
deploy.shin my own workspace.Apply diff example without code changes
Error from apply
Running the apply again succeeds
Apply diff example with code changes
apply works on the first try
The template files are as follows:
Top level main.tf
template/main.tf
template/README.md
To test, simply modify the template files (adding a comment in each is all you need, plus updating the version #) and then
terraform applywith the--parallelismflag.