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
x/net/context: Documentation for Context.Err doesn't explicitly state what values to expect before Done() is closed #14292
Comments
I would argue that (2) is correct. There is no reason to call (To poll whether a |
It's been a while, but if I recall, the reason to call for ctx.Err() != nil {
// do short operation that can't easily select on Done()
} Otherwise this would need to look like outer: for {
select {
case <-ctx.Done():
break outer
default:
// do short operation
}
} I don't recall the exact specifics of why this came up, though. I don't see why expecting to be able to call Err() without Done() is inherently racy though? I suppose if you were checking Err() and then doing something with the context, that could be racy (sort-of... the context itself would have to be racy), but I don't see why that would be NOT the case in the event that you do something in the default clause of the select with the context object either: outer: for {
select {
case <-ctx.Done():
break outer
default:
// context could actually be Done here anyway
}
} |
A more concrete use-case would be helpful. In my experience, more often than not the loops you're describing actually look like: for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// Do short operation that can't easily select on Done().
}
return someValue, nil Calling The fundamental problem is this: if the implementation of for {
// do short operation that can't easily select on Done()
} But if |
I guess that's why I was hoping the documentation could be clarified 😄. It sounds like the documentation should say something along the lines of "It is undefined behavior to call Err() before blocking on Done()." |
(I was hoping that it would be reasonable to give Err() a meaning before Done() in order to make it safe, but it sounds like documenting this behavior is insufficient to make it reliable since other implementations of Context presumably don't couple these two.) |
(sorry for all the updates) Would it make sense to have |
I believe this issue can be closed now that #19856 was accepted and implemented. |
Duplicate of #19856 |
In https://godoc.org/golang.org/x/net/context#Context, there are two possible readings for the documentation of Err:
Looking at the implementation, I suspect that 1 is the correct interpretation. I would propose that the documentation on this function be amended to read:
Note the added "If Done is not yet closed, Err must return nil." sentence.
The text was updated successfully, but these errors were encountered: