From f8ec93dfdd7008c112ed1bc05f2be0bfeb11a266 Mon Sep 17 00:00:00 2001 From: mmmray <142015632+mmmray@users.noreply.github.com> Date: Thu, 6 Jun 2024 04:43:15 +0200 Subject: [PATCH] drain buffer correctly in httpupgrade (#3428) * drain buffer correctly in httpupgrade it seems the recently added httupgrade testsuite is causing timeouts on master i have no evidence this is the real issue, but it feels to me that the server could accidentally over-read, and then the encapsulated connection will block forever trying to read data let's test it in CI a couple of times, i don't have a way to reproduce the issue * correctly drain buffer, again --- transport/internet/httpupgrade/dialer.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/transport/internet/httpupgrade/dialer.go b/transport/internet/httpupgrade/dialer.go index b70af331..45c30506 100644 --- a/transport/internet/httpupgrade/dialer.go +++ b/transport/internet/httpupgrade/dialer.go @@ -24,8 +24,10 @@ type ConnRF struct { func (c *ConnRF) Read(b []byte) (int, error) { if c.First { c.First = false - // TODO The bufio usage here is unreliable - resp, err := http.ReadResponse(bufio.NewReader(c.Conn), c.Req) // nolint:bodyclose + // create reader capped to size of `b`, so it can be fully drained into + // `b` later with a single Read call + reader := bufio.NewReaderSize(c.Conn, len(b)) + resp, err := http.ReadResponse(reader, c.Req) // nolint:bodyclose if err != nil { return 0, err } @@ -34,6 +36,8 @@ func (c *ConnRF) Read(b []byte) (int, error) { strings.ToLower(resp.Header.Get("Connection")) != "upgrade" { return 0, newError("unrecognized reply") } + // drain remaining bufreader + return reader.Read(b[:reader.Buffered()]) } return c.Conn.Read(b) }