-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: net/http: add context cancelation reason for server handlers #64465
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
Comments
You can make use of the Cause apis to do that, |
The idea of the proposal is to add cause for cases when context is canceled within standard library itself. And yes it can be accomplished with |
This would be very useful. Nowadays, when I see that a request failed due to |
cc @neild |
I'd like to work on this. I haven't contributed before, but I think this could be a good first issue and I'm running into it. My proposal would be to change
Feedback is welcome. In the meantime, I'll be following the contribution guide. |
When we cancel a HTTP server handler context, set an appropriate cancel cause. This makes investigation of context cancellation errors easier. Fixes golang#64465
When we cancel a HTTP server handler context, set an appropriate cancel cause. This makes investigation of context cancellation errors easier. Fixes golang#64465
When we cancel a HTTP server handler context, set an appropriate cancel cause. This makes investigation of context cancellation errors easier. Fixes golang#64465
Change https://go.dev/cl/636235 mentions this issue: |
Mainly, I believe this proposal requires consensus about the following three
|
I don't think this needs to be a proposal. I don't see a good reason to expose (If a handler does need to identify when it has returned, that's obviously trivial to add in the handler itself.) Adding additional human-readable information about the cancellation reason to the context cause seems reasonable to me. |
I actually use the exposed ErrConnectionClosed to determine the log level of related log lines, and this was the reason for proposing the CL. In my case, gRPC method failure caused by context cancellation (anywhere in the code) is normally
If the ErrConnectionClosed is not exported I believe I would have to look at the .Error() string to make the distinction between context cancellation caused by net/http or by any other location in the code, isn't it? @neild would you consider this enough justification to expose (one of) the two Errs, or would you rather suggest another solution? Thanks |
@neild there are two users agreeing with my comment above regarding the export of the Errs, could we use this as community input to my suggestion and move this proposal through for review? |
Proposal Details
While serving HTTP request we have
req.Context()
that can be propagated down the pipe. This context can be canceled either by standard library - byhttp.Server
itself in case of communication issues or byhttp.TimeoutHandler
. But also it can be canceled by user code.It would be nice to have ability to clarify the reason why context was canceled (in case it was canceled by standard library). Logged reason will make you 100% sure and should make investigation of such context cancellation errors easier.
I'm ready to make a PR in case this proposal is considered useful.
The text was updated successfully, but these errors were encountered: