Skip to content

context: skip the Done receive in Err when the channel is closedchan - #80915

Open
davidteather wants to merge 1 commit into
golang:masterfrom
davidteather:dteather/context-closedchan
Open

context: skip the Done receive in Err when the channel is closedchan#80915
davidteather wants to merge 1 commit into
golang:masterfrom
davidteather:dteather/context-closedchan

Conversation

@davidteather

@davidteather davidteather commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Err receives from the Done channel so that a non-nil error implies the
channel is closed. A blocking receive takes hchan.lock even when the
channel is already closed, and Done is lazy. Until something calls it,
cancel stores the package-level closedchan. Every canceled context in
the program shares one lock.

BenchmarkErrCanceledParallelDistinct gives each goroutine its own
context and is just as slow as the shared-context case, which is what
identifies the shared global rather than any per-context state.

closedchan is closed at init and cannot reopen, so observing it needs
no synchronization. Skip the receive in that case. Done still supplies
the channel, so a cancel in flight blocks on c.mu as before.

goos: darwin
goarch: arm64
pkg: context
cpu: Apple M2 Pro
│ old │ new │
│ sec/op │ sec/op vs base │
ErrOK-10 2.017n ± 2% 1.979n ± 2% ~ (p=0.427 n=25)
ErrCanceled-10 14.000n ± 2% 3.973n ± 2% -71.62% (p=0.000 n=25)
ErrOKParallel-10 0.3072n ± 7% 0.3191n ± 3% ~ (p=0.184 n=25)
ErrCanceledParallel-10 36.1900n ± 1% 0.7030n ± 8% -98.06% (p=0.000 n=25)
ErrCanceledParallelDistinct-10 36.8500n ± 2% 0.6554n ± 9% -98.22% (p=0.000 n=25)
geomean 6.496n 1.029n -84.15%

B/op and allocs/op are unchanged. ErrOK and ErrOKParallel return before
the changed branch.

Fixes #80914

@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: f2884a2) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/815961.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Ian Lance Taylor:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

Err receives from the Done channel so that a non-nil error implies the
channel is closed. A blocking receive takes hchan.lock even when the
channel is already closed, and Done is lazy. Until something calls it,
cancel stores the package-level closedchan. Every canceled context in
the program shares one lock.

BenchmarkErrCanceledParallelDistinct gives each goroutine its own
context and is just as slow as the shared-context case, which is what
identifies the shared global rather than any per-context state.

closedchan is closed at init and cannot reopen, so observing it needs
no synchronization. Skip the receive in that case. Done still supplies
the channel, so a cancel in flight blocks on c.mu as before.

goos: darwin
goarch: arm64
pkg: context
cpu: Apple M2 Pro
                               │      old      │                 new                  │
                               │    sec/op     │    sec/op     vs base                │
ErrOK-10                           2.017n ± 2%    1.979n ± 2%        ~ (p=0.427 n=25)
ErrCanceled-10                    14.000n ± 2%    3.973n ± 2%  -71.62% (p=0.000 n=25)
ErrOKParallel-10                  0.3072n ± 7%   0.3191n ± 3%        ~ (p=0.184 n=25)
ErrCanceledParallel-10           36.1900n ± 1%   0.7030n ± 8%  -98.06% (p=0.000 n=25)
ErrCanceledParallelDistinct-10   36.8500n ± 2%   0.6554n ± 9%  -98.22% (p=0.000 n=25)
geomean                            6.496n         1.029n       -84.15%

B/op and allocs/op are unchanged. ErrOK and ErrOKParallel return before
the changed branch.

Fixes golang#80914
@davidteather
davidteather force-pushed the dteather/context-closedchan branch from f2884a2 to 86eaaf2 Compare August 18, 2026 00:02
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 86eaaf2) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/815961.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from David Teather:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Ian Lance Taylor:

Patch Set 2: Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 2:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-08-18T00:18:48Z","revision":"3ba348a463a306afc069c4b2d024136c6f4695da"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Ian Lance Taylor:

Patch Set 2: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_5206>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 2:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 2: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/815961.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

context: Err on a canceled context contends on a process global channel lock

2 participants