-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
context: the docs should better clarify the context timeout #70945
Comments
Thanks. Would you like to send a patch improving the documentation? See https://go.dev/doc/contribute. |
@ianlancetaylor Hi, Ian. Of course, I'd be glad to. However, I currently don’t have enough time to dedicate myself fully to it. Beyond what I’ve mentioned in the issue, I’m unsure if there are other omissions in the documentation regarding timeouts, and I’d prefer to address everything comprehensively. I would greatly appreciate it if you or someone else could take this on in the meantime. That said, if no one steps in, I will certainly dedicate myself to it. |
Change https://go.dev/cl/638975 mentions this issue: |
There are multiple ways a context can be cancelled. The deadline can pass, the timeout can occur, or the user can call the cancelFunc. They all mean the same thing: that the context has been cancelled.
I think this is not strictly true; it is not true for derived Contexts that have had WithoutCancel() called on them. |
The current documentation and the code differentiate between cancellations and timeouts. Additionally, I'm not entirely sure I understand the distinction you made between "the deadline can pass" and "the timeout can occur"—they are the same thing.
Yes, that's an exception and the doc for
|
What is the URL of the page with the issue?
https://pkg.go.dev/[email protected]
What is your user agent?
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Screenshot
No response
What did you do?
Inspired by context package clarification made in #68923.
Since the
context
is one of the core package for golang concurrent programming, I believe that every segment of its behavior should be clearly written and documented.In many places, the
context
package documentation makes a "difference" (as it should IMO) between context cancellation (done by calling thecancel
function) and context timeout (deadline passed). For example:AfterFunc
uses the term "done" to denote both situations:Err() error
also makes differenceetc...
However, in some places, the documentation remains unclear about the time-out.
For example:
Cause
:From the last sentence to the first.
It says that
Cause
returns nil if c has not yet been canceled. It is not true. It returns a non-nil value when the context has not yet been canceled (the cancel function has not been called) but deadline has expired (its or one of its parents). IMO the same term used byAfterFunc
- done - can be used here as well. Thus, "Cause returns nil if c is not done yet (canceled or timed out; itself or one of its parent)".Next three sentences explain only the behavior when the context is cancelled, but not when the c's or one its parents deadline has passed. It may seem that it is covered under "Otherwise, ..." but to me it looks more like it covers the case when
CancelFunc
is called instead ofCancelCauseFunc
.The first sentence is incomplete.
Cause
returns a non-nil error not only when c is canceled but also when c or any of its parent contexts times out. Again, IMO the term done used in theAfterFunc
fits well here. Thus, "Cause returns a non-nil error explaining why c is done (canceled or timed out; itself or one of its parent)."CancelCauseFunc
:This seems incomplete to me. It also does not set the cause in the case when the deadline of the context or one of its parents has passed. IMO the term done can be used here again. Further, the examples should also be corrected.
AfterFunc
:Everything looks great here, but I believe it should be mentioned that this also applies when one of the parent contexts times out. Nowhere in the specification does it state that if a context times out, all its derived children also time out, which makes the description of
AfterFunc
feel somewhat incomplete. IMO, we just need to add the note about parents that I mentioned earlier for done in the other examples.There is only a note that if a Context is canceled, all contexts derived from it are also canceled.
IMO, it should also be noted that if a Context times out, all Contexts derived from it also time out (even if they don't have their own timer, such as those created with
WithCancel
). For instance, in the following example ctxChild (even though created without timer) will timeout together with its parent:In this way, there would be no need to add notes about parents within the term done, thus it will be as it currently appears in
AfterFunc
.What did you see happen?
Insufficiently explained behavior in the case of a timeout.
What did you expect to see?
Adding a note in the overview about the relationship between a context and its child in the case of a timeout, and the usage of the term done.
The text was updated successfully, but these errors were encountered: