From 3e92ae0f48f314a4c610fc16c5cd4e3e96460ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Wed, 24 Mar 2021 06:00:47 +0100 Subject: [PATCH] Use context timeout during gateway service shutdown (#8644) Co-authored-by: Raul Jordan Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/gateway/BUILD.bazel | 1 + beacon-chain/gateway/gateway.go | 11 +++++++++-- validator/rpc/gateway/BUILD.bazel | 1 + validator/rpc/gateway/gateway.go | 12 ++++++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/beacon-chain/gateway/BUILD.bazel b/beacon-chain/gateway/BUILD.bazel index d3bcf8bc19..04472676c8 100644 --- a/beacon-chain/gateway/BUILD.bazel +++ b/beacon-chain/gateway/BUILD.bazel @@ -18,6 +18,7 @@ go_library( "//proto/beacon/rpc/v1:go_grpc_gateway_library", "//shared:go_default_library", "@com_github_grpc_ecosystem_grpc_gateway//runtime:go_default_library", + "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_grpc_gateway_library", "@com_github_rs_cors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", diff --git a/beacon-chain/gateway/gateway.go b/beacon-chain/gateway/gateway.go index 8d768dac8f..b893f5126b 100644 --- a/beacon-chain/gateway/gateway.go +++ b/beacon-chain/gateway/gateway.go @@ -10,6 +10,7 @@ import ( "time" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1_gateway" pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1_gateway" "github.com/prysmaticlabs/prysm/shared" @@ -108,8 +109,14 @@ func (g *Gateway) Status() error { // Stop the gateway with a graceful shutdown. func (g *Gateway) Stop() error { if g.server != nil { - if err := g.server.Shutdown(g.ctx); err != nil { - log.WithError(err).Error("Failed to shut down server") + shutdownCtx, shutdownCancel := context.WithTimeout(g.ctx, 2*time.Second) + defer shutdownCancel() + if err := g.server.Shutdown(shutdownCtx); err != nil { + if errors.Is(err, context.DeadlineExceeded) { + log.Warn("Existing connections terminated") + } else { + log.WithError(err).Error("Failed to gracefully shut down server") + } } } diff --git a/validator/rpc/gateway/BUILD.bazel b/validator/rpc/gateway/BUILD.bazel index d7b9385017..9b2e1489f1 100644 --- a/validator/rpc/gateway/BUILD.bazel +++ b/validator/rpc/gateway/BUILD.bazel @@ -12,6 +12,7 @@ go_library( "//proto/validator/accounts/v2:ethereum_validator_account_gateway_proto", "//validator/web:go_default_library", "@com_github_grpc_ecosystem_grpc_gateway//runtime:go_default_library", + "@com_github_pkg_errors//:go_default_library", "@com_github_rs_cors//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", "@org_golang_google_grpc//:go_default_library", diff --git a/validator/rpc/gateway/gateway.go b/validator/rpc/gateway/gateway.go index 399d5580d1..12ccf93478 100644 --- a/validator/rpc/gateway/gateway.go +++ b/validator/rpc/gateway/gateway.go @@ -4,8 +4,10 @@ import ( "context" "net/http" "strings" + "time" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/pkg/errors" pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2_gateway" "github.com/prysmaticlabs/prysm/validator/web" "github.com/rs/cors" @@ -100,8 +102,14 @@ func (g *Gateway) Status() error { // Stop the gateway with a graceful shutdown. func (g *Gateway) Stop() error { - if err := g.server.Shutdown(g.ctx); err != nil { - log.WithError(err).Error("Failed to shut down server") + shutdownCtx, shutdownCancel := context.WithTimeout(g.ctx, 2*time.Second) + defer shutdownCancel() + if err := g.server.Shutdown(shutdownCtx); err != nil { + if errors.Is(err, context.DeadlineExceeded) { + log.Warn("Existing connections terminated") + } else { + log.WithError(err).Error("Failed to gracefully shut down server") + } } if g.cancel != nil {