Skip to content

Commit

Permalink
net/http: make Server respect shutdown state after handler finishes
Browse files Browse the repository at this point in the history
If the Server's Shutdown (or SetKeepAlivesEnabled) method was called
while a connection was in a Handler, but after the headers had been
written, the connection was not later closed.

Fixes #9478
Updates #17754 (reverts that workaround)

Change-Id: I65324ab8217373fbb38e12e2b8bffd0a91806072
Reviewed-on: https://go-review.googlesource.com/33141
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
bradfitz committed Nov 11, 2016
1 parent 39e3cbf commit 9f9d834
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/net/http/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4943,9 +4943,6 @@ func TestServerSetKeepAlivesEnabledClosesConns(t *testing.T) {
}) {
t.Fatalf("idle count before SetKeepAlivesEnabled called = %v; want 1", idle0)
}
if !waitCondition(2*time.Second, 10*time.Millisecond, ts.Config.ExportAllConnsIdle) {
t.Fatalf("test server has active conns")
}

ts.Config.SetKeepAlivesEnabled(false)

Expand Down
8 changes: 8 additions & 0 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,14 @@ func (c *conn) serve(ctx context.Context) {
c.setState(c.rwc, StateIdle)
c.curReq.Store((*response)(nil))

if !w.conn.server.doKeepAlives() {
// We're in shutdown mode. We might've replied
// to the user without "Connection: close" and
// they might think they can send another
// request, but such is life with HTTP/1.1.
return
}

if d := c.server.idleTimeout(); d != 0 {
c.rwc.SetReadDeadline(time.Now().Add(d))
if _, err := c.bufr.Peek(4); err != nil {
Expand Down

0 comments on commit 9f9d834

Please sign in to comment.