aboutsummaryrefslogtreecommitdiff
path: root/src/context
AgeCommit message (Collapse)Author
2026-03-17context: fix package doc description of WithDeadlineCause and WithTimeoutCauseyongqijia
The package documentation incorrectly states that WithCancelCause, WithDeadlineCause, and WithTimeoutCause all return a CancelCauseFunc. In fact, only WithCancelCause returns a CancelCauseFunc. WithDeadlineCause and WithTimeoutCause accept a cause error parameter directly and return a plain CancelFunc. Update the documentation to accurately describe the difference. Fixes #77478 Change-Id: I581f8a954dd98567bc46e74ded62af927cc48fb2 Reviewed-on: https://go-review.googlesource.com/c/go/+/750824 Reviewed-by: Sean Liao <sean@liao.dev> Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Carlos Amedee <carlos@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2026-03-10context: modernize AfterFunc example using WaitGroup.GoKrushnal Patil
The context.AfterFunc example currently uses the traditional sync.WaitGroup pattern with Add and Done. Update the example to use sync.WaitGroup.Go instead. Fixes #78018 Change-Id: I079a773a6ec1c65f26af2bd8092067843adb1cd1 GitHub-Last-Rev: 9e648ae4241ca0647d3a501f7658d48100c180b8 GitHub-Pull-Request: golang/go#78020 Reviewed-on: https://go-review.googlesource.com/c/go/+/752880 Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2025-11-24context: don't return the wrong error when Cause races cancellationDamien Neil
Check to see if a context is canceled at all before checking for the cancellaion cause. If we can't find a cause, use the original error. Avoids a data race where we look for a cause, find none (because the context is not canceled), the context is canceled, and we then return ctx.Err() (even though there is now a cause). Fixes #73390 Change-Id: I97f44aef25c6b02871d987970abfb4c215c5c80e Reviewed-on: https://go-review.googlesource.com/c/go/+/679835 Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-10-17all: remove unnecessary loop variable copies in testsTobias Klauser
Copying the loop variable is no longer necessary since Go 1.22. Change-Id: Iebb21dac44a20ec200567f1d786f105a4ee4999d Reviewed-on: https://go-review.googlesource.com/c/go/+/711640 Reviewed-by: Florian Lehner <lehner.florian86@gmail.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-09-18context: don't return a non-nil from Err before Done is closedDamien Neil
The Context.Err documentation states that it returns nil if the context's done channel is not closed. Fix a race condition introduced by CL 653795 where Err could return a non-nil error slightly before the Done channel is closed. No impact on Err performance when returning nil. Slows down Err when returning non-nil by about 3x, but that's still almost 2x faster than before CL 653795 and the performance of this path is less important. (A tight loop checking Err for doneness will be terminated by the first Err call to return a non-nil result.) goos: darwin goarch: arm64 pkg: context cpu: Apple M4 Pro │ /tmp/bench.0 │ /tmp/bench.1 │ │ sec/op │ sec/op vs base │ ErrOK-14 1.806n ± 1% 1.774n ± 0% -1.77% (p=0.000 n=8) ErrCanceled-14 1.821n ± 1% 7.525n ± 3% +313.23% (p=0.000 n=8) geomean 1.813n 3.654n +101.47% Fixes #75533 Change-Id: Iea22781a199ace7e7f70cf65168c36e090cd2e2a Reviewed-on: https://go-review.googlesource.com/c/go/+/705235 TryBot-Bypass: Damien Neil <dneil@google.com> Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-by: Nicholas Husin <nsh@golang.org> Auto-Submit: Damien Neil <dneil@google.com>
2025-08-08all: update blog linkjiahua wang
Change-Id: I8631fbc552b85f35b494a8d5a2c0baf68ee66982 Reviewed-on: https://go-review.googlesource.com/c/go/+/690215 Reviewed-by: qiu laidongfeng <2645477756@qq.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com>
2025-06-24context: fix typo in context_test.goWerner Spiegel
Change-Id: Iee90ce7965eec4074fcb81a34968591877a062d3 GitHub-Last-Rev: f18fd099e490b130affa298bfc40da27ac589b26 GitHub-Pull-Request: golang/go#74360 Reviewed-on: https://go-review.googlesource.com/c/go/+/683675 Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Bypass: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2025-04-08context: don't return a nil Cause for a canceled custom contextDamien Neil
Avoid a case where Cause(ctx) could return nil for a canceled context, when ctx is a custom context implementation and descends from a cancellable-but-not-canceled first-party Context. Fixes #73258 Change-Id: Idbd81ccddea82ecabece4373d718baae6ca4b58e Reviewed-on: https://go-review.googlesource.com/c/go/+/663936 Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-03-04context: skip allocs test with -asanDamien Neil
Allocates more with -asan after CL 653795. Change-Id: Ib8cc1de1d649623713b6fc123c1c59a47528857b Reviewed-on: https://go-review.googlesource.com/c/go/+/654876 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-03-03context: use atomic operation in ctx.ErrDamien Neil
oos: darwin goarch: arm64 pkg: context cpu: Apple M1 Pro │ /tmp/bench.0.mac │ /tmp/bench.1.mac │ │ sec/op │ sec/op vs base │ ErrOK-10 13.750n ± 1% 2.080n ± 0% -84.87% (p=0.000 n=10) ErrCanceled-10 13.530n ± 1% 3.248n ± 1% -76.00% (p=0.000 n=10) geomean 13.64n 2.599n -80.94% goos: linux goarch: amd64 pkg: context cpu: Intel(R) Xeon(R) CPU @ 2.30GHz │ /tmp/bench.0.linux │ /tmp/bench.1.linux │ │ sec/op │ sec/op vs base │ ErrOK-16 21.435n ± 0% 4.243n ± 0% -80.21% (p=0.000 n=10) ErrCanceled-16 21.445n ± 0% 5.070n ± 0% -76.36% (p=0.000 n=10) geomean 21.44n 4.638n -78.37% Fixes #72040 Change-Id: I3b337ab1934689d2da4134492ee7c5aac8f92845 Reviewed-on: https://go-review.googlesource.com/c/go/+/653795 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
2025-02-20context: replace newAfterFuncContext with a global declaration using blank ↵Jes Cok
identifier newAfterFuncContext has never been used, the only reason I can imagine for its existence is to guarantee that the implementation is correct. It is a small cleanup and make code more idiomatic. Change-Id: I61ee213a9284f3c3bda7f91196f3a1604babd0f6 GitHub-Last-Rev: c08bd69ef3f0dbc2f573f54095fea35dad2b1145 GitHub-Pull-Request: golang/go#71856 Reviewed-on: https://go-review.googlesource.com/c/go/+/651015 Auto-Submit: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2025-01-03context: use "canceled" in docs to refer to timed-out contextsDamien Neil
In documentation, we've usually but not always referred to a context with a closed Done channel as "done" rather than "canceled", to avoid ambiguity between a context canceled by calling a CancelFunc and one past its deadline. This actually adds ambiguity, however, since it's common to see references to a "canceled context" that are intended to cover contexts past their deadline. If you see "function F returns if its context is canceled", you can reasonably assume that F will return if its context passes its deadline, unless something says otherwise. Update the context package docs to explicitly state that a context is canceled when its deadline passes. Drop references to contexts becoming "done" and just use "canceled" throughout. Fixes #70945 Change-Id: I99fbd800c6049deaa37015a304f7f9d9a84100e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/640095 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com>
2024-11-06context: listen on localhost in exampleRuss Cox
Listening on ":0" triggers a Mac firewall box while the test runs. Change-Id: Ie6f8eb07eb76ea222f43bc40b1c30645294bc239 Reviewed-on: https://go-review.googlesource.com/c/go/+/625975 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-09-30context: link to context and structs blog postMatt T. Proud
This commit amends the package documentation for the context package and links the https://go.dev/blog/context-and-structs where the package documentation dissuades one against embedding a context into a struct. This is to help close the gap in understanding why this otherwise cryptic piece of guidance is provided. The other referenced blog article now points to go.dev instead of golang.org. Change-Id: I0844a57bde1c03b6dddd1dd2dab2d20557d791fb GitHub-Last-Rev: 4b039fba90f385a6ca8e44e8a34fbc339dbc21f0 GitHub-Pull-Request: golang/go#69696 Reviewed-on: https://go-review.googlesource.com/c/go/+/616515 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-09-03context: document that WithValue returns a derived contextIan Lance Taylor
Also replace "copy of parent" with "derived context" in doc comments. Fixes #68923 Change-Id: I319c1594f390e35b32b4e58ee979927bb84bfdf9 Reviewed-on: https://go-review.googlesource.com/c/go/+/606555 Reviewed-by: Sameer Ajmani <sameer@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Sameer Ajmani <sameer@golang.org> Commit-Queue: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-07-10context: handle nil values for valueCtx.String()Sean Liao
Fixes #68356 Change-Id: I57dc089a99f545e29a6759a8db5e28fabb6d1a61 Reviewed-on: https://go-review.googlesource.com/c/go/+/597415 Commit-Queue: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-03-26all: fix a large number of commentscui fliter
Partial typo corrections, following https://go.dev/wiki/Spelling Change-Id: I2357906ff2ea04305c6357418e4e9556e20375d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/573776 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: shuang cui <imcusg@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-02-17context: update doc comment to link to context interfaceALX99
Linking to the Context interface in the WithCancel doc comment makes it more consistent with the WithDeadline and WithTimeout doc comments. Change-Id: Ic935c63e8262784be5f3564816402221ba2fbd63 GitHub-Last-Rev: 9c6bb607a94f6f4cd27cc3f5e39c192e088ab386 GitHub-Pull-Request: golang/go#65768 Reviewed-on: https://go-review.googlesource.com/c/go/+/564996 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-02-09context: improve valueCtx.StringJoe Tsai
Check for stringer on the key itself. This is useful for locally defined context key types, where there may be multiple instances of that type. For example, see http.contextKey, which can now be called after this change. For the value itself, print the type at least instead of just resorting to "<not stringer>". Change-Id: I588ef1df34e90fb9ebd83cb180fea495e1fedaa8 Reviewed-on: https://go-review.googlesource.com/c/go/+/555697 Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-09-20all: stop using fmt.Sprintf in t.Error/t.FatalKir Kolyshkin
Change-Id: Id63e1e5ae7e225e4a6a721673bf2d43b6c398c25 Reviewed-on: https://go-review.googlesource.com/c/go/+/527701 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-09-18context: avoid key collisions in test afterfunc mapDamien Neil
The afterFuncContext type, used only in tests, contains a set of registered afterfuncs indexed by an arbitrary unique key. That key is currently a *struct{}. Unfortunately, all *struct{} pointers are equal to each other, so all registered funcs share the same key. Fortunately, the tests using this type never register more than one afterfunc. Change the key to a *byte. Change-Id: Icadf7d6f258e328f6e3375846d29ce0f98b60924 Reviewed-on: https://go-review.googlesource.com/c/go/+/526655 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Bryan Mills <bcmills@google.com>
2023-09-13context: support non-standard Context in CauseIan Lance Taylor
If Cause is called on a non-standard Context, call ctx.Err. Fixes #62582 Change-Id: Iac4ed93203eb5529f8839eb479b6ee2ee5ff6cbc Reviewed-on: https://go-review.googlesource.com/c/go/+/527277 Reviewed-by: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-08-21context: fix synchronization in ExampleAfterFunc_condBryan C. Mills
Condition variables are subtle and error-prone, and this example demonstrates exactly the sorts of problems that they introduce. Unfortunately, we're stuck with them for the foreseeable future. As previously implemented, this example was racy: since the callback passed to context.AfterFunc did not lock the mutex before calling Broadcast, it was possible for the Broadcast to occur before the goroutine was parked in the call to Wait, causing in a missed wakeup resulting in deadlock. The example also had a more insidious problem: it was not safe for multiple goroutines to call waitOnCond concurrently, but the whole point of using a sync.Cond is generally to synchronize concurrent goroutines. waitOnCond must use Broadcast to ensure that it wakes up the target goroutine, but the use of Broadcast in this way would produce spurious wakeups for all of the other goroutines waiting on the same condition variable. Since waitOnCond did not recheck the condition in a loop, those spurious wakeups would cause waitOnCond to spuriously return even if its own ctx was not yet done. Fixing the aforementioned bugs exposes a final problem, inherent to the use of condition variables in this way. This one is a performance problem: for N concurrent calls to waitOnCond, the resulting CPU cost is at least O(N²). This problem cannot be addressed without either reintroducing one of the above bugs or abandoning sync.Cond in the example entirely. Given that this example was already published in Go 1.21, I worry that Go users may think that it is appropriate to use a sync.Cond in conjunction with context.AfterFunc, so I have chosen to retain the Cond-based example and document its pitfalls instead of removing or replacing it entirely. I described this class of bugs and performance issues — and suggested some channel-based alternatives — in my GopherCon 2018 talk, “Rethinking Classical Concurrency Patterns”. The section on condition variables starts on slide 37. (https://youtu.be/5zXAHh5tJqQ?t=679) Fixes #62180. For #20491. Change-Id: If987cd9d112997c56171a7ef4fccadb360bb79bc Reviewed-on: https://go-review.googlesource.com/c/go/+/521596 Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com>
2023-06-11context: fix doc tipo (s/timout/timeout)Ian Lance Taylor
Change-Id: Ib02b35887896eab418ba9dde764754538cb23b4f Reviewed-on: https://go-review.googlesource.com/c/go/+/501277 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-06-06context: add godoc linksOlivier Mengué
This clarifies the ambiguity of the TODO word as raised in golang/go#56625. Also links the introduction text to each function. Note: linking from Context methods documentation is blocked for now by golang/go#59728. Change-Id: Ie6080bd8dee3a652436b0875ddc5f452287c9493 Reviewed-on: https://go-review.googlesource.com/c/go/+/501115 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-08all: make safe for new vet analyzerRuss Cox
The unused analyzer handles dot imports now, so a few tests have picked up vet errors. This CL errors like: context/x_test.go:524:47: result of context.WithValue call not used Change-Id: I711a62fd7b50381f8ea45ac526bf0c946a171047 Reviewed-on: https://go-review.googlesource.com/c/go/+/493598 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2023-04-19context: add AfterFuncDamien Neil
Add an AfterFunc function, which registers a function to run after a context has been canceled. Add support for contexts that implement an AfterFunc method, which can be used to avoid the need to start a new goroutine watching the Done channel when propagating cancellation signals. Fixes #57928 Change-Id: If0b2cdcc4332961276a1ff57311338e74916259c Reviewed-on: https://go-review.googlesource.com/c/go/+/482695 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2023-04-05context: clean up test filesSameer Ajmani
Tests in package context cannot depend directly on package testing due to an import cycle. We resolved this by having test functions in package context_test (x_test.go) forward to test functions in package context (context_test.go). This is fragile, since it's easy to add a test to context_test.go and forget to add the forwarding function, and tests written in this way cannot easily use testing package features like t.Run for subtests. It turns out that only four test functions actually use unexported members of package context. This CL moves all except those four to x_test.go and makes them regular tests. It also updates TestCause to use t.Run and t.Parallel to parallelize its test cases. It also adds documentation indicating when tests should be added to each file. Change-Id: Ic60bae32a7a44e07831b5388c9af219d53ba9af3 Reviewed-on: https://go-review.googlesource.com/c/go/+/480375 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2023-03-29context: add WithoutCancelSameer Ajmani
WithoutCancel returns a copy of parent that is not canceled when parent is canceled. The returned context returns no Deadline or Err, and its Done channel is nil. Calling Cause on the returned context returns nil. API changes: +pkg context, func WithoutCancel(Context) Context Fixes #40221 Change-Id: Ide29631c08881176a2c2a58409fed9ca6072e65d Reviewed-on: https://go-review.googlesource.com/c/go/+/479918 Run-TryBot: Sameer Ajmani <sameer@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-02-10context: reduce init-time allocationsCarlo Alberto Ferraris
Small cleanup to remove a couple of needless global variables. Instead of relying on two instances of emptyCtx having different addresses, we use different types. For #26775 Change-Id: I0bc4813e94226f7b3f52bf4b1b3c3a3bbbebcc9e Reviewed-on: https://go-review.googlesource.com/c/go/+/455455 Reviewed-by: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2023-02-03context: remove one allocation in timerCtxCarlo Alberto Ferraris
Embed the cancelCtx into timerCtx, instead of allocating both separately. name old time/op new time/op delta WithTimeout/concurrency=40-16 2.21µs ±11% 2.08µs ± 5% -5.76% (p=0.011 n=10+10) WithTimeout/concurrency=4000-16 1.94µs ± 6% 1.86µs ±10% ~ (p=0.099 n=9+10) WithTimeout/concurrency=400000-16 1.86µs ± 7% 1.83µs ±10% ~ (p=0.353 n=10+10) name old alloc/op new alloc/op delta WithTimeout/concurrency=40-16 2.56kB ± 0% 2.40kB ± 0% -6.25% (p=0.001 n=8+9) WithTimeout/concurrency=4000-16 2.56kB ± 0% 2.40kB ± 0% -6.25% (p=0.000 n=9+10) WithTimeout/concurrency=400000-16 2.56kB ± 0% 2.40kB ± 0% -6.26% (p=0.000 n=9+9) name old allocs/op new allocs/op delta WithTimeout/concurrency=40-16 50.0 ± 0% 40.0 ± 0% -20.00% (p=0.000 n=10+10) WithTimeout/concurrency=4000-16 50.0 ± 0% 40.0 ± 0% -20.00% (p=0.000 n=10+10) WithTimeout/concurrency=400000-16 50.0 ± 0% 40.0 ± 0% -20.00% (p=0.000 n=10+10) Change-Id: Ia0460db3b8412fbaa6f1539fd6b4fb1b873896c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/463999 Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-01-25context: eliminate arbitrary timeouts in examplesBryan C. Mills
ExampleWithDeadline and ExampleWithTimeout used an arbitrary 1-second timeout for a “blocked” select case, which could fail if the test goroutine happens to be descheduled for over a second, or perhaps if an NTP synchronization happens to jump by a second at just the right time. Either case is plausible, especially on a heavily-loaded or slow machine (as is often the case for builders for unusual ports). Instead of an arbitrary timeout, use a “ready” channel that is never actually ready. Fixes #57594. Change-Id: I9ff68f50b041a3382e7b267c28c5259e886a9d23 Reviewed-on: https://go-review.googlesource.com/c/go/+/460999 TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com>
2023-01-20context: add APIs for setting a cancelation cause when deadline or timer expiresSameer Ajmani
Fixes #56661 Change-Id: I1c23ebc52e6b7ae6ee956614e1a0a45d6ecbd5b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/449318 Run-TryBot: Sameer Ajmani <sameer@golang.org> Reviewed-by: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-28context: add lock in Cause to avoid raceIan Lance Taylor
Change-Id: I8d970e8db859bdd17390cfbc22cc2ba0d326ed0c Reviewed-on: https://go-review.googlesource.com/c/go/+/453735 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Sameer Ajmani <sameer@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2022-11-28context: canceLLationRuss Cox
Fixes post-review comment on CL 375977. Change-Id: If7117fd7b505670eb676a73d991917505bc18a4b Reviewed-on: https://go-review.googlesource.com/c/go/+/453296 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-08context: add APIs for writing and reading cancelation causeSameer Ajmani
Extend the context package to allow users to specify why a context was canceled in the form of an error, the "cause". Users write the cause by calling WithCancelCause to construct a derived context, then calling cancel(cause) to cancel the context with the provided cause. Users retrieve the cause by calling context.Cause(ctx), which returns the cause of the first cancelation for ctx or any of its parents. The cause is implemented as a field of cancelCtx, since only cancelCtx can be canceled. Calling cancel copies the cause to all derived (child) cancelCtxs. Calling Cause(ctx) finds the nearest parent cancelCtx by looking up the context value keyed by cancelCtxKey. API changes: +pkg context, func Cause(Context) error +pkg context, func WithCancelCause(Context) (Context, CancelCauseFunc) +pkg context, type CancelCauseFunc func(error) Fixes #26356 Fixes #51365 Change-Id: I15b62bd454c014db3f4f1498b35204451509e641 Reviewed-on: https://go-review.googlesource.com/c/go/+/375977 Reviewed-by: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> Auto-Submit: Sameer Ajmani <sameer@golang.org>
2022-08-29context: convert goroutines to atomic typecuiweixie
Change-Id: I021fbc9786a3e3f858770fe3e109a0de487390d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/426089 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: xie cui <523516579@qq.com> Reviewed-by: Damien Neil <dneil@google.com>
2022-08-08context: Revert "context: use CompareAndSwap in cancelCtx.Done"Cuong Manh Le
This reverts commit 964f0c7a306998256f1c5a5fd78fc457a972f001. Reason: cause increasing timeout in crypto/tls tests on race builders. Change-Id: Id16d4fcd19c2ca2e89ad4d0c9d55ef1105b19c76 Reviewed-on: https://go-review.googlesource.com/c/go/+/422035 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2022-08-08context: use CompareAndSwap in cancelCtx.DoneCuong Manh Le
So it is not necessary to use a mutex, speedup the code a bit: name old time/op new time/op delta CommonParentCancel-8 756ns ± 1% 760ns ± 1% ~ (p=0.184 n=10+10) WithTimeout/concurrency=40-8 1.36µs ± 6% 1.38µs ± 5% ~ (p=0.367 n=10+9) WithTimeout/concurrency=4000-8 1.30µs ± 1% 1.30µs ± 1% ~ (p=0.444 n=9+9) WithTimeout/concurrency=400000-8 1.25µs ± 1% 1.25µs ± 1% ~ (p=0.268 n=10+10) CancelTree/depth=1/Root=Background-8 62.3ns ± 1% 61.9ns ± 1% -0.72% (p=0.046 n=9+10) CancelTree/depth=1/Root=OpenCanceler-8 447ns ± 1% 437ns ± 0% -2.09% (p=0.000 n=9+9) CancelTree/depth=1/Root=ClosedCanceler-8 210ns ± 1% 211ns ± 1% +0.35% (p=0.024 n=10+10) CancelTree/depth=10/Root=Background-8 2.46µs ± 0% 2.42µs ± 1% -1.86% (p=0.000 n=10+10) CancelTree/depth=10/Root=OpenCanceler-8 3.50µs ± 1% 3.46µs ± 4% ~ (p=0.063 n=9+9) CancelTree/depth=10/Root=ClosedCanceler-8 1.21µs ± 0% 1.22µs ± 1% +0.72% (p=0.001 n=8+10) CancelTree/depth=100/Root=Background-8 26.3µs ± 1% 25.7µs ± 1% -2.20% (p=0.000 n=9+10) CancelTree/depth=100/Root=OpenCanceler-8 34.0µs ± 1% 33.2µs ± 1% -2.15% (p=0.000 n=9+10) CancelTree/depth=100/Root=ClosedCanceler-8 11.2µs ± 1% 11.2µs ± 0% ~ (p=0.562 n=10+9) CancelTree/depth=1000/Root=Background-8 265µs ± 1% 260µs ± 1% -2.15% (p=0.000 n=10+10) CancelTree/depth=1000/Root=OpenCanceler-8 341µs ± 1% 334µs ± 0% -1.90% (p=0.000 n=10+10) CancelTree/depth=1000/Root=ClosedCanceler-8 110µs ± 0% 111µs ± 1% +0.53% (p=0.001 n=9+10) CheckCanceled/Err-8 14.2ns ± 0% 14.2ns ± 0% -0.08% (p=0.012 n=8+8) CheckCanceled/Done-8 6.19ns ± 1% 5.69ns ± 1% -8.11% (p=0.000 n=8+9) ContextCancelDone-8 1.40ns ± 0% 1.31ns ± 0% -6.50% (p=0.000 n=9+9) DeepValueNewGoRoutine/depth=10-8 488ns ± 0% 490ns ± 0% +0.62% (p=0.000 n=9+9) DeepValueNewGoRoutine/depth=20-8 529ns ± 0% 531ns ± 1% +0.46% (p=0.004 n=10+10) DeepValueNewGoRoutine/depth=30-8 589ns ± 1% 594ns ± 0% +0.82% (p=0.004 n=9+9) DeepValueNewGoRoutine/depth=50-8 664ns ± 0% 668ns ± 0% +0.52% (p=0.000 n=10+9) DeepValueNewGoRoutine/depth=100-8 916ns ± 2% 915ns ± 2% ~ (p=0.912 n=10+10) DeepValueSameGoRoutine/depth=10-8 39.6ns ± 1% 38.8ns ± 2% -2.01% (p=0.001 n=9+10) DeepValueSameGoRoutine/depth=20-8 76.9ns ± 1% 74.4ns ± 1% -3.25% (p=0.000 n=9+10) DeepValueSameGoRoutine/depth=30-8 136ns ± 1% 125ns ± 1% -8.53% (p=0.000 n=9+10) DeepValueSameGoRoutine/depth=50-8 196ns ± 1% 192ns ± 1% -1.90% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=100-8 383ns ± 2% 372ns ± 2% -2.86% (p=0.000 n=10+10) Change-Id: Ifb12affed2d6eda1104e4074d63d3f602be4c46b Reviewed-on: https://go-review.googlesource.com/c/go/+/405674 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Damien Neil <dneil@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2022-04-11all: gofmt main repoRuss Cox
[This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-10-02context: implement Context.Value using iteration rather than recursionroudkerk
In profiles of a production server, 2.3% of CPU time is spent in runtime.newstack for stacks with 12 or more chained Context.Value calls. Using iteration will avoid some needless stack resizes. When calling Context.Value in the same goroutine (see DeepValueSameGoRoutine) , no stack resizing is needed (after warming up), and this change reduces time/op by around 10%. The time to start a new goroutine and call Context.Value (see DeepValueNewGoRoutine) is reduced by over 75% if a stack resize is needed. If you factor out the overhead of starting the goroutine (about 960ns) then, avoiding the stack resize saves about 95%. ``` name old time/op new time/op delta CommonParentCancel-12 960ns ± 1% 958ns ± 1% ~ (p=0.561 n=9+9) WithTimeout/concurrency=40-12 1.31µs ± 2% 1.29µs ± 6% ~ (p=0.305 n=9+10) WithTimeout/concurrency=4000-12 1.30µs ± 2% 1.30µs ± 2% ~ (p=0.343 n=10+10) WithTimeout/concurrency=400000-12 1.03µs ± 1% 1.02µs ± 2% ~ (p=0.213 n=9+9) CancelTree/depth=1/Root=Background-12 123ns ± 5% 126ns ± 2% +2.61% (p=0.023 n=10+9) CancelTree/depth=1/Root=OpenCanceler-12 781ns ± 4% 806ns ± 4% +3.20% (p=0.022 n=9+10) CancelTree/depth=1/Root=ClosedCanceler-12 370ns ± 4% 369ns ± 3% ~ (p=0.497 n=9+10) CancelTree/depth=10/Root=Background-12 4.74µs ± 4% 4.78µs ± 3% ~ (p=0.516 n=10+10) CancelTree/depth=10/Root=OpenCanceler-12 6.31µs ± 4% 6.29µs ± 4% ~ (p=1.000 n=10+10) CancelTree/depth=10/Root=ClosedCanceler-12 2.10µs ± 5% 2.09µs ± 5% ~ (p=0.839 n=10+10) CancelTree/depth=100/Root=Background-12 51.0µs ± 3% 51.2µs ± 2% ~ (p=0.631 n=10+10) CancelTree/depth=100/Root=OpenCanceler-12 60.8µs ± 1% 61.6µs ± 4% ~ (p=0.274 n=8+10) CancelTree/depth=100/Root=ClosedCanceler-12 19.3µs ± 2% 19.0µs ± 3% ~ (p=0.123 n=10+10) CancelTree/depth=1000/Root=Background-12 504µs ± 4% 512µs ± 4% ~ (p=0.123 n=10+10) CancelTree/depth=1000/Root=OpenCanceler-12 615µs ± 6% 619µs ± 4% ~ (p=1.000 n=10+10) CancelTree/depth=1000/Root=ClosedCanceler-12 190µs ± 2% 192µs ± 3% ~ (p=0.190 n=9+9) CheckCanceled/Err-12 12.1ns ± 2% 12.1ns ± 2% ~ (p=0.615 n=10+10) CheckCanceled/Done-12 7.27ns ± 1% 7.26ns ± 1% ~ (p=0.698 n=10+10) ContextCancelDone-12 1.03ns ± 1% 1.03ns ± 1% ~ (p=0.474 n=9+9) DeepValueNewGoRoutine/depth=10-12 1.02µs ± 3% 0.99µs ± 2% -3.41% (p=0.000 n=10+10) DeepValueNewGoRoutine/depth=20-12 1.11µs ± 3% 1.08µs ± 2% -2.51% (p=0.004 n=10+10) DeepValueNewGoRoutine/depth=30-12 5.55µs ±10% 1.17µs ± 4% -78.91% (p=0.000 n=10+10) DeepValueNewGoRoutine/depth=50-12 5.70µs ±13% 1.35µs ± 2% -76.31% (p=0.000 n=10+10) DeepValueNewGoRoutine/depth=100-12 9.69µs ± 4% 1.82µs ± 2% -81.18% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=10-12 54.2ns ± 2% 46.8ns ± 2% -13.71% (p=0.000 n=9+9) DeepValueSameGoRoutine/depth=20-12 109ns ± 2% 97ns ± 2% -11.11% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=30-12 155ns ± 3% 140ns ± 1% -9.49% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=50-12 256ns ± 2% 226ns ± 2% -11.83% (p=0.000 n=10+10) DeepValueSameGoRoutine/depth=100-12 492ns ± 3% 442ns ± 1% -10.15% (p=0.000 n=10+10) ``` Fixes #47292 Change-Id: I6bdeb234c979fb8fd6bfb91fd345cb5038f52c75 Reviewed-on: https://go-review.googlesource.com/c/go/+/335790 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Damien Neil <dneil@google.com>
2021-04-10all: fix spellingsNaman Gera
This follows the spelling choices that the Go project has made for English words. https://github.com/golang/go/wiki/Spelling Change-Id: Ie7c586d2cf23020cb492cfff58c0831d2d8d3a78 GitHub-Last-Rev: e16a32cd225a275f73d236bcb33703986d110ded GitHub-Pull-Request: golang/go#45442 Reviewed-on: https://go-review.googlesource.com/c/go/+/308291 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
2021-02-24context: avoid importing context package twiceKevin Burke
Change-Id: Id0a127e080dda8ee62738922c6de8caf3719dd68 Reviewed-on: https://go-review.googlesource.com/c/go/+/295949 Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Trust: Josh Bleecher Snyder <josharian@gmail.com> Trust: Kevin Burke <kev@inburke.com> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-02-24context: reduce contention in cancelCtx.DoneJosh Bleecher Snyder
Use an atomic.Value to hold the done channel. Conveniently, we have a mutex handy to coordinate writes to it. name old time/op new time/op delta ContextCancelDone-8 67.5ns ±10% 2.2ns ±11% -96.74% (p=0.000 n=30+28) Fixes #42564 Change-Id: I5d72e0e87fb221d4e230209e5fb4698bea4053c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/288193 Trust: Josh Bleecher Snyder <josharian@gmail.com> Trust: Sameer Ajmani <sameer@golang.org> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-02-23context: fix XTestInterlockedCancelsKeiichi Hirobe
The test does not use Done channel, so fix that. Change-Id: I795feab2e95de815b8b6ee7a7fd90f19f7af7db1 Reviewed-on: https://go-review.googlesource.com/c/go/+/294749 Reviewed-by: Sameer Ajmani <sameer@golang.org> Trust: Sameer Ajmani <sameer@golang.org> Trust: Cody Oss <codyoss@google.com> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2020-03-31context: fix a flaky timeout in TestLayersTimeoutBryan C. Mills
In CL 223019, I reduced the short timeout in the testLayers helper to be even shorter than it was. That exposed a racy (time-dependent) select later in the function, which failed in one of the slower builders (android-386-emu). Also streamline the test to make it easier to test with a very high -count flag: - Run tests that sleep for shortDuration in parallel to reduce latency. - Use shorter durations in examples to reduce test running time. - Avoid mutating global state (in package math/rand) in testLayers. After this change (but not before it), 'go test -run=TestLayersTimeout -count=100000 context' passes on my workstation. Fixes #38161 Change-Id: Iaf4abe7ac308b2100d8828267cda9f4f8ae4be82 Reviewed-on: https://go-review.googlesource.com/c/go/+/226457 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-18context: prevent creation of invalid contextsKyle Nusbaum
This commit makes it impossible to create derived contexts with nil parents. Previously it was possible to create derived contexts with nil parents, and invalid contexts could propogate through the program. Eventually this can cause a panic downstream, which is difficult to trace back to the source of the error. Although `WithCancel` and `WithDeadline` already panic if `parent` is `nil`, this adds explicit checks to give a useful message in the panic. Fixes #37908 Change-Id: I70fd01f6539c1b0da0e775fc5457e32e7075e52c GitHub-Last-Rev: 1b7dadd7db9ba42952644ad5e9a49591d6a5191f GitHub-Pull-Request: golang/go#37898 Reviewed-on: https://go-review.googlesource.com/c/go/+/223777 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-13context: deflake time-sensitive testsBryan C. Mills
Many of tests in this package assumed reasonable scheduling latency. Unfortunately, scheduling latency on builders and CI systems is not always reasonable. Rather than expecting that a timeout is detected within a fixed short interval, we can use (*testing.T).Deadline to portably scale the time we're willing to wait to something appropriate to the builder. Some of the tests also included arbitrary-duration sleeps, which are no longer needed after CL 196521; we can remove those instead of extending them. Promptness of timeouts is also an important property, but testing that property is better suited to benchmarks than to tests proper: unlike tests, we generally expect benchmarks to be run in a quiet, low-contention environment. Fixes #13956 Change-Id: I0797e2267fb778c8ad94add56d797de9e2c885e5 Reviewed-on: https://go-review.googlesource.com/c/go/+/223019 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-29context: mark testDeadline as a testing helper methodIan Lance Taylor
Change-Id: Ie6fc3e9789aea6e5949e66186db6f2b071b6fdff Reviewed-on: https://go-review.googlesource.com/c/go/+/204037 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>