Repository navigation
fix(cache): avoid 25P02 on concurrent narinfo insert - #1262
Conversation
|
Caution Review failedFailed to post review comments 📝 WalkthroughSummary by CodeRabbitRelease Notes
WalkthroughThe PR updates ChangesConcurrent Insert Handling in upsertNarInfoFromParsed
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly Related PRs
Suggested Labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
Code Review
This pull request modifies the upsertNarInfoFromParsed function in pkg/cache/cache.go to use an INSERT ... ON CONFLICT IGNORE approach, preventing transaction aborts during concurrent inserts. The reviewer suggested using a locking read (.ForUpdate()) for the subsequent fetch to ensure visibility across different database isolation levels, such as REPEATABLE READ, where a standard snapshot-based select might fail to see concurrently committed records.
78c71ae to
a6560ed
Compare
12c52c1 to
8d79b2f
Compare
|
This change is part of the following stack:
Change managed by git-spice. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/cache/cache.go`:
- Around line 4445-4453: In upsertNarInfoFromParsed: after calling
nb.OnConflictColumns(entnarinfo.FieldHash).Ignore().Exec(ctx), the subsequent
tx.NarInfo.Query().Where(entnarinfo.HashEQ(hash)).Only(ctx) can return NotFound
under MySQL REPEATABLE READ; change the post-Ignore fetch to treat NotFound as a
transient race by retrying the fetch (e.g., a small loop with a few attempts and
tiny backoff) or by performing a locking/current read in the same transaction,
and only return an error if all retries fail; update the code paths around
nb.OnConflictColumns(...).Ignore().Exec(ctx) and the following
tx.NarInfo.Query().Where(...).Only(ctx) to implement this retry/locking
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
8d79b2f to
d30794f
Compare
d8d6ef9 to
742e736
Compare
d30794f to
41c86ec
Compare
742e736 to
02ddc93
Compare
41c86ec to
4693c4c
Compare
upsertNarInfoFromParsed used `nb.Save()` (plain INSERT) when the initial SELECT found no row. If a concurrent goroutine inserted the same hash between that SELECT and the INSERT, PostgreSQL aborted the transaction (SQLSTATE 25P02). The subsequent re-fetch SELECT then also failed with "current transaction is aborted", propagating a 500. Replace `nb.Save()` with `OnConflictColumns(hash).Ignore().Exec()`, which maps to INSERT ... ON CONFLICT (hash) DO NOTHING. DO NOTHING leaves the transaction alive on conflict, so the always-following SELECT can retrieve the winner's row without 25P02. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
4693c4c to
452cd72
Compare
Summary
OnConflictColumns(hash).Update(...)with
OnConflictColumns(hash).Ignore().Exec()so that aconcurrent insert of the same hash does not abort the transaction.
DO NOTHINGinsert, query the row by hash to obtain theID for downstream operations.
Why
The same duplicate-key / aborted-transaction problem that affected
chunk inserts also affects narinfo inserts when two goroutines race
to cache the same hash. Applying
DO NOTHINGhere closes thatremaining 25P02 surface.
🤖 Generated with Claude Code