test: avoid scheduler races in context waits - #2451
Merged
xushiwei merged 2 commits intoAug 30, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Review: avoid scheduler races in context waits
Focused, well-scoped change to test/std/context/context_test.go. It introduces two helpers — requireDone (non-blocking "must already be done") and waitDone (wait up to a timeout, with a recheck) — and replaces the ad-hoc select { case <-ch: ... case <-time.After(...) } patterns.
Assessment: The refactor is correct and improves the tests.
requireDoneis the right (stricter) check at the four synchronous-cancellation sites: an explicitcancel(...)or a past deadline/timeout cancels the context before the constructor/cancel call returns, so a non-blockingselectwithdefaultis appropriate and would catch a regression where cancellation is not synchronous.waitDonecorrectly usestime.NewTimer+defer timer.Stop()(no leaked timer) and rechecksdoneafter the timer fires to avoid a false timeout whenselectpicks the timer branch even thoughdonebecame ready.- Happy-path runtime is unaffected: the larger bounds only apply on the failure path; on success the
<-donebranch is taken immediately. - No security or performance concerns.
Two minor, non-blocking notes are inline below.
LLGo baseline benchmarks
Program measurements
Core language and compiler benchmarks
Compared with |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Doneagainst a newly created short timerAfterFuncdelivery a realistic bound and recheck the success channel if the timeout case is selectedThis changes tests only; it does not relax or modify
context, timer, or scheduler semantics.Why
The Windows MinGW full-test lane in cpunion#208 reported
TestAfterFuncandTestWithTimeoutCause, then passed unchanged on rerun.WithTimeoutCause(parent, -time.Hour, cause)closesDonebefore returning. In the old assertion,time.After(10ms)was created while entering the select; if the test goroutine was descheduled long enough, both channels were ready and Go could legally choose the timeout case. The 100ms asynchronous callback bound had the same scheduler sensitivity.The immediate cases now verify the stronger contract directly with a nonblocking receive. The asynchronous helper also checks
Doneagain before reporting a timeout, so a random select choice cannot produce a false failure.Validation
go test ./test/std/context -count=100go test -race ./test/std/context -count=10