Skip to content

Commit bbbc658

Browse files
rkollarbradfitz
authored andcommitted
net/http: fix Server.ConnContext modifying context for all new connections
Fixes #35750 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]>
1 parent 94e9a5e commit bbbc658

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
@@ -6126,6 +6126,39 @@ func TestServerContextsHTTP2(t *testing.T) {
61266126
}
61276127
}
61286128

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

src/net/http/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,16 +2920,17 @@ func (srv *Server) Serve(l net.Listener) error {
29202920
}
29212921
return err
29222922
}
2923+
connCtx := ctx
29232924
if cc := srv.ConnContext; cc != nil {
2924-
ctx = cc(ctx, rw)
2925-
if ctx == nil {
2925+
connCtx = cc(connCtx, rw)
2926+
if connCtx == nil {
29262927
panic("ConnContext returned nil")
29272928
}
29282929
}
29292930
tempDelay = 0
29302931
c := srv.newConn(rw)
29312932
c.setState(c.rwc, StateNew) // before Serve can return
2932-
go c.serve(ctx)
2933+
go c.serve(connCtx)
29332934
}
29342935
}
29352936

0 commit comments

Comments
 (0)