Skip to content

test(offload): arrange the cross-session single-flight join, not a 150ms sleep (#748) - #749

Merged
amiddavid merged 1 commit into
mainfrom
fix/glm-sleep-join
Oct 27, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/glm-sleep-join

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

Fixes one flaky test. Written by GLM through Claude Code.

TestConcurrentIdenticalExtractionsCollapseToOneCall waited for its two sessions with time.Sleep(150ms), not a join. The sleep is now gone.

What changed

Why it happens

The test started two identical extractions in parallel, then slept 150 ms and hoped both had joined the single-flight call by then. On a loaded box, a caller can join late. Then it makes its own model call, and the "one call" assertion fails. #479 and #162 had the same defect; #742 fixed both with the parked-leader helper this change reuses.

How it was tested

All runs on the eval box, components/offload only.

Run Command Result
Targeted, fixed go test -race -run TestConcurrentIdenticalExtractionsCollapseToOneCall -count=20 -cpu 1,2,4 PASS (60 runs)
Load, clean origin/main 64 concurrent copies of the test binary, -test.count=20 -test.cpu=1, under -race FAIL 17 of 64 — "made 2 model calls"
Load, this branch same 64-copy burst PASS 64 of 64
Full package suite go test -race -count=1 ./components/offload/ PASS (172.5 s)
gofmt -l components/offload/ — clean
go vet ./components/offload/ — clean

Data dependence

None changes. Test-only change.

Skipped

None.

Closes #748

🤖 Generated with Claude Code

…0ms sleep (#748)

TestConcurrentIdenticalExtractionsCollapseToOneCall slept 150ms and hoped
both sessions were at the single-flight join when the leader was released.
Under load a straggler joined after the leader finished, made its own model
call, and failed the count assertion. #479 and #162 had the same defect;
#742 fixed both with a parked leader.

Use that machinery here: parkingModel parks the leader in Complete, the
beforeInflightJoinForTest hook reports each candidate's arrival at the join,
and the test closes release only after both sessions' candidates have
arrived. The follower is now a fact the fixture arranged, not scheduling
luck. Remove the sleep and the now-unused blockingModel.

Proof on the eval box, 64 concurrent copies of the test binary, 20 runs
each under -race: clean origin/main fails 17 of 64 ("made 2 model calls");
this change passes 64 of 64. Also passes -race -count=20 -cpu 1,2,4.

Written by GLM through Claude Code

Assisted-By: GLM
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.311 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 be1c6e476199 approve

Verdict: approve

Findings

None.

Non-blocking

  • components/offload/extract_econ_test.go:621 — the arrived channel has capacity exactly arrivals. If the fixture ever drifts to more candidates per request (a second tool result, a gate change), the extra hook send blocks forever inside beforeInflightJoinForTest, and the timeout path's wg.Wait() then hangs too — the failure mode becomes the package timeout, not the intended Fatalf message. offloadWithParkedLeader (common_test.go:136) has the same shape, so this matches house style. A send with a select on a done channel, or a buffer with slack plus an exact-count assertion, would fail cleanly instead.
  • components/offload/extract_econ_test.go:620-656 — the hook setup and wait loop re-implement offloadWithParkedLeader inline. Justified: the helper takes one request, this test needs two. If a third test needs the same arrangement, generalize the helper to take a list of requests.

What I checked

  • Read the full diff: one file, components/offload/extract_econ_test.go, test-only. blockingModel is deleted; no other file referenced it (grep over components/).
  • Verified the arrangement is sound, not just different:
    • beforeInflightJoinForTest (extract_llm.go:1319-1321) fires once per candidate, one statement before extractInflight.Do. toolIndices (common.go:457) picks only tool-role messages, so one tool result per request gives one candidate per request — 2 arrivals total, as the fixture expects.
    • The two requests share e, so e.modelName and extCfg are identical; extCfg.CacheContext is false in both (1 candidate each, extract_llm.go:1229). cands[k].id is extract.ContentKey(content) (extract_llm.go:919), which is content-derived and session-independent (internal/extract/resultkey_test.go pins this). So both candidates compute the same single-flight key.
    • The leader parks in parkingModel.Complete (common_test.go:114-118), so the key is still held when the other candidate reaches Do. Both arrivals are consumed before close(release), so the follower joins as a fact. The cross-session persistent cache (getResultGlobal, extract_llm.go:1023) cannot hit for the second request: the leader parks before putResultGlobal (extract_llm.go:1629) runs. e.mode = markerFull keeps that global path active, so the fixture exercises the real window.
    • Assertion: only the leader's closure runs, so model.calls is 1; a follower that failed to dedup would make 2 and fail. Counting via atomic.AddInt64/atomic.LoadInt64 is race-clean.
  • Checked the failure paths. On a short-arrival timeout, the test closes release and wg.Wait()s before Fatalf, so no goroutine races the cleanup that nils the hook. Hook set before the goroutines start, so the write happens-before their reads. wg.Wait() before the assertion on the happy path too.
  • Checked imports: time, sync, atomic, math all still used after the time.Sleep removal; nothing dangling.
  • Checked this is not a vacuous pass: the arrivals loop Fatalfs if fewer than 2 candidates reach the join, so a gate or cap that trims the batch fails the test rather than passing it silently.
  • Confirmed no production code changed, so fail-open behavior and data dependence are untouched; docs/data-dependence.md needs no update for a test-only PR.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.311 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 be1c6e476199 approve

Verdict: approve

Findings

None. The change is correct and removes the timing dependence it targets.

Non-blocking

  • components/offload/extract_econ_test.go:644 — The arrangement has a residual two-statement window. A goroutine can be preempted between the hook send and the extractInflight.Do call (extract_llm.go:1319-1322). If that happens, the leader can finish and drop the key before the follower registers, and the count becomes 2. The window is nanoseconds wide, and the same seam already ships in offloadWithParkedLeader (common_test.go:133). x/sync/singleflight offers no observable "waiter registered" point, so this is the best seam available. Note only; no change requested.
  • components/offload/extract_econ_test.go:644-656 — Each loop iteration makes a new time.After(10 * time.Second), so the total wait is up to 20s for 2 arrivals. One deadline for the whole loop would be tighter. Cosmetic.
  • The arrival loop, hook assignment, and cleanup duplicate offloadWithParkedLeader rather than reusing it. The reuse is not possible as-is: the helper runs one Offload, and this test needs two concurrent ones. Fine as it is.

What I checked

  • The full diff d08eff8b..HEAD: one file, components/offload/extract_econ_test.go, 34 insertions, 19 deletions. One commit, be1c6e47.
  • The hook seam: beforeInflightJoinForTest fires at extract_llm.go:1319, one statement before extractInflight.Do, inside runCall and past the llmConcurrency semaphore (const 4, extract_llm.go:106). Only the leader reaches parkingModel.Complete, so the in-flight key stays held while the other candidate joins.
  • Candidate count: each request has one tool message and one user message; candidates come from toolIndices (common.go:457), so exactly one candidate per request and exactly 2 arrivals. The buffered channel of size 2 can never block a sender.
  • Key identity: ResultKey hashes ContentKey(content), model name, and config (internal/extract/extract.go:205). Content is byte-identical across the two requests and nothing in the key is session-scoped, so both candidates share one single-flight key.
  • No vacuous pass: the global result cache is written only after the leader finishes (extract_llm.go:1510 onward, phase 3), and release is closed only after both arrivals. A trimmed or gated candidate now fails the test loudly at the 10s window instead of passing silently.
  • Cleanup safety: Offload waits for all runCall goroutines (extract_llm.go:1498), and the test calls wg.Wait() on both the normal and the timeout path before t.Fatalf. So the t.Cleanup that nils the hook cannot race a firing goroutine. The timeout path also closes release before waiting, so it cannot hang.
  • Concurrency discipline: no test in the package calls t.Parallel() (both grep hits are comments), so the package-global hook matches the discipline common_test.go:129 documents.
  • Dead code and imports: blockingModel had no other references; time, sync, sync/atomic, strings, and context are all still used in the file.
  • parkingModel.calls is written with atomic.AddInt64 and read with atomic.LoadInt64. No race.

@amiddavid
amiddavid merged commit 387664e into main Oct 27, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 27, 2026
@amiddavid
amiddavid deleted the fix/glm-sleep-join branch October 27, 2026 17:52
@amiddavid amiddavid mentioned this pull request Oct 28, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

test(offload): TestConcurrentIdenticalExtractionsCollapseToOneCall waits with a sleep, not a join

1 participant