Skip to content

Commit e31f859

Browse files
committed
net/http: attempt deadlock fix in TestDisableKeepAliveUpgrade
1. The test now checks the response status code. 2. The transport has been changed to not set `Connection: Close` if DisableKeepAlive is set but the request is a HTTP/1.1 protocol upgrade. Updates #43073
1 parent 31496cf commit e31f859

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/net/http/response.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,12 @@ func (r *Response) isProtocolSwitch() bool {
361361
// isProtocolSwitchResponse reports whether the response code and
362362
// response header indicate a successful protocol upgrade response.
363363
func isProtocolSwitchResponse(code int, h Header) bool {
364-
return code == StatusSwitchingProtocols &&
365-
h.Get("Upgrade") != "" &&
364+
return code == StatusSwitchingProtocols && isProtocolSwitchHeader(h)
365+
}
366+
367+
// isProtocolSwitchHeader can be used to check if the request or response is
368+
// for a protocol switch.
369+
func isProtocolSwitchHeader(h Header) bool {
370+
return h.Get("Upgrade") != "" &&
366371
httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade")
367372
}

src/net/http/serve_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -6481,6 +6481,10 @@ func TestDisableKeepAliveUpgrade(t *testing.T) {
64816481
}
64826482
defer resp.Body.Close()
64836483

6484+
if resp.StatusCode != http.StatusSwitchingProtocols {
6485+
t.Fatalf("unexpected status code: %v", resp.StatusCode)
6486+
}
6487+
64846488
rwc, ok := resp.Body.(io.ReadWriteCloser)
64856489
if !ok {
64866490
t.Fatalf("Response.Body is not a io.ReadWriteCloser: %T", resp.Body)

src/net/http/transport.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -2564,7 +2564,9 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
25642564
continueCh = make(chan struct{}, 1)
25652565
}
25662566

2567-
if pc.t.DisableKeepAlives && !req.wantsClose() {
2567+
if pc.t.DisableKeepAlives &&
2568+
!req.wantsClose() &&
2569+
!isProtocolSwitchHeader(req.Header) {
25682570
req.extraHeaders().Set("Connection", "close")
25692571
}
25702572

0 commit comments

Comments
 (0)