Skip to content

fix(cache): avoid 25P02 on concurrent narinfo insert - #1262

Merged
kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-narinfo-25p02
May 25, 2026
Merged

kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-narinfo-25p02

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

  • Replace the narinfo upsert OnConflictColumns(hash).Update(...)
    with OnConflictColumns(hash).Ignore().Exec() so that a
    concurrent insert of the same hash does not abort the transaction.
  • After the DO NOTHING insert, query the row by hash to obtain the
    ID 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 NOTHING here closes that
remaining 25P02 surface.

🤖 Generated with Claude Code

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label May 25, 2026
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Failed to post review comments

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved conflict handling in cache operations to ensure more reliable behavior during concurrent access scenarios. The system now more gracefully manages duplicate insert attempts and maintains transaction integrity under conflicting operations.

Walkthrough

The PR updates upsertNarInfoFromParsed in pkg/cache/cache.go: when a narinfo record is missing, it now runs INSERT ... ON CONFLICT (hash) DO NOTHING and then unconditionally re-SELECTs the row to obtain the ID, replacing the previous nb.Save(ctx) plus Go-side constraint-error retry.

Changes

Concurrent Insert Handling in upsertNarInfoFromParsed

Layer / File(s) Summary
ON CONFLICT insert with unconditional re-SELECT
pkg/cache/cache.go
The "record not found" branch replaces nb.Save(ctx) with ent.IsConstraintError-based retry by issuing INSERT ... ON CONFLICT (hash) DO NOTHING via OnConflictColumns(...).Ignore().Exec(ctx) and then always re-querying the narinfo row to obtain the resulting nir.ID.

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

  • kalbasit/ncps#1203: Both PRs touch the Ent-based NarInfo UPSERT flow in pkg/cache/cache.go.
  • kalbasit/ncps#1206: Also updates pkg/cache/cache.go’s upsertNarInfoFromParsed to handle concurrent insert conflicts via Ent.
  • kalbasit/ncps#1213: Modifies NarInfo upsert conflict handling and retry semantics in related code paths.

Suggested Labels

bug, go, size:S

Poem

A rabbit coded through the night,
Two inserts racing in the light.
ON CONFLICT whispers, "Do no harm,"
A gentle SELECT keeps things calm.
🐰🌙

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(cache): avoid 25P02 on concurrent narinfo insert' clearly and concisely summarizes the main change: preventing PostgreSQL error 25P02 on concurrent narinfo inserts by switching to DO NOTHING strategy.
Description check ✅ Passed The description provides clear context about the change: replacing OnConflictColumns with Ignore().Exec() to handle concurrent hash inserts, followed by a SELECT query to obtain the ID. It directly relates to the changeset and explains both the 'what' and 'why'.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added bug Something isn't working go Pull requests that update go code labels May 25, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/cache/cache.go Resolved
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-bulk-fetch-chunks branch from 78c71ae to a6560ed Compare May 25, 2026 00:25
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 12c52c1 to 8d79b2f Compare May 25, 2026 00:25

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3508c829-5c92-4f89-97db-b7978916ba9e

📥 Commits

Reviewing files that changed from the base of the PR and between 78c71ae and 12c52c1.

📒 Files selected for processing (1)
  • pkg/cache/cache.go

Comment thread pkg/cache/cache.go Resolved
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 8d79b2f to d30794f Compare May 25, 2026 01:00
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-bulk-fetch-chunks branch from d8d6ef9 to 742e736 Compare May 25, 2026 01:02
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from d30794f to 41c86ec Compare May 25, 2026 01:02
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-bulk-fetch-chunks branch from 742e736 to 02ddc93 Compare May 25, 2026 01:18
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 41c86ec to 4693c4c Compare May 25, 2026 01:18
Base automatically changed from user/wnasreddine/fix-bulk-fetch-chunks to main May 25, 2026 01:34
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 25, 2026
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]>
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 4693c4c to 452cd72 Compare May 25, 2026 01:35
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels May 25, 2026
@kalbasit
kalbasit enabled auto-merge (squash) May 25, 2026 01:35
@kalbasit
kalbasit merged commit ee91fa1 into main May 25, 2026
30 of 33 checks passed
@kalbasit
kalbasit deleted the user/wnasreddine/fix-narinfo-25p02 branch May 25, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code merge-stack size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant