golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.28k stars 17.57k forks source link

x/net/http2: cancel stream context more proactively when closing or resetting stream #68099

Open dagood opened 3 months ago

dagood commented 3 months ago

Go version

go version go1.22.4 linux/amd64

Output of go env in your module/workspace:

GOARCH='amd64'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
GOAMD64='v1'
[... trimmed, does not seem relevant]

What did you do?

When the server closes or resets a stream, in some cases my handler is still running. The handler may be performing an expensive operation or using a lot of memory. Canceling the context as soon as possible means the handler can detect that it's no longer doing useful work and stop wasting resources.

What did you see happen?

In some cases, the handler context is not canceled until later than I'd expect. For example, the scheduler might do a lot of work to wrap up http2 state for a stream reset before cancelling the context.

What did you expect to see?

As soon as the server knows that continuing to run the handler won't do any useful work, the context should be cancelled.

I wrote a CL to cancel earlier in a few cases, but I had trouble writing tests for it: https://go-review.googlesource.com/c/net/+/536515. I'm filing this as an issue in case someone more familiar with x/net/http2 wants to work on it. 🙂

gabyhelp commented 3 months ago

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)