]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: check server shutting down before processing the request
authorwineandchord <guoqizhou19@gmail.com>
Tue, 20 Feb 2024 07:18:11 +0000 (07:18 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 4 Mar 2025 12:56:10 +0000 (04:56 -0800)
The root cause of issue #65802 is a small race condition that occurs between
two events:

1. During the HTTP server shutdown, a connection in an idle state is identified
and closed.
2. The connection, although idle, has just finished reading a complete request
before being closed and hasn't yet updated its state to active.

In this scenario, despite the connection being closed, the request continues to
be processed. This not only wastes server resources but also prevents the
client request from being retried.

Fixes #65802

Change-Id: Ic22abb4497be04f6c84dff059df00f2c319d8652
GitHub-Last-Rev: 426099a3e75f51b80f8ca866938f31417d75ff89
GitHub-Pull-Request: golang/go#65805
Reviewed-on: https://go-review.googlesource.com/c/go/+/565277
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>

src/net/http/server.go

index b452f643bd3ff554aece7e555e0c6e8ca7a74c28..a29b8b39aafb72ecfb76ef64f8f7fbdf81e1a8a7 100644 (file)
@@ -2006,6 +2006,9 @@ func (c *conn) serve(ctx context.Context) {
                        // If we read any bytes off the wire, we're active.
                        c.setState(c.rwc, StateActive, runHooks)
                }
+               if c.server.shuttingDown() {
+                       return
+               }
                if err != nil {
                        const errorHeaders = "\r\nContent-Type: text/plain; charset=utf-8\r\nConnection: close\r\n\r\n"