diff --git a/server/start.go b/server/start.go index d314c35f0a36..90d40de67394 100644 --- a/server/start.go +++ b/server/start.go @@ -172,7 +172,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. } err = wrapCPUProfile(serverCtx, func() error { - return start(serverCtx, clientCtx, appCreator, withCMT, opts) + return start(cmd.Context(), serverCtx, clientCtx, appCreator, withCMT, opts) }) serverCtx.Logger.Debug("received quit signal") @@ -241,7 +241,7 @@ is performed. Note, when enabled, gRPC will also be automatically enabled. return cmd } -func start(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator, withCmt bool, opts StartCmdOptions) error { +func start(parentCtx context.Context, svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator, withCmt bool, opts StartCmdOptions) error { svrCfg, err := getAndValidateConfig(svrCtx) if err != nil { return err @@ -261,12 +261,12 @@ func start(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreato emitServerInfoMetrics() if !withCmt { - return startStandAlone(svrCtx, svrCfg, clientCtx, app, metrics, opts) + return startStandAlone(parentCtx, svrCtx, svrCfg, clientCtx, app, metrics, opts) } - return startInProcess(svrCtx, svrCfg, clientCtx, app, metrics, opts) + return startInProcess(parentCtx, svrCtx, svrCfg, clientCtx, app, metrics, opts) } -func startStandAlone(svrCtx *Context, svrCfg serverconfig.Config, clientCtx client.Context, app types.Application, metrics *telemetry.Metrics, opts StartCmdOptions) error { +func startStandAlone(parentCtx context.Context, svrCtx *Context, svrCfg serverconfig.Config, clientCtx client.Context, app types.Application, metrics *telemetry.Metrics, opts StartCmdOptions) error { addr := svrCtx.Viper.GetString(flagAddress) transport := svrCtx.Viper.GetString(flagTransport) @@ -278,7 +278,7 @@ func startStandAlone(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clie svr.SetLogger(servercmtlog.CometLoggerWrapper{Logger: svrCtx.Logger.With("module", "abci-server")}) - g, ctx := getCtx(svrCtx, false) + g, ctx := getCtx(parentCtx, svrCtx, false) // Add the tx service to the gRPC router. We only need to register this // service if API or gRPC is enabled, and avoid doing so in the general @@ -329,7 +329,7 @@ func startStandAlone(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clie return g.Wait() } -func startInProcess(svrCtx *Context, svrCfg serverconfig.Config, clientCtx client.Context, app types.Application, +func startInProcess(parentCtx context.Context, svrCtx *Context, svrCfg serverconfig.Config, clientCtx client.Context, app types.Application, metrics *telemetry.Metrics, opts StartCmdOptions, ) error { cmtCfg := svrCtx.Config @@ -337,7 +337,7 @@ func startInProcess(svrCtx *Context, svrCfg serverconfig.Config, clientCtx clien gRPCOnly := svrCtx.Viper.GetBool(flagGRPCOnly) - g, ctx := getCtx(svrCtx, true) + g, ctx := getCtx(parentCtx, svrCtx, true) if gRPCOnly { // TODO: Generalize logic so that gRPC only is really in startStandAlone @@ -620,11 +620,11 @@ func emitServerInfoMetrics() { telemetry.SetGaugeWithLabels([]string{"server", "info"}, 1, ls) } -func getCtx(svrCtx *Context, block bool) (*errgroup.Group, context.Context) { - ctx, cancelFn := context.WithCancel(context.Background()) +func getCtx(parentCtx context.Context, svrCtx *Context, block bool) (*errgroup.Group, context.Context) { + ctx, cancelFn := context.WithCancel(parentCtx) g, ctx := errgroup.WithContext(ctx) // listen for quit signals so the calling parent process can gracefully exit - ListenForQuitSignals(g, block, cancelFn, svrCtx.Logger) + ListenForQuitSignals(parentCtx, g, block, cancelFn, svrCtx.Logger) return g, ctx } diff --git a/server/util.go b/server/util.go index 080fa6df78d3..1b772313c54b 100644 --- a/server/util.go +++ b/server/util.go @@ -379,15 +379,19 @@ func ExternalIP() (string, error) { // // Note, the blocking behavior of this depends on the block argument. // The caller must ensure the corresponding context derived from the cancelFn is used correctly. -func ListenForQuitSignals(g *errgroup.Group, block bool, cancelFn context.CancelFunc, logger log.Logger) { +func ListenForQuitSignals(parentCtx context.Context, g *errgroup.Group, block bool, cancelFn context.CancelFunc, logger log.Logger) { sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) f := func() { - sig := <-sigCh + select { + case sig := <-sigCh: + logger.Info("caught signal", "signal", sig.String()) + cancelFn() + case <-parentCtx.Done(): + logger.Info("parentCtx is done") + } cancelFn() - - logger.Info("caught signal", "signal", sig.String()) } if block {