Skip to content

Commit 56c5351

Browse files
windycryptofjl
authored andcommittedSep 22, 2023
rpc: add graceful shutdown timeout for HTTP server (ethereum#25258)
This change ensures the HTTP server will always terminate within at most 5s, even when all connections are busy and do not become idle. Co-authored-by: Felix Lange <fjl@twurst.com>

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed
 

‎node/rpcstack.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828
"sync"
2929
"sync/atomic"
30+
"time"
3031

3132
"github.com/ethereum/go-ethereum/log"
3233
"github.com/ethereum/go-ethereum/rpc"
@@ -81,6 +82,10 @@ type httpServer struct {
8182
handlerNames map[string]string
8283
}
8384

85+
const (
86+
shutdownTimeout = 5 * time.Second
87+
)
88+
8489
func newHTTPServer(log log.Logger, timeouts rpc.HTTPTimeouts) *httpServer {
8590
h := &httpServer{log: log, timeouts: timeouts, handlerNames: make(map[string]string)}
8691

@@ -261,7 +266,13 @@ func (h *httpServer) doStop() {
261266
h.wsHandler.Store((*rpcHandler)(nil))
262267
wsHandler.server.Stop()
263268
}
264-
h.server.Shutdown(context.Background())
269+
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
270+
defer cancel()
271+
err := h.server.Shutdown(ctx)
272+
if err == ctx.Err() {
273+
h.log.Warn("HTTP server graceful shutdown timed out")
274+
h.server.Close()
275+
}
265276
h.listener.Close()
266277
h.log.Info("HTTP server stopped", "endpoint", h.listener.Addr())
267278

0 commit comments

Comments
 (0)
Please sign in to comment.