Skip to content

test(cache): fail fast on ConcurrentDownloadCancel hang (refs #1252) - #1257

Merged
kalbasit merged 2 commits into
mainfrom
fail-fast-concurrent-download-cancel-hang
May 24, 2026
Merged

kalbasit merged 2 commits into
mainfrom
fail-fast-concurrent-download-cancel-hang

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

testConcurrentDownloadCancelOneClientOthersContinue had two un-timeout-guarded blocking calls after the existing 5–10 s t.Fatal guards: io.ReadAll(readerB) (line 1916) and wg.Wait() (line 1932). When the shared streaming goroutine got stuck in some race with client A's cancellation, readerB's pipe writer never closed and io.ReadAll blocked for the full go test -timeout budget — observed as 20-minute hangs on CI under heavy load during the lean-flake-check measurement runs.

This PR wraps both calls in a new test-only helper runWithTimeout (30 s budget, dumps all goroutines via runtime.Stack on timeout) so the next occurrence of the hang fails the test in 30 s with the exact stuck-goroutine trace instead of consuming 20 minutes of CI.

What this PR does NOT do

It does not fix the underlying race. The race is non-trivial: not reproducible locally even under 200× -race -count stress, code inspection in c.GetNar's shared-reader-fanout path is suggestive but ambiguous (see #1252 for the hypotheses), and a real fix needs goroutine-level diagnostics from an actual CI hang.

Once this PR is merged and the hang fires once more in CI, the failure will print exactly which goroutine is stuck where. That data should let us pinpoint and fix the root cause cheaply.

Stack

main → fix-narinfo-distributed-coordination-race (#1256, the real ds.stored race fix)
     → fail-fast-concurrent-download-cancel-hang (this PR)

Test plan

  • go test -race -count=50 -run "TestCacheBackends/SQLite/ConcurrentDownloadCancelOneClientOthersContinue" ./pkg/cache/ → 50/50 pass (the bug doesn't reproduce locally; this is purely defensive instrumentation).
  • golangci-lint run ./pkg/cache/cache_test.go → 0 issues after --fix.

Refs #1252.

🤖 Generated with Claude Code

@kalbasit

kalbasit commented May 24, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. go Pull requests that update go code labels May 24, 2026
@coderabbitai

coderabbitai Bot commented May 24, 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: 46fe8b12-c38d-40fa-9cba-0873b94b0c65

📥 Commits

Reviewing files that changed from the base of the PR and between 0f4c62d and 9c27534.

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

📝 Walkthrough

Summary by CodeRabbit

  • Tests
    • Added local timeout utilities to prevent test hangs and ensure faster failure for concurrent download scenarios.
    • Improved failure diagnostics by capturing and reporting goroutine stack traces when timeouts occur, making test failures easier to investigate.

Walkthrough

Adds a local test timeout helper and applies it to the ConcurrentDownloadCancelOneClientOthersContinue test: imports runtime, defines runWithTimeout to enforce deadlines and dump goroutine stacks on timeout, and wraps a blocking read and wg.Wait() with this helper.

Changes

Test Timeout and Diagnostics

Layer / File(s) Summary
Timeout helper infrastructure
pkg/cache/cache_test.go
Adds runtime import and defines runWithTimeout helper that executes a function in a goroutine, waits up to a deadline, and on timeout dumps all goroutine stacks via runtime.Stack before failing the test.
Test timeout protection
pkg/cache/cache_test.go
Wraps the blocking io.ReadAll(readerB) read and wg.Wait() call in the test with the timeout helper to ensure the test fails fast with actionable diagnostics if goroutines stall after cancellation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Suggested labels

merge-stack

Poem

🐰 I watched the tests hop, then stopped in their tracks,
A timeout I crafted to guard against lacks.
When goroutines dawdle and answers run late,
I dump every stack — no more endless wait.
Hippity-hop, diagnostics celebrate!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding timeout instrumentation to a test that was hanging during cancellation scenarios.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem (blocking calls causing 20-minute hangs), the solution (timeout wrapper with goroutine dumps), and test results.
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 introduces a runWithTimeout helper in pkg/cache/cache_test.go to prevent tests from hanging indefinitely and to provide goroutine dumps for debugging concurrency issues. This helper is integrated into the testConcurrentDownloadCancelOneClientOthersContinue test. The reviewer suggested improving the helper by using time.NewTimer for better resource management and implementing a dynamic buffer for runtime.Stack to prevent truncation of diagnostic information.

Comment thread pkg/cache/cache_test.go Resolved

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/cache/cache_test.go (1)

1845-1845: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Fix compilation: sync.WaitGroup used with wg.Go() in pkg/cache/cache_test.go

  • wg is declared as sync.WaitGroup but wg.Go(func() { ... }) is called at ~1845/~1862 and again at ~2263/~2275; sync.WaitGroup has no Go() method (it only has Add/Done/Wait), so this won’t compile—switch to errgroup.Group or replace with wg.Add(1) + defer wg.Done() + direct goroutine launches.
  • Timeout helper (runWithTimeout) + its runtime.Stack goroutine-dump on timeout and the 30*time.Second wrapping of io.ReadAll(readerB) / wg.Wait() look correct.
🤖 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_test.go` at line 1845, The tests declare wg as sync.WaitGroup
but call wg.Go(...), which fails because sync.WaitGroup has no Go method; fix by
replacing the sync.WaitGroup usage at the wg.Go call sites (references: wg.Go,
wg.Wait, wg.Add/Done usage) with either an errgroup.Group (create an
errgroup.Group variable and call g.Go for goroutine spawn, then g.Wait) or keep
the sync.WaitGroup and change each wg.Go(func(){...}) to wg.Add(1); go func(){
defer wg.Done(); ... }(); ensuring runWithTimeout stays unchanged and continue
to call wg.Wait (or g.Wait) where appropriate.
🤖 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.

Outside diff comments:
In `@pkg/cache/cache_test.go`:
- Line 1845: The tests declare wg as sync.WaitGroup but call wg.Go(...), which
fails because sync.WaitGroup has no Go method; fix by replacing the
sync.WaitGroup usage at the wg.Go call sites (references: wg.Go, wg.Wait,
wg.Add/Done usage) with either an errgroup.Group (create an errgroup.Group
variable and call g.Go for goroutine spawn, then g.Wait) or keep the
sync.WaitGroup and change each wg.Go(func(){...}) to wg.Add(1); go func(){ defer
wg.Done(); ... }(); ensuring runWithTimeout stays unchanged and continue to call
wg.Wait (or g.Wait) where appropriate.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d8f53b42-0a4e-435a-96b0-dec8425a8226

📥 Commits

Reviewing files that changed from the base of the PR and between a9af63f and 0f4c62d.

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

Base automatically changed from fix-narinfo-distributed-coordination-race to main May 24, 2026 16:22
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels May 24, 2026
kalbasit and others added 2 commits May 24, 2026 09:23
testConcurrentDownloadCancelOneClientOthersContinue had two
un-timeout-guarded blocking calls after the existing 5-10 s
t.Fatal guards:

  - io.ReadAll(readerB) on line 1916
  - wg.Wait() on line 1932

When the shared streaming goroutine got stuck in some race with
client A's cancellation (root cause unknown, not reproducible
locally even under 200x stress), readerB's pipe writer never
closed and io.ReadAll blocked for the full `go test -timeout`
budget — observed as 20-minute hangs on CI under heavy load
during the lean-flake-check measurement runs. See ncps #1252 for
the original filing and the hypothesis about the shared-reader
fanout path in c.GetNar.

Wrap both blocking calls in a new test-only helper, runWithTimeout,
that runs fn in a goroutine, fails the test if it doesn't complete
within d, and dumps all goroutines via runtime.Stack on timeout.
30 s budget is generous for the streaming path but well under the
20-min outer test timeout — converting a silent hang into a
fast-failing test with the exact goroutine stack at the failure
point.

This does NOT fix the underlying race. It makes the next
occurrence diagnosable so the actual fix can land based on real
data instead of speculation.

Verified locally: 50/50 passing runs at -race (the bug doesn't
reproduce locally; this is purely defensive instrumentation for CI).

Refs #1252.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Replace time.After with time.NewTimer + defer Stop() so the timer
is reclaimed immediately when fn completes rather than waiting for
the full duration to expire.

Use a dynamic doubling buffer for runtime.Stack so the goroutine
dump is never truncated in complex concurrent tests with many
goroutines — the 1 MB fixed buffer could silently cut off the
diagnostic information this helper exists to provide.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@kalbasit
kalbasit force-pushed the fail-fast-concurrent-download-cancel-hang branch from 0f4c62d to 9c27534 Compare May 24, 2026 16:26
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels May 24, 2026
@kalbasit
kalbasit enabled auto-merge (squash) May 24, 2026 16:27
@kalbasit
kalbasit merged commit 8c61ec1 into main May 24, 2026
30 of 40 checks passed
@kalbasit
kalbasit deleted the fail-fast-concurrent-download-cancel-hang branch May 24, 2026 16:40
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 size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant