From 3230fa3a016611ff79c3d6093fffd80acf894fd8 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 6 Jul 2022 19:36:55 +0800 Subject: [PATCH] impose time limit for rpc shutdown in case it hangs over in edge cases --- node/rpcstack.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/node/rpcstack.go b/node/rpcstack.go index 0d2be9008a..7901d37ebe 100644 --- a/node/rpcstack.go +++ b/node/rpcstack.go @@ -27,6 +27,7 @@ import ( "strings" "sync" "sync/atomic" + "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rpc" @@ -81,6 +82,10 @@ type httpServer struct { handlerNames map[string]string } +const ( + shutdownTimeout = 5 * time.Second +) + func newHTTPServer(log log.Logger, timeouts rpc.HTTPTimeouts) *httpServer { h := &httpServer{log: log, timeouts: timeouts, handlerNames: make(map[string]string)} @@ -261,7 +266,12 @@ func (h *httpServer) doStop() { h.wsHandler.Store((*rpcHandler)(nil)) wsHandler.server.Stop() } - h.server.Shutdown(context.Background()) + ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) + defer cancel() + err := h.server.Shutdown(ctx) + if err != nil { + h.log.Warn("Something wrong with HTTP server graceful shutdown", "error", err) + } h.listener.Close() h.log.Info("HTTP server stopped", "endpoint", h.listener.Addr())