Skip to content

net/http: fix Server.ConnContext modifying context for all new connections #35751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6126,6 +6126,39 @@ func TestServerContextsHTTP2(t *testing.T) {
}
}

// Issue 35750: check ConnContext not modifying context for other connections
func TestConnContextNotModifyingAllContexts(t *testing.T) {
setParallel(t)
defer afterTest(t)
type connKey struct{}
ts := httptest.NewUnstartedServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
rw.Header().Set("Connection", "close")
}))
ts.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
if got := ctx.Value(connKey{}); got != nil {
t.Errorf("in ConnContext, unexpected context key = %#v", got)
}
return context.WithValue(ctx, connKey{}, "conn")
}
ts.Start()
defer ts.Close()

var res *Response
var err error

res, err = ts.Client().Get(ts.URL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()

res, err = ts.Client().Get(ts.URL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
}

// Issue 30710: ensure that as per the spec, a server responds
// with 501 Not Implemented for unsupported transfer-encodings.
func TestUnsupportedTransferEncodingsReturn501(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2920,16 +2920,17 @@ func (srv *Server) Serve(l net.Listener) error {
}
return err
}
connCtx := ctx
if cc := srv.ConnContext; cc != nil {
ctx = cc(ctx, rw)
if ctx == nil {
connCtx = cc(connCtx, rw)
if connCtx == nil {
panic("ConnContext returned nil")
}
}
tempDelay = 0
c := srv.newConn(rw)
c.setState(c.rwc, StateNew) // before Serve can return
go c.serve(ctx)
go c.serve(connCtx)
}
}

Expand Down