fix(workspaces): make upload name resolution and insert atomic#618
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix(workspaces): make upload name resolution and insert atomic#618posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
Concurrent uploads of the same filename to the same parent could fail with a SQLite UNIQUE constraint error on kernel_items.name. The workspace kernel is a Durable Object, so concurrent RPC calls interleave at `await` points. Because `resolveItemName` and the `INSERT` were separated by the awaited `writeFileBytes` / `r2.delete` calls, two simultaneous uploads could both resolve to the same collision-free name (neither inserted yet), then the second insert collided on the partial unique index — a check-then-act (TOCTOU) race. Move the awaited file/R2 work before name resolution so `resolveItemName` and the synchronous `INSERT` run in the same event-loop turn with no suspension between them. `shellPath` and metadata are derived from `itemId`/`requestedName`, not the resolved `name`, so reordering is safe. Generated-By: PostHog Code Task-Id: 888b423f-9eae-481d-b17d-49619571f76f
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 changed
WorkspaceKernelFileCommands.createFileFromUploadnow resolves the collision-free item name and runs theINSERT INTO kernel_itemsin the same synchronous event-loop turn. The awaitedwriteFileBytes/r2.deletecalls were moved to before name resolution, so nothing suspends betweenresolveItemNameand the insert.Why
Concurrent uploads of the same filename to the same parent could fail with a SQLite
UNIQUE constraint failed: kernel_items.nameerror. The workspace kernel is a Cloudflare Durable Object, so concurrent RPC calls interleave atawaitpoints. With the awaited file/R2 work sitting between name resolution and the insert, two simultaneous uploads could both resolve to the same name (neither row inserted yet), and the second insert then collided on the partial unique index — a classic check-then-act (TOCTOU) race. The user observed a 500UPLOAD_FAILED; the auto-rename logic is correct serially, which is why a retry succeeds.Reordering is safe because
shellPathand the metadata are derived fromitemId/requestedName, not the resolvedname.Testing notes
awaitremains betweenresolveItemNameand theINSERT(getNextSortOrderis synchronous), closing the interleaving window in the single-threaded DO.vite/client/nodetype-lib config errors are unrelated to this change).Created with PostHog Code from an inbox report.
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.