Skip to content

fix(cache): fill stub narinfo from concurrent race - #1263

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

kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-stub-narinfo-race

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

  • After the DO NOTHING + re-fetch pattern, detect whether the
    fetched row is a stub (URL is NULL or empty) written by a
    concurrent goroutine mid-flight.
  • If so, issue an UpdateOneID to fill in the caller's narinfo data
    before returning the row.

Why

DO NOTHING returns the pre-existing row as-is. If another goroutine
inserted a stub row (URL=NULL) just before us and hasn't filled it
yet, callers would operate on an incomplete narinfo record. Detecting
and updating the stub ensures the row is always fully populated after
upsertNarInfoFromParsed returns.

🤖 Generated with Claude Code

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. go Pull requests that update go code labels May 25, 2026
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a race condition where concurrently created placeholder records could be returned without their final data; the system now detects these stubs, applies the missing updates, and returns fully populated records.

Walkthrough

In upsertNarInfoFromParsed, after a re-fetch following an insert conflict, the code detects a raced stub narinfo row with an empty URL and issues an UpdateOneID using applyNarInfoUpdate to populate parsed narInfo fields before returning.

Changes

Race condition handling in upsertNarInfoFromParsed

Layer / File(s) Summary
Concurrent insert stub completion
pkg/cache/cache.go
After re-fetching a narinfo row from a concurrent insert, the function checks if URL is nil/empty and performs an UpdateOneID to apply the parsed narInfo fields to complete the stub before returning.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • kalbasit/ncps#1262: Modifies pkg/cache/cache.go insert/re-SELECT flow for concurrent narinfo inserts; closely related to the insert/re-fetch logic adjusted here.
  • kalbasit/ncps#1216: Adds post-check logic using nir.URL during migration, related to detecting completed vs stub narinfo rows.
  • kalbasit/ncps#1256: Addresses a related race by delaying signaling until storeInDatabase completes, reducing occurrence of stub rows that this PR now repairs.

Suggested labels

bug

Poem

🐰 I found a stub with URL gone,
I nudged and stitched it back at dawn,
With hops and code I filled the gap,
Now narinfo sings — a tidy map! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: filling stub narinfo data that results from concurrent race conditions in the cache.
Description check ✅ Passed The description clearly explains the problem (concurrent goroutines creating incomplete stub rows) and the solution (detecting and filling stubs before returning), which aligns with the changeset.
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.

@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 updates pkg/cache/cache.go to handle race conditions during NarInfo upserts by ensuring that stub records with missing URLs are populated before being returned. I have no feedback to provide as there were no review comments to evaluate.

@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 12c52c1 to 8d79b2f Compare May 25, 2026 00:25
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-stub-narinfo-race branch from 59a27db to 4beaa0f Compare May 25, 2026 00:25
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-stub-narinfo-race branch from 4beaa0f to b2314a2 Compare May 25, 2026 01:00
@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-narinfo-25p02 branch from d30794f to 41c86ec Compare May 25, 2026 01:02
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-stub-narinfo-race branch from b2314a2 to c61ff43 Compare May 25, 2026 01:03
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 41c86ec to 4693c4c Compare May 25, 2026 01:18
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-stub-narinfo-race branch from c61ff43 to b43c865 Compare May 25, 2026 01:18
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-narinfo-25p02 branch from 4693c4c to 452cd72 Compare May 25, 2026 01:35
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-stub-narinfo-race branch from b43c865 to 509b380 Compare May 25, 2026 01:35
Base automatically changed from user/wnasreddine/fix-narinfo-25p02 to main May 25, 2026 02:10
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels May 25, 2026
When upsertNarInfoFromParsed issues DO NOTHING and a concurrent
writer already inserted a stub (URL=NULL), the re-fetch returns
that stub unchanged, leaving storeInDatabase to link an
incomplete narinfo row with no URL.

After re-fetching, check if the row's URL is nil/empty and
update it with the caller's data — same logic already used in
the existing.URL==nil branch for a pre-existing stub.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@kalbasit
kalbasit force-pushed the user/wnasreddine/fix-stub-narinfo-race branch from 509b380 to 1a97db0 Compare May 25, 2026 02:10
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 25, 2026
@kalbasit
kalbasit enabled auto-merge (squash) May 25, 2026 02:11

@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.

🧹 Nitpick comments (1)
pkg/cache/cache.go (1)

4463-4472: ⚡ Quick win

Add a concurrent regression test for the stub-repair branch.

This path fixes a race that is hard to validate by inspection alone. A focused test that forces the DO NOTHING + re-fetch flow onto a stub row would make regressions much easier to catch, and it should run under go test -race ./....

As per coding guidelines, **/*.go: Use go test -race ./... with race detector enabled for all tests.

🤖 Prompt for 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.

In `@pkg/cache/cache.go` around lines 4463 - 4472, Add a concurrent regression
test that reproduces the race-path where a concurrent writer inserts a stub
(URL=NULL) and the reader hits the DO NOTHING + re-fetch repair branch so we
exercise the block that checks nir.URL and calls tx.NarInfo.UpdateOneID(nir.ID),
applyNarInfoUpdate(...) and ub.Save(...). The test should: create a stub narinfo
row, run two goroutines (one doing the insert/DO NOTHING flow and one doing the
re-fetch/repair flow) synchronized to force the race, assert that after repair
the narinfo row has a non-empty URL, and be executed under the race detector (go
test -race ./...). Put the test near other cache tests and name it to clearly
indicate it validates the stub-repair concurrent path.
🤖 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.

Nitpick comments:
In `@pkg/cache/cache.go`:
- Around line 4463-4472: Add a concurrent regression test that reproduces the
race-path where a concurrent writer inserts a stub (URL=NULL) and the reader
hits the DO NOTHING + re-fetch repair branch so we exercise the block that
checks nir.URL and calls tx.NarInfo.UpdateOneID(nir.ID), applyNarInfoUpdate(...)
and ub.Save(...). The test should: create a stub narinfo row, run two goroutines
(one doing the insert/DO NOTHING flow and one doing the re-fetch/repair flow)
synchronized to force the race, assert that after repair the narinfo row has a
non-empty URL, and be executed under the race detector (go test -race ./...).
Put the test near other cache tests and name it to clearly indicate it validates
the stub-repair concurrent path.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a9f1c4f8-9002-4a8c-bd9c-62a3c43f3eec

📥 Commits

Reviewing files that changed from the base of the PR and between 509b380 and 1a97db0.

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

@kalbasit
kalbasit merged commit 1f645f9 into main May 25, 2026
29 of 33 checks passed
@kalbasit
kalbasit deleted the user/wnasreddine/fix-stub-narinfo-race branch May 25, 2026 02:37
kalbasit added a commit that referenced this pull request Jun 9, 2026
#1375)

The coordination poll loop called hasFinishedAsset then hasAsset on every
200ms tick, each repeating HasNarInStore (a storage stat / S3 HEAD) and
the nar_file lookup — redundant I/O and DB load under contention.

Extract one oracle, narServability(ctx, narURL) (servable, finished, err),
and have isServable and hasFinishedNar both delegate to it (preserving the
single-source-of-truth that prevents the divergent-check regressions
#1255/#1263/#1279/#1290). Collapse coordinateDownload /
pollForDownloadOrTakeOver from two predicate closures to one
checkAsset(ctx) (servable, finished); the tick now computes both states in
a single call. Behavior is unchanged (full -race suite green).

Resolves threads PRRT_kwDONV6tZs6H_WfI, _WfM, _WfP, _WfR, _WfW, _Wfa,
_Wfc, _Wfn, _Wfo.
kalbasit added a commit that referenced this pull request Jun 9, 2026
#1375)

The coordination poll loop called hasFinishedAsset then hasAsset on every
200ms tick, each repeating HasNarInStore (a storage stat / S3 HEAD) and
the nar_file lookup — redundant I/O and DB load under contention.

Extract one oracle, narServability(ctx, narURL) (servable, finished, err),
and have isServable and hasFinishedNar both delegate to it (preserving the
single-source-of-truth that prevents the divergent-check regressions
#1255/#1263/#1279/#1290). Collapse coordinateDownload /
pollForDownloadOrTakeOver from two predicate closures to one
checkAsset(ctx) (servable, finished); the tick now computes both states in
a single call. Behavior is unchanged (full -race suite green).

Resolves threads PRRT_kwDONV6tZs6H_WfI, _WfM, _WfP, _WfR, _WfW, _Wfa,
_Wfc, _Wfn, _Wfo.
kalbasit added a commit that referenced this pull request Jun 9, 2026
#1375)

The coordination poll loop called hasFinishedAsset then hasAsset on every
200ms tick, each repeating HasNarInStore (a storage stat / S3 HEAD) and
the nar_file lookup — redundant I/O and DB load under contention.

Extract one oracle, narServability(ctx, narURL) (servable, finished, err),
and have isServable and hasFinishedNar both delegate to it (preserving the
single-source-of-truth that prevents the divergent-check regressions
#1255/#1263/#1279/#1290). Collapse coordinateDownload /
pollForDownloadOrTakeOver from two predicate closures to one
checkAsset(ctx) (servable, finished); the tick now computes both states in
a single call. Behavior is unchanged (full -race suite green).

Resolves threads PRRT_kwDONV6tZs6H_WfI, _WfM, _WfP, _WfR, _WfW, _Wfa,
_Wfc, _Wfn, _Wfo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code merge-stack size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant