Skip to content
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

proposal: net/http: add context cancelation reason for server handlers #64465

Open
hummerd opened this issue Nov 30, 2023 · 4 comments
Open

proposal: net/http: add context cancelation reason for server handlers #64465

hummerd opened this issue Nov 30, 2023 · 4 comments
Labels
Milestone

Comments

@hummerd
Copy link

hummerd commented Nov 30, 2023

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 - by http.Server itself in case of communication issues or by http.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.

@gopherbot gopherbot added this to the Proposal milestone Nov 30, 2023
@mauri870
Copy link
Member

mauri870 commented Nov 30, 2023

You can make use of the Cause apis to do that, context.WithDeadlineCause, context.WithCancelCause, context.WithTimeoutCause and so on, and then look at context.Cause for the cancellation cause.

@hummerd
Copy link
Author

hummerd commented Nov 30, 2023

You can make use of the Cause apis to do that, context.WithDeadlineCause, context.WithCancelCause, context.WithTimeoutCause and so on, and then look at context.Cause for the cancellation cause.

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 context.WithCancelCause and context.WithTimeoutCause.

@kszafran
Copy link

kszafran commented Jan 2, 2024

This would be very useful. Nowadays, when I see that a request failed due to context canceled, I have no idea happened. It could be a timeout on a SQL query, some internal request, etc. And if the context was canceled, because the client closed the connection (which happens in (*connReader).backgrounRead() -> cr.handleReadError(err), as far as I'm able to tell), I'd like to be able to distinguish it and not log it as an error.

@seankhliao seankhliao changed the title proposal: net/http: Add context cancelation reason for server handlers proposal: net/http: add context cancelation reason for server handlers Jan 13, 2024
@seankhliao
Copy link
Member

cc @neild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants