aboutsummaryrefslogtreecommitdiff
path: root/src/context/context.go
AgeCommit message (Collapse)Author
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-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>
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>
2019-09-26context: use fewer goroutines in WithCancel/WithTimeoutRuss Cox
If the parent context passed to WithCancel or WithTimeout is a known context implementation (one created by this package), we attach the child to the parent by editing data structures directly; otherwise, for unknown parent implementations, we make a goroutine that watches for the parent to finish and propagates the cancellation. A common problem with this scheme, before this CL, is that users who write custom context implementations to manage their value sets cause WithCancel/WithTimeout to start goroutines that would have not been started before. This CL changes the way we map a parent context back to the underlying data structure. Instead of walking up through known context implementations to reach the *cancelCtx, we look up parent.Value(&cancelCtxKey) to return the innermost *cancelCtx, which we use if it matches parent.Done(). This way, a custom context implementation wrapping a *cancelCtx but not changing Done-ness (and not refusing to return wrapped keys) will not require a goroutine anymore in WithCancel/WithTimeout. For #28728. Change-Id: Idba2f435c81b19fe38d0dbf308458ca87c7381e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/196521 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-09-18context: mention asynchronous cancellation propagationEgon Elbre
Fixes #33185 Change-Id: I0adcffa5d1c9e55ae52309c59f961b0710166098 Reviewed-on: https://go-review.googlesource.com/c/go/+/187921 Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-02all: remove os.ErrTimeoutDamien Neil
It is unclear whether the current definition of os.IsTimeout is desirable or not. Drop ErrTimeout for now so we can consider adding it (or some other error) in a future release with a corrected definition. Fixes #33411 Change-Id: I8b880da7d22afc343a08339eb5f0efd1075ecafe Reviewed-on: https://go-review.googlesource.com/c/go/+/188758 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-01all: remove os.ErrTemporaryDamien Neil
As discussed in https://github.com/golang/go/issues/32463#issuecomment-506833421 the classification of deadline-based timeouts as "temporary" errors is a historical accident. I/O timeouts used to be duration-based, so they really were temporary--retrying a timed-out operation could succeed. Now that they're deadline-based, timeouts aren't temporary unless you reset the deadline. Drop ErrTemporary from Go 1.13, since its definition is wrong. We'll consider putting it back in Go 1.14 with a clear definition and deprecate net.OpError.Temporary. Fixes #32463 Change-Id: I70cda664590d8872541e17409a5780da76920891 Reviewed-on: https://go-review.googlesource.com/c/go/+/188398 Reviewed-by: Jonathan Amsterdam <jba@google.com>
2019-05-20context: document CancelFunc to be safe for simultaneous use by multiple ↵Alex Myasoedov
goroutines Fixes #32145 Change-Id: If4c9dd3a2af748974141ad6e571f80efcbaad772 Reviewed-on: https://go-review.googlesource.com/c/go/+/177899 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-04all: add Unwrap and Is methods to various error typesDamien Neil
Add Unwrap methods to types which wrap an underlying error: "encodinc/csv".ParseError "encoding/json".MarshalerError "net/http".transportReadFromServerError "net".OpError "net".DNSConfigError "net/url".Error "os/exec".Error "signal/internal/pty".PtyError "text/template".ExecError Add os.ErrTemporary. A case could be made for putting this error value in package net, since no exported error types in package os include a Temporary method. However, syscall errors returned from the os package do include this method. Add Is methods to error types with a Timeout or Temporary method, making errors.Is(err, os.Err{Timeout,Temporary}) equivalent to testing the corresponding method: "context".DeadlineExceeded "internal/poll".TimeoutError "net".adrinfoErrno "net".OpError "net".DNSError "net/http".httpError "net/http".tlsHandshakeTimeoutError "net/pipe".timeoutError "net/url".Error Updates #30322 Updates #29934 Change-Id: I409fb20c072ea39116ebfb8c7534d493483870dc Reviewed-on: https://go-review.googlesource.com/c/go/+/170037 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-04-16all: s/cancelation/cancellation/Josh Bleecher Snyder
Though there is variation in the spelling of canceled, cancellation is always spelled with a double l. Reference: https://www.grammarly.com/blog/canceled-vs-cancelled/ Change-Id: I240f1a297776c8e27e74f3eca566d2bc4c856f2f Reviewed-on: https://go-review.googlesource.com/c/go/+/170060 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-04-16context: simplify stringify with a type switch喜欢兰花山丘
Minor style change. Change-Id: Ib30243a71a83de1a67d3d005bfdd1e04265fca1e GitHub-Last-Rev: 9d654de10eaa6f01ece29790fb81bc41dfd61eaf GitHub-Pull-Request: golang/go#31479 Reviewed-on: https://go-review.googlesource.com/c/go/+/172199 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2019-03-27context: don't depend on fmtBrad Fitzpatrick
So the net package doesn't indirectly depend on unicode tables. But we're still not quite there, because a new test added in this CL reveals that we still have a path to unicode via: deps_test.go:570: TODO(issue 30440): policy violation: net => sort => reflect => unicode Updates #30440 Change-Id: I710c2061dfbaa8e866c92e6c824bd8df35784165 Reviewed-on: https://go-review.googlesource.com/c/go/+/169080 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-25context: remove dependency on reflectMichael Fraenkel
Make context depend on reflectlite instead of reflect in effort to eventually make net no longer depend on unicode tables. With this CL we're down to just: net -> context -> fmt -> unicode tables The next CL can remove context -> fmt. Updates #30440 Change-Id: I7f5df15f975d9dc862c59aa8477c1cfd6ff4967e Reviewed-on: https://go-review.googlesource.com/c/go/+/164239 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-03context: avoid duplicate removeChildWeerasak Chongnguluam
When deadline has already passed, current context is canceled before return cancel function. So is unnecessary to call cancel with remove from parent again in return cancel function. Change-Id: I37c687c57a29d9f139c7fb648ce7de69093ed623 Reviewed-on: https://go-review.googlesource.com/c/50410 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-08-22context: don't talk about tools that don't existDominik Honnef
This comment has been the source of much confusion and broken dreams. We can add it back if a tool ever gets released. Updates #16742 Change-Id: I4b9c179b7c60274e6ff1bcb607b82029dd9a893f Reviewed-on: https://go-review.googlesource.com/130876 Reviewed-by: Matt Layher <mdlayher@gmail.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-04-15context: avoid defer in the cancelCtx.Err methodKeegan Carruthers-Smith
name old time/op new time/op delta CheckCanceled/Err-4 53.5ns ± 2% 20.8ns ± 0% -61.05% (p=0.008 n=5+5) CheckCanceled/Done-4 44.4ns ± 1% 44.5ns ± 0% ~ (p=0.889 n=5+5) Change-Id: I2c68700a2b33f8feb3d307ce7c966590a3e960af Reviewed-on: https://go-review.googlesource.com/107137 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-09-21context: fix references to "d" in WithDeadline docsMichael Darakananda
Docs of WithDeadline refers to variable "d" which does not exist in the docs. This commit renames the time argument to "d" to make the doc work. Change-Id: Ifd2c1be7d2e3f7dfb21cd9bb8ff7fc5039c8d3bd Reviewed-on: https://go-review.googlesource.com/65130 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-08-30context: fix lint warning “drop = 0 from declaration”Michael Stapelberg
Previously, the suggested code would result in the following golint warning: “should drop = 0 from declaration of var errorsOnlyKey; it is the zero value” Change-Id: I1a302c1e40ca89acbc76897e39097ecd04865460 Reviewed-on: https://go-review.googlesource.com/60290 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-04-26context: define behavior for Err before Done is closedRuss Cox
The Context definition to date has not defined what Err returns before the Done channel is closed. Define that it returns nil, as most implementations do. All the standard context implementations (those in package context and in golang.org/x/net/context) return Err() == nil when Done is not yet closed. However, some non-standard implementations may exist that return Err() != nil in this case, as permitted by the Context definition before this date. Call these "errorful implementations". Because all the standard context implementations ensure that Err() == nil when Done is not yet closed, clients now exist that assume Err() != nil implies Done is closed and use calling Err as a quick short-circuit check instead of first doing a non-blocking receive from Done and then, if that succeeds, needing to call Err. This assumption holds for all the standard Context implementations, so these clients work fine in practice, even though they are making unwarranted assumptions about the Context implementations. Call these "technically incorrect clients". If a technically incorrect client encounters an errorful implementation, the client misbehaves. Because there are few errorful implementations, over time we expect that many clients will end up being technically incorrect without realizing it, leading to latent, subtle bugs. If we want to eliminate these latent, subtle bugs, there are two ways to do this: either make errorful implementations more common (exposing the client bugs more often) or redefine the Context interface so that the clients are not buggy after all. If we make errorful implementations more common, such as by changing the standard context implementations to return ErrNotDone instead of nil when Err is called before Done is closed, this will shake out essentially all of the technically incorrect clients, forcing people to find and fix those clients during the transition to Go 1.9. Technically this is allowed by the compatibility policy, but we expect there are many pieces of code assuming that Err() != nil means done, so updating will cause real pain. If instead we disallow errorful implementations, then they will need to be fixed as they are discovered, but the fault will officially lie in the errorful Context implementation, not in the clients. Technically this is disallowed by the compatibility policy, because these errorful implementations were "correct" in earlier versions of Go, except that they didn't work with common client code. We expect there are hardly any errorful implementations, so that disallowing them will be less disruptive and more in the spirit of the compatibility policy. This CL takes the path of expected least disruption, narrowing the Context interface semantics and potentially invalidating existing implementations. A survey of the go-corpus v0.01 turned up only five Context implementations, all trivial and none errorful (details in #19856). We are aware of one early Context implementation inside Google, from before even golang.org/x/net/context existed, that is errorful. The misbehavior of an open-source library when passed such a context is what prompted #19856. That context implementation would be disallowed after this CL and would need to be corrected. We are aware of no other affected context implementations. On the other hand, a survey of the go-corpus v0.01 turned up many instances of client code assuming that Err() == nil implies not done yet (details also in #19856). On balance, narrowing Context and thereby allowing Err() == nil checks should invalidate significantly less code than a push to flush out all the currently technically incorrect Err() == nil checks. If release feedback shows that we're wrong about this balance, we can roll back this CL and try again in Go 1.10. Fixes #19856. Change-Id: Id45d126fac70e1fcc42d73e5a87ca1b66935b831 Reviewed-on: https://go-review.googlesource.com/40291 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2017-02-22context: document that Err is unspecified before DoneIan Lance Taylor
It could have been defined the other way, but since the behavior has been unspecified, this is the conservative approach for people writing different implementations of the Context interface. Change-Id: I7334a4c674bc2330cca6874f7cac1eb0eaea3cff Reviewed-on: https://go-review.googlesource.com/37375 Reviewed-by: Matt Layher <mdlayher@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-02-01context: lazily initialize cancelCtx done channelJosh Bleecher Snyder
This CL reduces allocations when a context created with WithCancel either (1) never has its Done channel used or (2) gets cancelled before its Done channel is used This is not uncommon. Many contexts are created for tasks that end up not using them. name old time/op new time/op delta ContextCancelTree/depth=1/Root=Background-8 112ns ± 2% 74ns ± 1% -34.03% (p=0.000 n=17+18) ContextCancelTree/depth=1/Root=OpenCanceler-8 601ns ± 3% 544ns ± 1% -9.56% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=ClosedCanceler-8 367ns ± 4% 257ns ± 1% -30.01% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=Background-8 2.91µs ± 2% 2.87µs ± 0% -1.38% (p=0.000 n=20+18) ContextCancelTree/depth=10/Root=OpenCanceler-8 4.36µs ± 2% 4.26µs ± 1% -2.34% (p=0.000 n=20+18) ContextCancelTree/depth=10/Root=ClosedCanceler-8 2.02µs ± 2% 1.51µs ± 1% -25.18% (p=0.000 n=19+19) ContextCancelTree/depth=100/Root=Background-8 30.5µs ± 6% 30.5µs ± 1% ~ (p=0.941 n=20+20) ContextCancelTree/depth=100/Root=OpenCanceler-8 39.8µs ± 1% 41.1µs ± 1% +3.15% (p=0.000 n=18+19) ContextCancelTree/depth=100/Root=ClosedCanceler-8 17.8µs ± 1% 13.9µs ± 1% -21.61% (p=0.000 n=18+20) ContextCancelTree/depth=1000/Root=Background-8 302µs ± 1% 313µs ± 0% +3.62% (p=0.000 n=20+18) ContextCancelTree/depth=1000/Root=OpenCanceler-8 412µs ± 2% 427µs ± 1% +3.55% (p=0.000 n=18+19) ContextCancelTree/depth=1000/Root=ClosedCanceler-8 178µs ± 1% 139µs ± 1% -21.80% (p=0.000 n=19+17) name old alloc/op new alloc/op delta ContextCancelTree/depth=1/Root=Background-8 176B ± 0% 80B ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=OpenCanceler-8 544B ± 0% 448B ± 0% -17.65% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=ClosedCanceler-8 352B ± 0% 160B ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=Background-8 3.49kB ± 0% 3.39kB ± 0% -2.75% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=OpenCanceler-8 3.86kB ± 0% 3.76kB ± 0% -2.49% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=ClosedCanceler-8 1.94kB ± 0% 0.88kB ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=Background-8 36.6kB ± 0% 36.5kB ± 0% -0.26% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=OpenCanceler-8 37.0kB ± 0% 36.9kB ± 0% -0.26% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=ClosedCanceler-8 17.8kB ± 0% 8.1kB ± 0% -54.55% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=Background-8 368kB ± 0% 368kB ± 0% -0.03% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=OpenCanceler-8 368kB ± 0% 368kB ± 0% -0.03% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=ClosedCanceler-8 176kB ± 0% 80kB ± 0% -54.55% (p=0.000 n=20+20) name old allocs/op new allocs/op delta ContextCancelTree/depth=1/Root=Background-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=OpenCanceler-8 8.00 ± 0% 7.00 ± 0% -12.50% (p=0.000 n=20+20) ContextCancelTree/depth=1/Root=ClosedCanceler-8 6.00 ± 0% 4.00 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=Background-8 48.0 ± 0% 47.0 ± 0% -2.08% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=OpenCanceler-8 53.0 ± 0% 52.0 ± 0% -1.89% (p=0.000 n=20+20) ContextCancelTree/depth=10/Root=ClosedCanceler-8 33.0 ± 0% 22.0 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=Background-8 498 ± 0% 497 ± 0% -0.20% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=OpenCanceler-8 503 ± 0% 502 ± 0% -0.20% (p=0.000 n=20+20) ContextCancelTree/depth=100/Root=ClosedCanceler-8 303 ± 0% 202 ± 0% -33.33% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=Background-8 5.00k ± 0% 5.00k ± 0% -0.02% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=OpenCanceler-8 5.00k ± 0% 5.00k ± 0% -0.02% (p=0.000 n=20+20) ContextCancelTree/depth=1000/Root=ClosedCanceler-8 3.00k ± 0% 2.00k ± 0% -33.33% (p=0.000 n=20+20) Change-Id: Ibd7a0c3d5c847861cf1497f8fead34329413d26d Reviewed-on: https://go-review.googlesource.com/34979 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Sameer Ajmani <sameer@golang.org>
2016-11-12context: document appropriate WithValue key type moreBrad Fitzpatrick
Fixes #17826 Updates #17302 Change-Id: I7c1ebd965e679e7169a97e62d27ae3ede2473aa1 Reviewed-on: https://go-review.googlesource.com/33152 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-10-05context: make DeadlineExceeded implement net.ErrorRuss Cox
It already implemented the Timeout method, but implementing the full net.Error is more convenient. Fixes #14238 (again). Change-Id: Ia87f897f0f35bcb49865e2355964049227951ca6 Reviewed-on: https://go-review.googlesource.com/30370 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-30context: discourage use of basic types as keys in WithValueMatt Layher
Fixes #17302 Change-Id: I375d5d4f2714ff415542f4fe56a548e53c5e8ba6 Reviewed-on: https://go-review.googlesource.com/30134 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-09context: reduce memory usage of context treeJack Lindamood
Modifies context package to use map[]struct{} rather than map[]bool, since the map is intended as a set object. Also adds Benchmarks to the context package switching between different types of root nodes and a tree with different depths. Included below are bytes deltas between the old and new code, using these benchmarks. benchmark old bytes new bytes delta BenchmarkContextCancelTree/depth=1/Root=Background-8 176 176 +0.00% BenchmarkContextCancelTree/depth=1/Root=OpenCanceler-8 560 544 -2.86% BenchmarkContextCancelTree/depth=1/Root=ClosedCanceler-8 352 352 +0.00% BenchmarkContextCancelTree/depth=10/Root=Background-8 3632 3488 -3.96% BenchmarkContextCancelTree/depth=10/Root=OpenCanceler-8 4016 3856 -3.98% BenchmarkContextCancelTree/depth=10/Root=ClosedCanceler-8 1936 1936 +0.00% BenchmarkContextCancelTree/depth=100/Root=Background-8 38192 36608 -4.15% BenchmarkContextCancelTree/depth=100/Root=OpenCanceler-8 38576 36976 -4.15% BenchmarkContextCancelTree/depth=100/Root=ClosedCanceler-8 17776 17776 +0.00% BenchmarkContextCancelTree/depth=1000/Root=Background-8 383792 367808 -4.16% BenchmarkContextCancelTree/depth=1000/Root=OpenCanceler-8 384176 368176 -4.16% BenchmarkContextCancelTree/depth=1000/Root=ClosedCanceler-8 176176 176176 +0.00% Change-Id: I699ad704d9f7b461214e1651d24941927315b525 Reviewed-on: https://go-review.googlesource.com/25270 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2016-08-30all: use time.Until where applicableBrad Fitzpatrick
Updates #14595 Change-Id: Idf60b3004c7a0ebb59dd48389ab62c854069e09f Reviewed-on: https://go-review.googlesource.com/28073 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-06-24context: update documentation on cancelation and go vet check.Sameer Ajmani
Also replace double spaces after periods with single spaces. Change-Id: Iedaea47595c5ce64e7e8aa3a368f36d49061c555 Reviewed-on: https://go-review.googlesource.com/24431 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-06-15context: document how to release resources associated with Contexts.Sameer Ajmani
Some users don't realize that creating a Context with a CancelFunc attaches a subtree to the parent, and that that subtree is not released until the CancelFunc is called or the parent is canceled. Make this clear early in the package docs, so that people learning about this package have the right conceptual model. Change-Id: I7c77a546c19c3751dd1f3a5bc827ad106dd1afbf Reviewed-on: https://go-review.googlesource.com/24090 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-06-01context: fix typo in commentsKenny Grant
Change-Id: I41310ec88c889fda79d80eaf4a742a1000284f60 Reviewed-on: https://go-review.googlesource.com/23591 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-05-19context: make DeadlineExceeded have a Timeout methodBrad Fitzpatrick
Fixes #14238 Change-Id: I1538bfb5cfa63e36a89df1f6eb9f5a0dcafb6ce5 Reviewed-on: https://go-review.googlesource.com/23256 Reviewed-by: Dave Cheney <dave@cheney.net> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-05-05context: use https in docsShenghou Ma
Change-Id: I9354712768702e3b083c77f30165a34cb414d686 Reviewed-on: https://go-review.googlesource.com/22810 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-04-29context: produce a nicer panic message for a nil WithValue keyBrad Fitzpatrick
Change-Id: I2e8ae403622ba7131cadaba506100d79613183f1 Reviewed-on: https://go-review.googlesource.com/22601 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-04-27context: fix doc typoBrad Fitzpatrick
Fixes #15449 Change-Id: I8d84d076a05c56694b48f7b84f572b1a6524f522 Reviewed-on: https://go-review.googlesource.com/22493 Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-04-11context: document that WithValue's key must be comparableBrad Fitzpatrick
Also, check it and explode earlier, rather than cryptic failures later. Change-Id: I319a425f60e2bc9d005a187fbdbd153faa96411c Reviewed-on: https://go-review.googlesource.com/21799 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Minux Ma <minux@golang.org>
2016-04-05context: add the context package from golang.org/x/net/contextBrad Fitzpatrick
This copies the golang.org/x/net/context package to the standard library. It is imported from the x/net repo's git rev 1d9fd3b8333e (the most recent modified to x/net/context as of 2016-03-07). The corresponding change to x/net/context is in https://golang.org/cl/20347 Updates #14660 Change-Id: Ida14b1b7e115194d6218d9ac614548b9f41641cc Reviewed-on: https://go-review.googlesource.com/20346 Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>