summaryrefslogtreecommitdiff
path: root/src/context
Commit message (Collapse)AuthorAgeFilesLines
* context: define behavior for Err before Done is closedRuss Cox2017-04-261-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* all: fix minor misspellingsEric Lagergren2017-04-031-1/+1
| | | | | | | | Change-Id: I1f1cfb161640eb8756fb1a283892d06b30b7a8fa Reviewed-on: https://go-review.googlesource.com/39356 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* context: document that Err is unspecified before DoneIan Lance Taylor2017-02-221-0/+1
| | | | | | | | | | | | | | 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>
* context: lazily initialize cancelCtx done channelJosh Bleecher Snyder2017-02-011-9/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* all: make spelling consistentBrad Fitzpatrick2016-12-081-2/+2
| | | | | | | | | | Fixes #17938 Change-Id: Iad12155f4976846bd4a9a53869f89e40e5b3deb3 Reviewed-on: https://go-review.googlesource.com/34147 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
* context: document appropriate WithValue key type moreBrad Fitzpatrick2016-11-121-3/+7
| | | | | | | | | Fixes #17826 Updates #17302 Change-Id: I7c1ebd965e679e7169a97e62d27ae3ede2473aa1 Reviewed-on: https://go-review.googlesource.com/33152 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* context: adjust tests to avoid importing "testing" in package contextRuss Cox2016-11-033-56/+112
| | | | | | | | | | | | So that testing can use context in its public API. For #16221. Change-Id: I6263fa7266c336c9490f20164ce79336df44a57e Reviewed-on: https://go-review.googlesource.com/32648 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* context: add comments to the WithCancel example, apply minor improvementsJaana Burcu Dogan2016-10-261-16/+23
| | | | | | | | | | Fixes #17534. Change-Id: I28af74b287a5a09d5f6607a012f3d5d133b04ed2 Reviewed-on: https://go-review.googlesource.com/32017 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* context: make DeadlineExceeded implement net.ErrorRuss Cox2016-10-052-3/+24
| | | | | | | | | | | | | 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>
* context: discourage use of basic types as keys in WithValueMatt Layher2016-09-301-1/+3
| | | | | | | | Fixes #17302 Change-Id: I375d5d4f2714ff415542f4fe56a548e53c5e8ba6 Reviewed-on: https://go-review.googlesource.com/30134 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* context: add examplesCarlos C2016-09-092-33/+109
| | | | | | | | | | | | Add function level examples to the package. Partially addresses #16360 Change-Id: I7162aed4e4a969743c19b79c9ffaf9217d2c1c08 Reviewed-on: https://go-review.googlesource.com/26930 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* context: reduce memory usage of context treeJack Lindamood2016-09-092-6/+44
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* all: use time.Until where applicableBrad Fitzpatrick2016-08-301-2/+2
| | | | | | | | | | | 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>
* context: test WithCancel with canceled parentJaana Burcu Dogan2016-08-191-0/+15
| | | | | | | | Change-Id: I32079cc12cfffb8520f0073a8b5119705dc0cd1b Reviewed-on: https://go-review.googlesource.com/27401 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* context: add test for WithDeadline in the pastJack Lindamood2016-07-261-0/+6
| | | | | | | | | | | | Adds a test case for calling context.WithDeadline() where the deadline exists in the past. This change increases the code coverage of the context package. Change-Id: Ib486bf6157e779fafd9dab2b7364cdb5a06be36e Reviewed-on: https://go-review.googlesource.com/25007 Reviewed-by: Sameer Ajmani <sameer@golang.org> Run-TryBot: Sameer Ajmani <sameer@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* context: cancel the context in ExampleWithTimeout, with explanationAlan Donovan2016-06-301-1/+9
| | | | | | | | Fixes #16230 Change-Id: Ibb10234a6c3ab8bd0cfd93c2ebe8cfa66f80f6b0 Reviewed-on: https://go-review.googlesource.com/24682 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* context: update documentation on cancelation and go vet check.Sameer Ajmani2016-06-241-35/+38
| | | | | | | | | 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>
* context: document how to release resources associated with Contexts.Sameer Ajmani2016-06-151-3/+11
| | | | | | | | | | | | | 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>
* context: fix typo in commentsKenny Grant2016-06-011-1/+1
| | | | | | | | 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>
* context: make DeadlineExceeded have a Timeout methodBrad Fitzpatrick2016-05-192-1/+19
| | | | | | | | | | 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>
* context: use https in docsShenghou Ma2016-05-051-2/+2
| | | | | | Change-Id: I9354712768702e3b083c77f30165a34cb414d686 Reviewed-on: https://go-review.googlesource.com/22810 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* context: produce a nicer panic message for a nil WithValue keyBrad Fitzpatrick2016-04-292-0/+7
| | | | | | | 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>
* context: fix doc typoBrad Fitzpatrick2016-04-271-1/+1
| | | | | | | | Fixes #15449 Change-Id: I8d84d076a05c56694b48f7b84f572b1a6524f522 Reviewed-on: https://go-review.googlesource.com/22493 Reviewed-by: Andrew Gerrand <adg@golang.org>
* context: attempt to deflake timing testsBrad Fitzpatrick2016-04-112-39/+30
| | | | | | | | | | | | | | | Passes on OpenBSD now when running it with -count=500. Presumably this will also fix the same problems seen on FreeBSD and Windows. Fixes #15158 Change-Id: I86451c901613dfa5ecff0c2ecc516527a3c011b3 Reviewed-on: https://go-review.googlesource.com/21840 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* context: document that WithValue's key must be comparableBrad Fitzpatrick2016-04-112-1/+20
| | | | | | | | | | | 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>
* context: disable more flaky tests on openbsdBrad Fitzpatrick2016-04-081-0/+3
| | | | | | | | | | Updates #15158 Change-Id: Icb3788152a7a5a9b0d56ea38da46d770ffdce413 Reviewed-on: https://go-review.googlesource.com/21763 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
* context: mark more tests as flaky on OpenBSDBrad Fitzpatrick2016-04-071-0/+6
| | | | | | | | | | Updates #15158 Change-Id: I53e9e68d36efbf52736822e6caa047cfff501283 Reviewed-on: https://go-review.googlesource.com/21653 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* net, runtime: skip flaky tests on OpenBSDBrad Fitzpatrick2016-04-061-0/+4
| | | | | | | | | | | | | | | | | Flaky tests are a distraction and cover up real problems. File bugs instead and mark them as flaky. This moves the net/http flaky test flagging mechanism to internal/testenv. Updates #15156 Updates #15157 Updates #15158 Change-Id: I0e561cd2a09c0dec369cd4ed93bc5a2b40233dfe Reviewed-on: https://go-review.googlesource.com/21614 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
* context: add the context package from golang.org/x/net/contextBrad Fitzpatrick2016-04-053-0/+1047
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>