Skip to content

Commit c14ed5b

Browse files
AlexanderYastrebovianlancetaylor
authored andcommitted
Revert "net/http: close accepted connection"
This reverts CL 353714. The change closes accepted connection also in graceful shutdown which breaks the fix for #33313 (and apparent duplicate #36819). The proper fix should close accepted connection only if server is closed but not in graceful shutdown. Updates #48642 Change-Id: I2f7005f3f3037e6563745731bb2693923b654004 GitHub-Last-Rev: f6d885a GitHub-Pull-Request: #52823 Reviewed-on: https://go-review.googlesource.com/c/go/+/405454 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ec5bdef commit c14ed5b

File tree

2 files changed

+9
-40
lines changed

2 files changed

+9
-40
lines changed

src/net/http/serve_test.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6725,28 +6725,3 @@ func testMaxBytesHandler(t *testing.T, maxSize, requestSize int64) {
67256725
t.Errorf("expected echo of size %d; got %d", handlerN, buf.Len())
67266726
}
67276727
}
6728-
6729-
// Issue 48642: close accepted connection
6730-
func TestServerCloseAccepted(t *testing.T) {
6731-
closed := 0
6732-
conn := &rwTestConn{
6733-
closeFunc: func() error {
6734-
closed++
6735-
return nil
6736-
},
6737-
}
6738-
ln := &oneConnListener{conn: conn}
6739-
var srv Server
6740-
// Use ConnContext to close server after connection is accepted but before it is tracked
6741-
srv.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
6742-
srv.Close()
6743-
return ctx
6744-
}
6745-
got := srv.Serve(ln)
6746-
if got != ErrServerClosed {
6747-
t.Errorf("Serve err = %v; want ErrServerClosed", got)
6748-
}
6749-
if closed != 1 {
6750-
t.Errorf("Connection expected to be closed")
6751-
}
6752-
}

src/net/http/server.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,10 +1754,10 @@ const (
17541754
func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) {
17551755
srv := c.server
17561756
switch state {
1757+
case StateNew:
1758+
srv.trackConn(c, true)
17571759
case StateHijacked, StateClosed:
1758-
srv.mu.Lock()
1759-
delete(srv.activeConn, c)
1760-
srv.mu.Unlock()
1760+
srv.trackConn(c, false)
17611761
}
17621762
if state > 0xff || state < 0 {
17631763
panic("internal error")
@@ -3068,10 +3068,6 @@ func (srv *Server) Serve(l net.Listener) error {
30683068
}
30693069
tempDelay = 0
30703070
c := srv.newConn(rw)
3071-
if !srv.trackConn(c) {
3072-
rw.Close()
3073-
return ErrServerClosed
3074-
}
30753071
c.setState(c.rwc, StateNew, runHooks) // before Serve can return
30763072
go c.serve(connCtx)
30773073
}
@@ -3143,19 +3139,17 @@ func (s *Server) trackListener(ln *net.Listener, add bool) bool {
31433139
return true
31443140
}
31453141

3146-
// trackConn adds a connection to the set of tracked connections.
3147-
// It reports whether the server is still up (not Shutdown or Closed).
3148-
func (s *Server) trackConn(c *conn) bool {
3142+
func (s *Server) trackConn(c *conn, add bool) {
31493143
s.mu.Lock()
31503144
defer s.mu.Unlock()
3151-
if s.shuttingDown() {
3152-
return false
3153-
}
31543145
if s.activeConn == nil {
31553146
s.activeConn = make(map[*conn]struct{})
31563147
}
3157-
s.activeConn[c] = struct{}{}
3158-
return true
3148+
if add {
3149+
s.activeConn[c] = struct{}{}
3150+
} else {
3151+
delete(s.activeConn, c)
3152+
}
31593153
}
31603154

31613155
func (s *Server) idleTimeout() time.Duration {

0 commit comments

Comments
 (0)