From 7181118a851bc22cf7acc604fe24940eb4926288 Mon Sep 17 00:00:00 2001 From: wineandchord Date: Tue, 20 Feb 2024 07:18:11 +0000 Subject: [PATCH] net/http: check server shutting down before processing the request 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 Reviewed-by: Carlos Amedee Reviewed-by: Sean Liao Reviewed-by: Damien Neil Auto-Submit: Sean Liao --- src/net/http/server.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/net/http/server.go b/src/net/http/server.go index b452f643bd..a29b8b39aa 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -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" -- 2.51.0