Skip to content

net/http: keep a connection with the server when its hijack fails - #82386

Open
fabifont wants to merge 1 commit into
golang:masterfrom
fabifont:net-http-hijack-owner
Open

fabifont wants to merge 1 commit into
golang:masterfrom
fabifont:net-http-hijack-owner

Conversation

@fabifont

Copy link
Copy Markdown

hijackLocked marks the connection hijacked and clears its deadlines
before the Peek that collects the background read's byte into the
bufio.Reader. If that Peek fails, Hijack returns an error and no
connection, but the connection stays marked hijacked: conn.serve then
returns without closing it, its ConnState hook never sees
StateHijacked or StateClosed, and the net.Conn is only released when
the garbage collector finalizes its descriptor.

Mark the connection hijacked, and hand over its deadlines and
writer, only once the byte has been collected, so that a failed
hijack leaves the connection with the server as if Hijack had not
been called, and the server closes it as usual. The Peek cannot fail
with the readers the server sets up today, so this fixes the error
path rather than a case anyone has hit.

Fixes #82385

hijackLocked marks the connection hijacked and clears its deadlines
before the Peek that collects the background read's byte into the
bufio.Reader. If that Peek fails, Hijack returns an error and no
connection, but the connection stays marked hijacked: conn.serve then
returns without closing it, its ConnState hook never sees
StateHijacked or StateClosed, and the net.Conn is only released when
the garbage collector finalizes its descriptor.

Mark the connection hijacked, and hand over its deadlines and
writer, only once the byte has been collected, so that a failed
hijack leaves the connection with the server as if Hijack had not
been called, and the server closes it as usual. The Peek cannot fail
with the readers the server sets up today, so this fixes the error
path rather than a case anyone has hit.

Fixes golang#82385
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 5118df8) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/851108.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

net/http: a failed Hijack leaves the connection marked hijacked with no owner

2 participants