Skip to content

Commit 94b6eec

Browse files
rkollarbradfitz
authored andcommitted
[release-branch.go1.13] net/http: fix Server.ConnContext modifying context for all new connections
Updates #35750 Fixes #35765 Change-Id: I65d38cfc5ddd66131777e104c269cc3559b2471d GitHub-Last-Rev: 953fdfd GitHub-Pull-Request: #35751 Reviewed-on: https://go-review.googlesource.com/c/go/+/208318 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> (cherry picked from commit bbbc658) Reviewed-on: https://go-review.googlesource.com/c/go/+/208235 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent abfbc05 commit 94b6eec

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/net/http/serve_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6121,6 +6121,39 @@ func TestServerContextsHTTP2(t *testing.T) {
61216121
}
61226122
}
61236123

6124+
// Issue 35750: check ConnContext not modifying context for other connections
6125+
func TestConnContextNotModifyingAllContexts(t *testing.T) {
6126+
setParallel(t)
6127+
defer afterTest(t)
6128+
type connKey struct{}
6129+
ts := httptest.NewUnstartedServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
6130+
rw.Header().Set("Connection", "close")
6131+
}))
6132+
ts.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
6133+
if got := ctx.Value(connKey{}); got != nil {
6134+
t.Errorf("in ConnContext, unexpected context key = %#v", got)
6135+
}
6136+
return context.WithValue(ctx, connKey{}, "conn")
6137+
}
6138+
ts.Start()
6139+
defer ts.Close()
6140+
6141+
var res *Response
6142+
var err error
6143+
6144+
res, err = ts.Client().Get(ts.URL)
6145+
if err != nil {
6146+
t.Fatal(err)
6147+
}
6148+
res.Body.Close()
6149+
6150+
res, err = ts.Client().Get(ts.URL)
6151+
if err != nil {
6152+
t.Fatal(err)
6153+
}
6154+
res.Body.Close()
6155+
}
6156+
61246157
// Issue 30710: ensure that as per the spec, a server responds
61256158
// with 501 Not Implemented for unsupported transfer-encodings.
61266159
func TestUnsupportedTransferEncodingsReturn501(t *testing.T) {

src/net/http/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,16 +2915,17 @@ func (srv *Server) Serve(l net.Listener) error {
29152915
}
29162916
return e
29172917
}
2918+
connCtx := ctx
29182919
if cc := srv.ConnContext; cc != nil {
2919-
ctx = cc(ctx, rw)
2920-
if ctx == nil {
2920+
connCtx = cc(connCtx, rw)
2921+
if connCtx == nil {
29212922
panic("ConnContext returned nil")
29222923
}
29232924
}
29242925
tempDelay = 0
29252926
c := srv.newConn(rw)
29262927
c.setState(c.rwc, StateNew) // before Serve can return
2927-
go c.serve(ctx)
2928+
go c.serve(connCtx)
29282929
}
29292930
}
29302931

0 commit comments

Comments
 (0)