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

net/http: add clean way to stop a no-content-length transfer from an HTTP2 Server #22604

agirbal opened this issue Nov 6, 2017 · 2 comments


Copy link

@agirbal agirbal commented Nov 6, 2017

With HTTP2 there is no more chunked encoding, instead it's built into the stream system. Now in many use cases the server may proxy responses back from other systems, or generate them on the fly, in which cases there is no content-length. It is possible that the proxying / processing breaks, and then it's not clear how to communicate the breakage to the client. If you just return the handler, the HTTP2 library will wrap it up like nothing happened and user will get truncated data.

With HTTP1, the common trick is to just hijack and close the connection. This works well.

	if err == io.ErrUnexpectedEOF {
		// the upstream closed on us ungracefully.
		// if no content-length, need to notify downstream that content is incomplete.
		// In HTTP1.1 means closing connection, which requires hijacking.
		if rw.Header().Get("Content-Length") == "" {
			if hj, ok := rw.(http.Hijacker); ok {
				conn, _, _ := hj.Hijack()

A similar question for HTTP2 was asked here #14797
To which the answer from @bradfitz was to just panic. But this seems messy since the app may still have other processing to do related to this request, and at minimum things like access logging. It just feels unnatural to panic in this case, and difficult to understand what truly happens over Go revisions without testing (does stream get closed? Or just will time out?).

Would it be possible to expose the equivalent of a Hijack for HTTP2 streams, where one could explicitly abort the stream if needed?

What version of Go are you using (go version)?


Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?


What did you do?

What did you expect to see?

What did you see instead?

@ianlancetaylor ianlancetaylor changed the title With HTTP2, way to abort a no-content-length transfer from Server net/http: add clean way to stop a no-content-length transfer from an HTTP2 Server Nov 6, 2017
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Nov 6, 2017
Copy link

@ianlancetaylor ianlancetaylor commented Nov 6, 2017

Copy link

@bradfitz bradfitz commented Nov 7, 2017 was added in Go 1.8 explicitly for this purpose. It's tested and documented.

You may not like panicking as the answer, and we don't exactly either, but it's where we ended up.

@bradfitz bradfitz closed this Nov 7, 2017
@golang golang locked and limited conversation to collaborators Nov 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants