proposal: context/v2: update context package
ianlancetaylor opened this issue · 58 comments
This issue is intended to cover ideas about updating the context package for Go 2.
- The current context package leads to stuttering in declarations:
ctx context.Context. - The current
Context.WithValuefunction accepts values of any types, which is easy to misuse by passing, say, a string rather than a value of some package-local type. - The name
Contextis confusing to some people, since the main use of contexts is cancelation of goroutines. - Context values are passed everywhere explicitly, which troubles some people. Some explicitness is clearly good, but can we make it simpler?
See also #27982.
I would add to that list:
context.WithValue(intentionally) obscures information about the actual inputs required by a function; for example, because those inputs are required by some method of some other parameter. If we're getting real parametricity, it would be preferable to make those requirements explicit at compile time.
The name Context is confusing to some people, since the main use of contexts is cancelation of goroutines.
Related #28017
I have struggled with performance issues in the past with package context, which were tied somewhat to the API. This led to one hack fix and contemplation of some ugly compiler changes (#18493). I’d like any re-think of context to see how lightweight we can make the package so that it can be used in very fine-grained contexts.
/cc @rogpeppe who I think had some ideas about the design of the API when it was first introduced.
See also #28279.
Perhaps have go return a one-buffer channel. Closing the channel would cancel the goroutine, and we could have semantics similar to #27982 to check for cancellation. Reading from the channel would read the value that the function returns.
The problem with this, is that there is no way to read multiple return values, which is a pretty big issue. Not quite sure about an elegant way to solve this without having tuple types built-in to the language.
Writing to the channel would unfortunately write to the channel, but if you do stupid things, you should expect stupid behavior. It doesn't have any real use except for breaking your own code. (And we can't have it return a read-only channel if we want to be able to close it)
Context of course would still exist, but it's primary purpose would be for goroutine-local values, rather than cancellation.
I feel that Context only partly solved the problem of cancellation, since there is no facility to wait until the thing we're cancelling, has been cancelled (e.g. wait for the cancelled goroutine to stop). I built a "stopper" for this use case: https://github.com/function61/gokit/blob/dc75639388d554c7f79ca7ec6c967436b6c8301c/stopper/stopper_test.go (disclaimer: I should've made "stopper" a hybrid composed of context - my design is far from good and I plan to improve it)
I like this comment: #21355 (comment)
A somewhat less invasive way to reduce the syntactic overhead of
Contextwould be to make it a built-in type (likeerror).That would at least cut the stutter by a third:
-func Foo(ctx context.Context) error { +func Foo(ctx context) error {
I think my only issue with it is that functions that take contexts also typically have a Context in the function name, ie FooContext(ctx context.Context) error would become FooContext(ctx context) error, and we don't save quite as much as we think we do. Either way cutting off the redundancy is nice.
@deanveloper any example of a FooContext function that actually have 'context' in the name? I haven't seen any of that myself. But context does spread all the way deep into call hierarchy which is sort of horrifying.
Partially copying my reply in #20280
As someone who has been writing Go for two months. I never really understood context, or had to understand it, even though I see it everywhere and use it everywhere, as required by the code standard to pass it down in call hierarchy as the first argument. Today I am refactoring some code that uses it, and wonder if I can get it removed, so I figured out what context is for for the first time.
It is not immediate obvious for a beginner to understand the purpose context is used for, or to understand that IO would like to use context for cancellation, instead of something else, or that context is a primary mechanism of cancellation at all...
It's simply not self-explanatory enough, and I don't think the terminology is consistent for a developer who also deals with concepts with the same name in a different language. e.g. writing C++ at the same time, I would think Context has server-lifetime instead of request-lifetime.
I think that makes it worthwhile for us to carefully explain what it is to help engineers / new learners understand, and make the package itself as clear as possible. e.g. If it is for request-scoped info, why is it package not named request/context, or maybe it should be concurrency/context? Or if it is really used in conjunction with goroutine, how come it is not part of the language feature?
[The part where I don't know what I am talking about]
I feel like some of the concepts were borrowed from the web, where the functionalities, e.g. canceling an asynchronous operation might be realized by a browser API, i.e. to implement the behavior on call stack alone might not be sufficient, and require invention like this which gets a bit confusing. If that's the reality, we should state the facts very clearly and be crystal clear about the limitation and potential confusions.
@deanveloper any example of a FooContext function that actually have 'context' in the name? I haven't seen any of that myself. But context does spread all the way deep into call hierarchy which is sort of horrifying.
Here are a few examples off the top of my head, you can probably make a grep which could find all instances
- https://godoc.org/database/sql/#Conn
- https://godoc.org/database/sql/driver/
- https://godoc.org/net/#Dialer.DialContext
Edit - Here's a third-party lib that I've used as well https://godoc.org/github.com/nlopes/slack
@deanveloper oh this has to do with providing two versions of everything that either use context or not, as Go doesn't support overloading? Yeh that might need to be solved at a different level :|
I think not supporting overloading is actually a good thing. Makes sure that code stays readable and encourages writing multiple functions for different actions (rather than using something like a boolean flag). Although it does lead to cases like this, unfortunately.
I don't think it has anything to do with overloading, rather I think it is so because the context package was introduced fairly recently, and due to Go's backwards compatibility there has to be separate mechanisms for incrementally improving code (DialContext() as in different function, or a new context field like in net/http). If the context package was there from the start, there would probably be just one net.Dial() that takes context as first arg.
Correct, but if Go did support function overloading, then a Dial function could have been written with a context as the first argument and not broken compatability.
It was a combination of adding features in later as well as having no support for overloading.
@deanveloper or library writers can adopt a currying approach https://golang.org/pkg/net/http/#Request.WithContext
I also meant that having 'Foo' and 'FooContext' doesn't have to do with overloading, but it's just something library writers have to deal with, rather than something that would impact any decision regarding context itself here.
It's simply not self-explanatory enough, and I don't think the terminology is consistent for a developer who also deals with concepts with the same name in a different language. e.g. writing C++ at the same time, I would think Context has server-lifetime instead of request-lifetime.
I agree. I think this is due to context being a combination of two completely separate concepts - WithValue aka the real "context" and cancellation. There're several blog posts written about this problem and the solution I would like Go to pursue is to split these concepts into separate types. Ideally I would like to WithValue to go away completely and leave only cancellation part of the context. After that it can be renamed to clearly state what it's for. We can look at CancellationToken from C# as an example.
Standard library have no use for WithValue at all. It should only accept cancellation tokens. Passing arbitrary values between calls might be useful but it should be limited to outside code. I don't even remember any standard library function that reads values from context. Is there any?
I don't even remember any standard library function that reads values from context. Is there any?
net/http is one such package. It inspects client request contexts for net/http/httptrace hooks, and calls them.
If we drop WithValue from context, what could be the transport for layers of middlewares?
The point isn't to drop WithValue from context, but rather to split context into two separate ideas.
As far as I see it, context is 2 things:
- A cancellation mechanism
- A store for global variables
Now, as far as cancellation goes, I believe that really belongs somehow connected to the thing that just might get cancelled. You can always easily cancel from within a go-routine (that's just a normal error return), but, today, you have to use context to cancel from outside a go-routine. The canonical place to put a cancel function, thusly, is something like:
cancel := go doSomething()
// if we for some reason want to not do that thing, then
cancel()Note: I would relatedly love a timeout added to sync.WaitGroup, but that's an issue for another issue.
As for a global store of variables, I think this only happened because context must be passed in to everywhere. If you really want to have a set of values that go everywhere with you, that's a nice sort of thing to construct intentionally. net/http uses the values, but it doesn't need to. It could have its own struct for this that's not awkwardly tied to other packages.
It's also slightly more: runtime/pprof.Do introduces labels (key, value string) which are context bound. Effectively it might be WithValue underneath (didn't check source), but it's use case is distinctly different than WithValue.
I think something along the lines of #21355 should be the way to go here:
Contexts pass values through intermediate layers of the call stack. Cancellation is one such value (in the form of the Done channel), but is not the only one and is not particularly different from any other value plumbed in this fashion.
The problem contexts aim to solve is not that of passing parameters to a function, but of passing parameters through a function that does not care about them.
The key point here being that the context could be retrieved from a stack frame (e.g. a stack frame should hold the context information). To expand a bit more concretely: runtime.Frame should have a Context field, and the runtime package api can be extended to work with the frame information in various ways:
- Get the last set context from available stack frames (e.g. from a parent N levels up),
- Set current frame context which will then be available for nested function calls,
- Get all frame contexts (I don't see a particular use here, but,...)
The semantics around context this way stay the same, as the context is only passed through the stack depth, but implicitly instead of explicitly via function APIs. When the express purpose of the context is cancellation, this doesn't mean that all the function of the call stack need to be context aware, as was already talked about.
To add a few other opinions:
- context.Context should have a Cancel() func, even if you explicitly have to get a Cancellable,
- I'm fine with keeping
context.Contextnotcontextdue to existing package practices (e.g. time.Time)
As for a global store of variables, I think this only happened because context must be passed in to everywhere.
It sadly makes passing interface{} through anything, to anything, incredibly easy.
I do wish restricting or eliminating that side effect was feasible, unfortunately its usage as a global parameter bag in the wild is rampant.
Typemap in WithValue like rust does with coming generic, https://github.com/reem/rust-typemap
@ianlancetaylor wrote:
- Context values are passed everywhere explicitly, which troubles some people. Some explicitness is clearly good, but can we make it simpler?
Most of the function/method calls I do nowadays have as first parameter ctx and so ctx is part of most of the frames on the call stacks I typically see.
=> Thought experiment: Why not make ctx an implicit part of the Go 2 call stack?
Implications:
- The call frame for
main()would implicitly containctx := context.Background(), which is justcontext.WithValuewithout any values (empty slate). - There should be a built-in function to annotate the current stack frame as replacement for
context.WithValue. - There should be a built-in function to read a previously made annotation as replacement for
ctx.Value(). Ideally we wouldn't need a type check anymore like in Go 1 but could directly use the returned value. - The annotations could be import-path specific so that two packages can't clash with their annotations. This would allow to make the type of the key under which the value is stored to be just a string and gives the user an easier to use key/value store semantic.
- Sometimes one has the need to call a function with a different
ctx, like the original context has timed out but you need to do cleanups that should have a strict timeout. For this we could introduce some sort ofdisownfunctionality to ignore previous annotations / start again with a clean slate. This could also be limited to spawning a new Go-routine. So on Go-routine launch there would be some way to decide if the existing annotations should be "inherited".
Pros:
- No more handing around of
ctx. That would be implicit as the annotations are part of the stack. - No more extra key type as it is currently required to avoid a clash with keys. Instead the key type could be just made string and this would be much closer to a key/value store semantic.
- Packages could define their own annotation handling functions to annotate the callers stack frame (requires respective built-in functions). With that you could replace something like
context.WithTimeoutwith atimeoutpackage that containstimeout.Set(time.Duration)to set a timeout andtimeout.Elapsed() boolto check if the timeout has been reached.
Cons:
- Stack frame annotations could be even more black magic than
contextis already and hence even harder to grasp for Go beginners. - Go routines could be more complicated as there is now possibly a decision to be made if the annotations should be "inherited".
Thoughts?
@michael-schaller if it is part of the call stack then it is GLS/TLS - which has already been rejected (incorrectly so IMO).
@robaho My proposal isn't based on thread-local storage (TLS) / goroutine-routine local storage (GLS). Similarly to passing ctx with every function call in Go 1 (and hence being part of every stack frame) my proposal would implicitly add ctx to every stack frame in Go 2. TLS/GLS on the other side is independent of the stack state and is only bound to the respective thread/goroutine.
Also just to clarify, I'm just trying to give food for thought on this topic. If my proposal is rejected then that is IMHO a win as it also brings us closer to an answer what to do with context in Go 2. ;-)
@michael-schaller the internals are different but the net effect is the same as GLS/TLS - that you have go routine scoped variables that are implicitly passed across function boundaries - that is - routine scoped. Not function call scoped. That is GLS/TLS.
@robaho I'm confused. context in Go 1 isn't GLS and my proposal for Go 2 is very similar to Go 1's context. The essentially difference is that ctx isn't passed on explicitly anymore and instead implicitly. So I'm really confused how you think that the net effect is GLS.
GLS is like global variables, just that they are only accessible by a single goroutine. Also if you change a gouroutine-local variable then it changes for the whole call stack of this goroutine.
ctx on the other side is something that you pass on, even across goroutines. Furthermore you can't change an existing context, you can just create new ones. Either blank ones (context.Background()) or extended/annotated ones (context.With*). That means particularly that a ctx further down the call stack can't affect/alter previous contexts.
@michael-schaller if it is not explicitly passed as a parameter it has the same semantics as TLS/GLS since almost all uses of context - except for cancellation - requires creating a new one to hold the request state (or in the case of pprof labels) - which brings the same name collisions / global namespace issues. But I am willing to be that most framework uses of context.Context probably store a map in the Context, so they only have a single Context instance per routine, and then modify the map, rather than creating a new Context using WithValue - this is the only way to pass information back up to higher-level monitoring code.
For those interested, I suggest looking at ThreadLocal in Java. A difference being that because of generics it is type-safe, but the important thing is that 99+% of all usages are in frameworks or to avoid concurrency penalties when sharing expensive to create objects. Go's context.Context does not address the latter use case (nor really the first very well).
@robaho Thanks for the explanation. I think I understand now what you were hinting at. Let me try to rephrase it in my words. Please correct me if I got any of this wrong (again). ;-)
GLS (if it would exist in Go) would be available to the whole stack of a goroutine and hence it could affect all function calls of that goroutine, similarly to how a global variable could affect all function calls of a process. My proposal is similar as it could affect the whole stack after an annotation has been made. So annotations made in the very beginning of a goroutine would be effectively GLS. To make matters worse annotations that have been inherited by a new goroutine would be a GLS that isn't limited to a single goroutine anymore.
On the other side with context in Go 1 the programmer always has full control over the provided ctx and how it is passed on. However as ctx in Go 1 is typically just passed on as is or is extended/annotated with one of the context.With* functions means that context in Go 1 has a very similar problem as a ctx created for a new goroutine is effectively GLS for this goroutine. To make matters worse a ctx passed on to any new goroutines is also GLS for that goroutine, which again means that this kind of GLS isn't limited to a single goroutine anymore.
@michael-schaller that is generally it. To be specific, the arguments against the current Context design, is that:
- It is an untyped global namespace
- No way to propagate up except storing a map in the context at a higher level
- No way to cache expensive objects without concurrency access penalties
- Documenting Context use is difficult (related to 1)
Even the “always add to the function signature” only serves to create a very poor GLS and lots of noise.
One other point to add, in the broad case things are more difficult in Go due to the easy concurrency (similar to using multiple worker pools in handling different stages of a single request in Java) so using GLS for “request state” is not trivial either. It may be that a first class “improved Context” designed for this specific case would be a great language feature.
No way to propagate up except storing a map in the context at a higher level
I don't think context package should make that easier. That's clearly a design flaw with the application, not context package.
so using GLS for “request state” is not trivial either.
That's exactly why context should be passed explicitly. GLS doesn't cover the same use cases that context covers. Context spans logical scopes (which consist of any number of goroutines), not goroutine or function scopes.
Problem:
In large production applications (a web service in my case), it is unnecessarily difficult to diagnose context.Canceled errors.
Even with diligent error wrapping, the end result is often a string, like
handle request: perform business logic: talk to database: context canceled
There are many potential causes for this context cancelation. Some of them are normal operating procedure, and others require intervention. For example, it could be the request is complete, or a custom-built timeout, or perhaps a graceful application shutdown.
In general, if a context wrapped with multiple WithCancels (or WithTimeouts), the cause for context canceled (or deadline exceeded) is ambiguous without additional logging.
Solution:
I would like to propose the addition of explicit errors to supplement context.Canceled and context.DeadlineExceeded.
With errors (nee xerrors), I imagine it would be useful to implement Unwrap, such that the error Is both Canceled and the custom error.
Here's a strawman to demonstrate the concept:
package example
import (
"context"
"errors"
"fmt"
)
var ErrSomethingSpecific = fmt.Errorf("something specific happened")
func Example() {
ctx, cancel := context.WithCancel(context.Background(), ErrSomethingSpecific)
cancel()
fmt.Println(errors.Is(ctx.Err(), context.Canceled))
fmt.Println(errors.Is(ctx.Err(), ErrSomethingSpecific))
// Output:
// true
// true
}I got frustrated with that as well and have been using https://github.com/OneOfOne/bctx/blob/master/err.go in local and work projects.
For passing data, perhaps it would be better to use a builtin function to access the data. So instead of a global context variable (which would have .WithValue and .Value), there would instead be addcontext(value) and getcontext(Type) (value, bool) builtins. This would reduce a lot of the namespace bloat with xxxContext functions and having context as the first parameter in every function. Contexts that were added in a function call are removed when the function exits. For instance:
type userContext struct {
currentUser string
}
func main() {
printUser()
withUser()
printUser()
}
func withUser() {
addcontext(userContext{ "dean" })
printUser()
}
func printUser() {
if user, ok := getcontext(userContext); ok {
fmt.Println("user:", user.currentUser);
} else {
fmt.Println("no user")
}
}
// output:
// no user
// user: dean
// no user
Note that this also makes contexts much more type safe, which is a huge benefit since current contexts have the type safety of a map[interface{}]interface{}.
This still doesn't feel like the best solution. But hopefully this could inspire another idea that's even better. Perhaps requiring removing context explicitly with defer. Or, context is added when calling a function (ie withcontext(context, functioncall())).
===== EDIT =====
I like the withcontext example which I had brought up in the last paragraph, so I thought I would rewrite my last example using withcontext instead of addcontext:
type userContext struct {
currentUser string
}
func main() {
printUser()
withcontext(userContext{ "dean" }, printUser())
printUser()
}
func printUser() {
if user, ok := getcontext(userContext); ok {
fmt.Println("user:", user.currentUser);
} else {
fmt.Println("no user")
}
}
// output:
// no user
// user: dean
// no user
I'd like to write a more formal proposal for what I had written above, although it seems that people don't like it. Some constructive feedback would be much appreciated. In my eyes, using builtin functions and using types as keys accomplishes three things:
- Keys are now guaranteed to be unique. In the "context" package, it is even recommended that one already uses custom types for keys to avoid any namespace collision. (granted, these custom types could use enum values as keys which may be more elegant).
- Type safety. Because we now know the type of the context, we have type safety over the context's parameters.
- "Context bloat" where many functions begin with
xxxContext(ctx context.Context, ...), as the contexts are passed through builtins rather than through function parameters.
The goal of what was written above is strictly for the .WithValue side of contexts. This would also be a backwards-compatible change (minus some minor tooling changes to add additional builtin functions). It is likely also possible to add the "cancellation" side of contexts into the withcontext/getcontext proposal as well with a relatively simple package.
@deanveloper All attempts at making context be goroutine-local state, or implicitly passed on the stack, suffer from the same issues: There are functions that need to deal with more than one context, either merging or splitting them as needed. Please read the earlier discussion using those terms. And please do write up a proposal that takes into account the whole challenge, if you think your idea is actually different from the above. And I would personally say please also make that a separate issue and link it here. And note that goroutine-local state as a concept has been firmly rejected in the past.
Not only merging or splitting. The problem is implicit passing itself. Go is not that kind of language and people are generally opposed to that (myself included). It's much preferred that context is passed explicitly.
The problem is implicit passing itself
Explicit passing is part of the problem this discussion exists though - this is what I mean by "context bloat" where we have every function starting with ctx context.Context as the first argument. If I have functions a, b, c, d, and e, where a provides a context and e consumes from it, then b, c, and d all need to have ctx context.Context as arguments, even if they don't use it themselves.
This also makes refactoring difficult. Imagine if the 5 functions above weren't initially intended to need contexts, but then after some later development, it's decided that e needs to consume from a for whatever reason. Now, contexts need to be added to the signatures of the latter 4 functions, even though in reality only 2 functions are even changing (a providing data and e consuming it)
Go is not that kind of language
I beg to differ - Go likes to be explicit until things get too verbose. Specifying every single function with ctx context.Context is distracting and annoying.
Think back to Java/C++ where the type of the variable is always listed with the variable itself. This got distracting, and was needlessly verbose. Both languages have since added features to remove this, and Go of course has := which also removes the needless verbosity of specifying the type of a variable.
The reason that Go is so great (in my opinion) is that it's simple. Go isn't the kind to do everything explicit - Go is the kind to do everything simple. Explicitly usually correlates with simplicity, but I am arguing that it doesn't in this case, in the same way that other features in Go such as variable type inference and garbage collection do.
There are functions that need to deal with more than one context, either merging or splitting them as needed
Sorry, I should have been more clear with the example I had shown. This method of using contexts also allows for more than one context at a time, using the type of the context as a key. However my proposal doesn't support having 2 of the same type of context at the same time, but that is relatively easy to implement by simply declaring a new type with the old type as its underlying type.
My apologies, I misunderstood this. This does imply a single context.Context passed per-function, however I think the drawback is worth it. I would also consider these as edge cases personally, and say that if splitting/merging of multiple contexts needs to be done, then using explicit context passing may be better. My proposal targets the main use-case of using contexts, which is passing from a to e without concerning b, c, and d. I don't want to solve more complicated problems like splitting and merging contexts with magical builtins.
Please read the earlier discussion using those terms
I've been pretty involved in the discussion (minus the many comments discussing if the other proposed method was considered GLS or not). This proposal is mostly an addition to the one proposed earlier, although I believe it highlights the benefits and drawbacks a lot better with examples. If you are referencing this comment, I believe that 2 is a non-issue, contexts should be unidirectional. 3 is going to be hard to solve no matter what. However, this solution solves both 1 and 4 perfectly, as it is no longer untyped, and keys are namespaced by package (both of these solve 1), and because types are keys, documentation is easy to write on the provider side, and read on the consumer side (solving 4).
And note that goroutine-local state as a concept has been firmly rejected in the past.
This is passed on the stack the same way that context.Context currently is, except implicitly now instead of explicitly.
I'll write the proposal and link it here. Thank you both for the feedback, I will also be addressing it in the proposal when it is written.
Explicit passing is part of the problem this discussion exists though
Well, not really. The problem is verbosity and stutter (ctx context.Context is a mouthful) and explicitness is clearly called out as desirable. That part of the issue could be fixed by making context built-in, for example.
This also makes refactoring difficult.
Now imagine that contexts are implicit. Easy refactoring is impossible because context is entirely runtime concept. Compiler can't help you with it. But with explicit contexts adding or removing context argument clearly shows which call sites are affected. Implicit parameters are prone to hidden errors that can only be found at runtime either in tests or in production. That's actually one of the problems with current context implementation - it can carry arbitrary values that, essentially, become implicit function arguments. It's very hard to track down bugs around them.
That's why explicitness is very important. Context clearly demonstrates function behavior. It's main usage is usually cancellation and it's very important that functions can explicitly tell that they support cancellation by accepting context argument. With implicit passing the only way to provide such intent is by documenting it for every function that expects context. Essentially, we're moving one of the function arguments into the comments. And we all know why this is bad.
That brings us to why Go is not that kind of language. To save a few characters we're trading significant advantages that help with code maintainability. Go always puts that above anything else. And I don't think var/auto/:= example is relevant here. It removes verbosity but doesn't have any significant drawbacks because these languages are statically typed.
I would still like to see a more concrete proposal but I just think in its current state it would be heavily downvoted. Any kind of implicitness is a tough sell for Go. There's been too many examples of that.
It's main usage is usually cancellation and it's very important that functions can explicitly tell that they support cancellation by accepting context argument
This is not how a function says that they support cancellation. A function should state that it supports cancellation by documenting that it supports cancellation. The only thing that accepting a context states is that it may take some arbitrary data as input. For instance in the databse/sql package:
// BeginTx starts a transaction.
//
// The provided context is used until the transaction is committed or rolled back.
// If the context is canceled, the sql package will roll back
// the transaction. Tx.Commit will return an error if the context provided to
// BeginTx is canceled.
// ...
Also, perhaps my function doesn't support cancellation on its own, but its callees do. Now, my callees can return a cancellation error which propagates up the stack without the middle-men needing to support cancellation themselves.
Easy refactoring is impossible because context is entirely runtime concept. Compiler can't help you with it.
This point is somewhat fair. But it depends on what you mean by "easy refactoring", which isn't strictly defined.
Easy refactoring could be talking about only needing to change 2 lines in the 2 places that you're concerned about, rather than 9 lines potentially scattered around several files. (9 lines: 2 lines for every function (the arguments and the next function call), minus 1 line because a() doesn't need to change it's signature).
However, easy refactoring could also mean type-safe-refactoring, which allows us to
To save a few characters we're trading significant advantages that help with code maintainability. Go always puts that above anything else.
This isn't about saving a few characters, this is about reducing the amount of bloat on many functions that are written.
And I don't think var/auto/:= example is relevant here. It removes verbosity but doesn't have any significant drawbacks because these languages are statically typed.
It's actually very relevant, it's almost the exact same issue. Languages like Java/C++ took so long to implement var/auto. This isn't because they couldn't (at least in later years), they didn't implement var/auto because they didn't want to. They thought that keeping the type along with the variable was verbose, but that verbosity made things significantly clearer. However after languages like Swift, Go, Kotlin, Rust, and several others, they saw that removing the verbosity of explicitly defining every variable's type actually made code easier to read. My argument is that passing a context into nearly every function is the same way.
I much appreciate that Go is a verbose language. But in some applications, it is extremely useful for contexts to be passed implicitly rather than explicitly.
As I was writing a formal proposal for this, it just kept getting more and more cumbersome. Adding more builtin functions, describing how the contexts get passed, it just gets worse and worse. Ideally the best change (imo) would be to bake cancelling into the language, keep "contextual data" the way it is, and adding some kind of TypeMap to context.Context to help with type-safety and documentation of contextual values. Maybe if context is added as a built-in type, it wouldn't include cancellation, and cancellation could be baked in.
How about stack-local storage with explicit 'context parameters'
When functions want to explicitly handle context, give them a context parameter. Functions without context parameters will recursively pass along any 'context-paramters' onto functions they call.
Parameters are matched by type (to avoid parameter name/position clashes) so there is only one possible context.Context 'context-parameter'. Can easily be overiden, trivial to deal with multiple contexts (as you can still pass them to a function as a normal parameter). Still explicit when it matters. Works with methods and generics. Prevents context poisoning and is flexible to different context types.
//DoSomething has a context parameter.
//any functions that 'DoSomething' calls will transparently & recursively
//pass ctx along to any functions with an explicit context paramter.
func{ctx context.Context} DoSomethingWithContext() {
DoSomethingWithoutContext() //automatically passes ctx to this function.
DoAnotherThingWithContext() //automatically passes ctx to this function.
DoAnotherThingWithContext{context.Background()}() //override the passed context with a different context.
}
func{ctx context.Context} DoAnotherThingWithContext() {}
func DoSomethingWithoutContext() {
DoAnotherThingWithContext{context.TODO()}() //override the passed context with a different context.
DoAnotherThingWithContext() //passes the implicit context provided by the caller.
}context.Value is no longer needed as packages can declare their own private 'context types' and pass them as type-safe 'context paramaters' across API boundaries.
ie.
type myValue string
func{val myValue} CheckMyValue() {
fmt.Println(val)
}
//DoSomething could be defined in a different package.
func DoSomething(do func()) {
do()
}
//Prints Hello World
func main() {
DoSomething{myValue("Hello World")}(CheckMyValue)
}Compiler can optimise away the passing of the 'context parameter' when it knows no functions that can handle the parameter are ever called.
I feel like I would like this better if it didn’t mean that Go could have 3 potential parameter lists for a function (after type parameters are added)
edit - also, what happens if the context hadn’t been provided?
I guess I personally don’t see the benefit of adding a language feature for contexts when they aren’t widely used outside of cancellation.
A much better idea (in my eyes) would be to bake cancellation into the language, and improve context.Context with a TypeMap (after generics)
what happens if the context hadn’t been provided?
It is read as an empty value (nil).
bake cancellation into the language
What does this mean? What does this look like?
It is read as an empty value (nil).
How would I differentiate wanting to pass an empty value vs the value not being provided at all? Presumably if I have a type CurrentUserID int, I would want to tell the difference between the ID being 0 and the ID not being provided. The only workaround in this solution seems to be either passing a pointer to an int, or to pass a struct { CurrentUserID int, Ok bool }. Both of these solutions are a bit clunky to me.
What does this mean? What does this look like?
This means to have cancellation as a native feature in Go. I’m not sure what it would look like, I (and hopefully others) should think about that.
Cancellation as a native feature in Go could be implemented in many ways.
The simplest form could be like the current context just without the context.WithValue support. But the question is really why we would bother with that as then we might as well just use a channel like we did in the pre-context days to signal cancellation.
A different idea would be to allow go routine management like the OS allows signalling between processes, which would mean that we would have some way to send a termination signal to a go routine from another go routine. Such a signal implementation would be very different fromcontext though as a ctx propagates through the call chain and hence it is easy to cancel everything the ctx propagated to. With signals this would be different as sending a termination signal to a go routine would only affect this go routine unless that signal would automatically also propagate to all go routines that have been spawned from that go routine (similar to signaling process groups on the OS side). Another open question is also how a go routine would handle a termination signal. There could be a runtime function to check if a go routine has received a termination signal or the runtime could cause a panic in the go routine on termination signal.
I'm sure there are even more options on how to implement go routine cancellation natively in Go. The question for me is really if it is worth to implement go routine cancellation as a standalone feature in Go and if that cancellation support would be as versatile and relatively easy to understand as what we currently have with context. Particularly the propagation strictly down the call chain that we currently have with context could be easily lost and IMHO that is one of the greatest strengths of context. Currently it allows to cancel a whole call tree (tree because of go routines) while still leaving the programmer in control of how the context is propagated and how the cancellation is done.
But the question is really why we would bother with that as then we might as well just use a channel like we did in the pre-
contextdays to signal cancellation.
context.Context without context.WithValue() has three main benefits over just using a chan struct{}:
- Doesn't panic on a secondary cancellation without needing to do anything extra manually.
- Allows for simple handling of timeouts as well as manual cancellations.
- Has a tree hierarchy to it that allows a sub-context that can be canceled separately from the parent but also cancels if the parent gets canceled.
The first and second are relatively easy to do manually with a chan struct{} combined with a sync.Once and time.Timer(), but the third is quite a bit more complicated and extremely useful. I think that that alone warrants the existence of context.Context.
- The current context package leads to stuttering in declarations: ctx context.Context.
- Context values are passed everywhere explicitly, which troubles some people. Some explicitness is clearly good, but can we make it simpler?
https://golang.org/ref/spec#Function_types
Consider a breaking language change, a shorthand for ParameterDecl. Today we live with
package main
import (
"context"
"fmt"
)
type ctx = context.Context
func getname(ctx ctx) (string, bool) {
u, ok := ctx.Value("namekey").(string)
return u, ok
}
func main() {
ctx := context.WithValue(context.Background(), "namekey", "goku")
u, ok := getname(ctx)
if !ok {
panic("no user")
}
fmt.Println("user is", u)
}what if we had
func getname(ctx) (string, bool) {
u, ok := ctx.Value("namekey").(string)
return u, ok
}at our disposal, all else being equal to the first code block?
ParameterDecl = [ IdentifierList ] [ "..." ] Type .
vs
ParameterDecl = IdentifierList [ "..." ] Type | identifier .
For the stuttering, around the time the context library was created, I created a library with similar functionality with a context type called execution.Context
If we make it so that every function on a single goroutine share an implicit execution context, the problem is going to be about how to propagate changes to this execution context down the call chain. For instance if we want to modify the interface by wrapping it.
I suspect that explicitness as is done nowadays, might be the most practical way.
I'm currently working on a proposal:
- Allowing a
cancelfunction to be returned bygo myFunc() - Adding a new built-in function,
cancelled() <-chan structwhich allows us to tell when the current goroutine has been cancelled - A new struct,
context.Data, which is an immutable datastore very similar tocontext.Context, and uses a typemap for storage. Unfortunately because type parameters may not exist on methods, it may be a bit strange to use
Would people be interested in this? My main concern is for the cancelled() function. Also I'm probably going to file context.Data as a separate proposal.
@deanveloper Sounds interesting. Would be nice to see your proposal, and how it compares to ctx.Done(), etc.
Maybe you should be considering WaitGroups, and how waiting for a goroutine could be waited on to finish could be intergrated? cancel, done := go foo() so the parent goroutine can wait for children goroutines to complete.
Well, thats just my wild from the cuff ideas. I'm just learning Structured Concurrency for Go, so am interested in seeing proposals and ideas on how to make SC easier under Go.
Context values are passed everywhere explicitly, which troubles some people. Some explicitness is clearly good, but can we make it simpler?
If it was just some it would be good. Unfortunetly the current Context solution has become infective, adding unneccesary noise to almost all method and function signatures.
If we split the two uses, that of Cancellation and that of "store",
then a bounded Scope soluton could include the Cancellation feature, living only the store feature to be resolved, but that is at least in my experience not the main part. It gets used a lot just because we already have a context for cancellation purposes and it happens to also have a convenient store.
Suppose we have way to name Scopes and pas a "context.ContextLite"(offering only the cancellation mechanism)
WithContext(ctx: ContextLite){
...
}
ctx would be available everywhere in that scope (and inherited further down the chain) and used to cancel whatever tasks we want to cancel.
starting an Anonymous Scope {...} implictly passes context.background.
Scopes Started within another scope inherit the parent context and can augment farther.
a cancellation on any level of the scope hierarchy, propagates the cancellation down to the child scopes.
This sounds very similar to TLS/GLS but it would be limited to just the context type for cancellation.
Ofcourse there is no reason why to limit this to just cancellation and not be a real TLS/GLS but the problems are distinct,.
I think it has been a mistake to mix these two in the same Context so whatever the solution, even if the mechanism remains the same.
The current context.Context is still a good solution for storing values because it is not infectous like the cancellation problem is.