Repository navigation
fix(cache): fill stub narinfo from concurrent race - #1263
Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughIn 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. ChangesRace condition 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 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.
12c52c1 to
8d79b2f
Compare
59a27db to
4beaa0f
Compare
|
This change is part of the following stack:
Change managed by git-spice. |
4beaa0f to
b2314a2
Compare
8d79b2f to
d30794f
Compare
d30794f to
41c86ec
Compare
b2314a2 to
c61ff43
Compare
41c86ec to
4693c4c
Compare
c61ff43 to
b43c865
Compare
4693c4c to
452cd72
Compare
b43c865 to
509b380
Compare
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]>
509b380 to
1a97db0
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/cache/cache.go (1)
4463-4472: ⚡ Quick winAdd 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 undergo test -race ./....As per coding guidelines,
**/*.go: Usego 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.
#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.
#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.
#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.
Summary
DO NOTHING+ re-fetch pattern, detect whether thefetched row is a stub (URL is NULL or empty) written by a
concurrent goroutine mid-flight.
UpdateOneIDto fill in the caller's narinfo databefore returning the row.
Why
DO NOTHINGreturns the pre-existing row as-is. If another goroutineinserted 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
upsertNarInfoFromParsedreturns.🤖 Generated with Claude Code