Skip to content

Commit

Permalink
[CLOB-930] Allow for parent context to shutdown application.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwik committed Jan 22, 2024
1 parent cfe7020 commit fbb2683
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
22 changes: 11 additions & 11 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -329,15 +329,15 @@ 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
home := cmtCfg.RootDir

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
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 8 additions & 4 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fbb2683

Please sign in to comment.