Skip to content

provider: two arms of one hedge race recording their rows at the same time deadlock, and the turn never ends #330

Closed

Description

@santoshkumarradha

Hit while running the #321 wire lane (TestManualOnTheWire, deepseek/deepseek-v4-flash): the run went silent mid-question and stayed silent for 26 minutes with the process asleep on 27s of CPU. SIGQUIT named it — two goroutines of the same hedge race, each holding the lock the other is waiting for.

The two orders

streamWatch.mu → hedgeRace.mu — internal/provider/waitreport.go, streamWatch.facts:

w.mu.Lock()
facts := waitFacts{
    lane: w.race.askedLane(w.arm),   // <- takes race.mu while holding w.mu
    ...
}
w.mu.Unlock()
facts.arms, facts.waste, facts.note = w.race.spend(w.arm)   // correctly outside

hedgeRace.mu → streamWatch.mu — internal/provider/hedge.go, hedgeRace.spend:

r.mu.Lock()
defer r.mu.Unlock()
for _, other := range r.arms {
    if other.index != arm {
        waste += r.estimateLocked(laneOf(other), other.watch.written())
        // laneOf -> other.watch.lane() -> watch.mu, and written() -> watch.mu,
        // both while holding race.mu
    }
}

Two arms of one race recording their rows at the same time is all it takes: arm A is inside facts holding watchA.mu and waiting on race.mu; arm B is inside spend holding race.mu and walking every arm's watch, including watchA.

The stacks, from the run

goroutine 913 [sync.Mutex.Lock, 26 minutes]:
  provider.(*hedgeRace).askedLane   hedge.go:940       <- wants race.mu
  provider.(*streamWatch).facts     waitreport.go:262  <- holds watch(0x…65 9e0).mu
  provider.(*Client).record         calllog.go:237
  provider.(*Client).send           retry.go:212

goroutine 979 [sync.Mutex.Lock, 26 minutes]:
  provider.(*streamWatch).lane      armwatch.go:321    <- wants watch(0x…65 9e0).mu
  provider.laneOf                   hedge.go:1072
  provider.(*hedgeRace).spend       hedge.go:962       <- holds race.mu
  provider.(*streamWatch).facts     waitreport.go:273
  provider.(*Client).record         calllog.go:237
  provider.(*Client).send           retry.go:212

Everything above them piles up behind it: hedgeRace.count and hedgeRace.hears sit on race.mu for the same 26 minutes, the turn never ends, and the caller's own 6-minute context does not help — the goroutines are parked on a mutex, not on the request.

What it costs

The turn never finishes and the surface shows a session that is visibly working and saying nothing. It needs a race that actually hedged (two arms) and both arms recording at once, so it is rare and it is not rare enough: it cost one whole measurement pass of 47 questions.

Replication

Deterministic, no model, no network — two goroutines against one race with two armed watches:

// internal/provider, -race, run it a few hundred times
func TestTwoArmsRecordingAtOnceDoNotDeadlock(t *testing.T) {
    race := aRaceWithTwoArmedWatches(t)          // both arms have a streamWatch
    done := make(chan struct{}, 2)
    for _, arm := range []int{0, 1} {
        go func() { _, _ = race.arms[arm].watch.facts(); done <- struct{}{} }()
    }
    for range 2 {
        select {
        case <-done:
        case <-time.After(5 * time.Second):
            t.Fatal("two arms recording their rows deadlocked")
        }
    }
}

The wire lane reproduces it too, but only by luck: go test -tags e2e -count=1 -run TestManualOnTheWire ./internal/e2e/ and wait for a question the router hedges.

Acceptance

  • The test above passes a few hundred consecutive runs under -race.
  • No path in internal/provider takes streamWatch.mu and hedgeRace.mu in both orders. The narrow fix is one line at the seam: facts already reads spend outside w.mu, and askedLane should be read the same way — take both race readings before or after the w.mu section, never inside it.
  • A wire pass of TestManualOnTheWire runs 47 questions end to end without going silent.

Drafted with CodeAF · reviewed and owned by the author

Activity

  1. santoshkumarradha commented on Sep 2, 2026

    @santoshkumarradha
    MemberAuthor

    Provenance and preservation for the run that produced these stacks, added so the artifact outlives the lane's worktree.

  2. added
    bug
    on Oct 3, 2026
  3. added
    area:provider
    on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area:providerbug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions