Fix document creation race condition #221
Merged
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.
When a client attempts to create a new doc with a given name, we first check if the doc exists and if not we put an empty doc on S3. There is a potential race condition where if two requests check if a doc exists, they will both write an empty file. If the file has since changed, those changes will be erased.
I thought this was mostly theoretical, but I want to rule it out as it seems to be happening.
The fix is that, instead of attempting to create the empty file from the stateless worker, we delegate file creation to the durable object itself. This means instantiating the durable object earlier in the connection lifecycle, but has the advantage that the document will only be created if it does not already exist (since there is guaranteed to only be one instance of the durable object).