Skip to content

fix(cache): don't route serve to chunks without a chunk store - #1325

Merged
kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-flaky-pg-test
Jun 4, 2026
Merged

kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-flaky-pg-test

Conversation

@kalbasit

@kalbasit kalbasit commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the flaky TestCacheBackends/.../RunLRUCleanupInconsistentNarInfoState
failure in CI (run 26929993682)
— chunk store not initialized, cannot serve NAR from chunks.

The root cause is a TOCTOU on hasInStore in GetNar. It reads
HasNarInStore once, then re-evaluates servability via isServable, which
performs its own fresh HasNarInStore. When the whole-file NAR lands in the
store between those two checks, the first flag is stale (false) while the NAR
is in fact present. serveNarFromStorageViaPipe then computed
serveFromChunks := !hasInStore = true and routed an uncompressed request to
getNarFromChunks, which hard-fails when no chunk store is configured.

This is the inverse of #1324 (present-at-check, deleted-before-use); here it is
absent-at-check, present-before-use.

Fix

  • Gate the chunk route on chunk-store availability:
    serveFromChunks := !hasInStore && c.isChunkStoreAvailable().
  • When no chunk store exists, an uncompressed request resolves against the
    whole-file store: a present whole file is served; a genuine absence returns a
    clean storage.ErrNotFound, preserving cache-miss recovery and the
    upload-only contract.
  • When a chunk store is present the gate reduces to the original
    !hasInStore, so standard CDC and drain-mode serving are byte-identical
    (the fix(cache): fall back to chunks when migration races serve #1324 store→chunks fallback is unchanged).

Test plan

  • New deterministic regression test reproduces the exact CI error
    (RED → GREEN): pkg/cache/cdc_serve_stale_hasinstore_internal_test.go
  • Formerly-flaky RunLRUCleanupInconsistentNarInfoState passes 5× under -race
  • #1324 migration-race tests still pass (no regression)
  • task fmt, task lint (0 issues), task test, and full task test:auto
    (real Postgres/MySQL/Redis/Garage) pass

Spec

Adds the cdc-chunking requirement "Serving a whole-file NAR MUST be resilient
to a stale time-of-check store-presence observation" (4 scenarios), and archives
the OpenSpec change under
openspec/changes/archive/2026-06-03-fix-flaky-pg-test/.

The flaky TestCacheBackends/.../RunLRUCleanupInconsistentNarInfoState
failed with "chunk store not initialized, cannot serve NAR from
chunks". The cause is a TOCTOU on hasInStore in GetNar: it reads
HasNarInStore once, then re-evaluates servability via isServable, which
performs its own fresh HasNarInStore. When the whole-file NAR lands in
the store between those two checks, the first flag is stale (false)
while the NAR is in fact present. serveNarFromStorageViaPipe then
computed serveFromChunks := !hasInStore = true and routed an
uncompressed request to getNarFromChunks, which hard-fails when no
chunk store is configured.

This is the inverse of #1324 (present-at-check, deleted-before-use);
here it is absent-at-check, present-before-use. Gate the chunk route on
isChunkStoreAvailable() so an uncompressed request is never routed to
chunks when no chunk store exists; the present whole file is served and
a genuine absence returns a clean ErrNotFound (cache-miss recovery and
the upload-only contract are preserved). When a chunk store IS present
the gate reduces to the original !hasInStore, so standard CDC and drain
serving are unchanged.

Adds a deterministic regression test reproducing the exact error
(RED -> GREEN) and syncs the cdc-chunking spec with the new
resilience requirement.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit

kalbasit commented Jun 4, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jun 4, 2026
@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bf18e44f-c318-4b3b-9e2c-ef49ea01ed30

📥 Commits

Reviewing files that changed from the base of the PR and between 5515c1a and 828186d.

📒 Files selected for processing (8)
  • openspec/changes/archive/2026-06-03-fix-flaky-pg-test/.openspec.yaml
  • openspec/changes/archive/2026-06-03-fix-flaky-pg-test/design.md
  • openspec/changes/archive/2026-06-03-fix-flaky-pg-test/proposal.md
  • openspec/changes/archive/2026-06-03-fix-flaky-pg-test/specs/cdc-chunking/spec.md
  • openspec/changes/archive/2026-06-03-fix-flaky-pg-test/tasks.md
  • openspec/specs/cdc-chunking/spec.md
  • pkg/cache/cache.go
  • pkg/cache/cdc_serve_stale_hasinstore_internal_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a race condition causing intermittent cache serving failures. The system now correctly serves cached content even when storage availability checks occur at different times.
  • Tests

    • Added regression tests for cache serving with stale presence checks.
  • Documentation

    • Enhanced specifications for cache serving resilience and error handling.

Walkthrough

This PR fixes a TOCTOU race in the NAR cache serving path where stale store-presence observations cause uncompressed requests to misroute to chunk serving. The fix re-gates routing on current chunk-store availability within serveNarFromStorageViaPipe, adds comprehensive specification updates, and includes deterministic regression tests.

Changes

Stale HasInStore Routing Fix

Layer / File(s) Summary
Problem statement and specification
openspec/changes/archive/2026-06-03-fix-flaky-pg-test/.openspec.yaml, openspec/changes/archive/2026-06-03-fix-flaky-pg-test/design.md, openspec/changes/archive/2026-06-03-fix-flaky-pg-test/proposal.md, openspec/changes/archive/2026-06-03-fix-flaky-pg-test/specs/cdc-chunking/spec.md, openspec/changes/archive/2026-06-03-fix-flaky-pg-test/tasks.md, openspec/specs/cdc-chunking/spec.md
Design and proposal documents detail the TOCTOU race: GetNar captures stale store-absence information that causes serveNarFromStorageViaPipe to incorrectly route uncompressed requests to chunk serving even after the NAR arrives in storage. Acceptance criteria mandate serving whole-file NARs despite stale observations, avoiding chunk routing when no chunk store exists, and adding deterministic regression tests. Updated specs formalize error-handling constraints and re-check requirements.
Chunk-store routing fix
pkg/cache/cache.go
serveNarFromStorageViaPipe now gates chunk-store routing on both the stale hasInStore flag and current chunk-store availability via c.isChunkStoreAvailable(). This prevents uncompressed requests from routing to chunk serving when the chunk store is not configured, resolving the crash when a NAR arrives between checks.
Deterministic regression tests
pkg/cache/cdc_serve_stale_hasinstore_internal_test.go
Tests validate TOCTOU behavior when no chunk store is configured. The first test stores a whole-file NAR and calls the serve path with stale hasInStore=false, asserting the whole file is served. The second test confirms genuine absence returns storage.ErrNotFound (not chunk-store errors) when the NAR is absent. A helper constructs a cache with real local store and SQLite DB but without chunk-store configuration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

  • kalbasit/ncps#1324: Updates serveNarFromStorageViaPipe to handle storage.ErrNotFound and routing to getNarFromChunks based on chunk-store availability.
  • kalbasit/ncps#1255: Modifies serveNarFromStorageViaPipe chunk-routing logic based on request compression and CDC chunking state.

Suggested labels

bug, go, merge-stack

Poem

🐰 A race condition once made requests stray,
Routing through chunks that vanished away,
But now we re-check what the present time shows,
And serve from whole storage wherever it flows!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(cache): don't route serve to chunks without a chunk store' directly captures the main code change and the core fix, matching the primary modification in pkg/cache/cache.go.
Description check ✅ Passed The description comprehensively explains the TOCTOU issue on hasInStore, the root cause, the implemented fix, test coverage, and spec updates, all directly related to the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 addresses a flaky test failure (RunLRUCleanupInconsistentNarInfoState) caused by a time-of-check to time-of-use (TOCTOU) race condition on the hasInStore flag. When a whole-file NAR is initially observed as absent but lands in the store before being served, the stale flag previously routed uncompressed requests to the chunk store, resulting in a 'chunk store not initialized' error when no chunk store was configured. The fix gates the chunk-routing decision on chunk store availability (c.isChunkStoreAvailable()) in serveNarFromStorageViaPipe. Additionally, a new test file cdc_serve_stale_hasinstore_internal_test.go was added to deterministically reproduce and verify the fix, and relevant specifications were updated. There are no review comments to address.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@kalbasit
kalbasit merged commit d345d50 into main Jun 4, 2026
38 checks passed
@kalbasit
kalbasit deleted the user/wnasreddine/fix-flaky-pg-test branch June 4, 2026 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant