Fix stale SHA conflict handling in updateContent and saveFile#162
Open
deferredreward wants to merge 1 commit intounfoldingWord:masterfrom
Open
Fix stale SHA conflict handling in updateContent and saveFile#162deferredreward wants to merge 1 commit intounfoldingWord:masterfrom
deferredreward wants to merge 1 commit intounfoldingWord:masterfrom
Conversation
updateContent() previously caught ALL PUT errors and assumed the branch didn't exist, falling through to a new_branch creation attempt. When the actual error was a 409 Conflict (stale SHA from another tab saving the same file), this created a cascade failure ending in a generic "Error creating file" message. Fix: updateContent now checks the HTTP status code before deciding what to do: - 409/422 (stale SHA): re-fetch the file to get the current SHA from the server, then retry the PUT once with the fresh SHA - 404 (branch missing): create new branch (existing behavior) - Other errors: propagate as-is saveFile() had the same over-broad catch pattern, falling through to createContent() on any updateContent error. Now it only tries createContent for 404 (file genuinely missing), since updateContent handles stale SHA retries internally. Verified with multi-tab reproduction: Tab A saves → Tab B saves (SHA changes) → Tab A saves again Before: "Error saving file! File could not be saved." After: 409 detected → fresh SHA fetched → PUT retried → save succeeds Closes unfoldingWord#161 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
Test ResultsMulti-tab stale SHA save — VERIFIED ✅Repro (tc-create-app):
Console evidence from verified test run: No error popup. No "Error creating file." No auth/login prompt. Not Yet Tested
|
Author
Remaining Test CasesThese scenarios test edge cases in the stale SHA handling and error routing. 1. updateContent with real 404 — branch genuinely missingSetup: Use an app that calls
2. updateContent with 500 server errorSetup: Mock or intercept the PUT endpoint to return 500
3. saveFile with 404 from updateContent — file doesn't exist on branchSetup: A scenario where the file exists on master but not on the user branch (e.g., first edit of a new file)
4. updateContent retry fails — stale SHA even after re-fetchSetup: This is a theoretical edge case where the file changes again between the
5. Concurrent rapid saves from same tabSetup: Open tc-create-app, edit a TSV file
|
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
Fixes multi-tab save failures caused by stale SHA conflicts being mishandled as missing branches.
Before: Any PUT error in
updateContent()was treated as "branch doesn't exist" → new_branch creation attempt → cascade failure → generic "Error creating file" shown to user.After:
updateContent()checks the HTTP status:saveFile()now only falls through tocreateContent()on 404 (file genuinely missing), sinceupdateContenthandles stale SHA retries internally.Root Cause
When two tabs edit the same file, Tab B's save changes the file's SHA on the server. Tab A still holds the old SHA. When Tab A tries to save:
updateContentcatch: assumes branch missing → retries PUT withnew_branch→ failssaveFilecatch: falls through tocreateContent()→ file already exists → "Error creating file."Files Changed
src/core/gitea-api/repos/contents/contents.ts—updateContent(): status-aware error handling with SHA re-fetch on 409/422src/components/file/helpers.js—saveFile(): only fall through tocreateContenton 404Verified
Tested against tc-create-app with exact multi-tab reproduction:
Console evidence:
No error popup. No "Error creating file." No auth/login prompt.
Closes #161
🤖 Generated with Claude Code