From e1ffaa52f4762d318a67242baf56d32185b3c05f Mon Sep 17 00:00:00 2001 From: Kirill Maksimov Date: Thu, 29 Oct 2020 14:04:32 +0200 Subject: [PATCH] Fix autoupdate context shutdown --- .version | 2 +- services/execute.go | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.version b/.version index 4e2cea3b..3f0882c5 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -v1.8.0 \ No newline at end of file +v1.8.1 \ No newline at end of file diff --git a/services/execute.go b/services/execute.go index cfbd14c3..4f569c48 100644 --- a/services/execute.go +++ b/services/execute.go @@ -19,6 +19,8 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr return nil, fmt.Errorf("--keys is a required parameter for provisioning flow") } + // crucial for a proper shutdown + ctxWithCancel, cancelAndExit := context.WithCancel(ctx) supervisor := &govnr.TreeSupervisor{} // clean up old files @@ -29,14 +31,13 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr if flags.StatusFilePath == "" && flags.MetricsFilePath == "" { logger.Info("status file path and metrics file path are empty, periodical report disabled") } else { - supervisor.Supervise(WatchAndReportStatusAndMetrics(ctx, logger, flags.StatusFilePath, flags.MetricsFilePath)) + supervisor.Supervise(WatchAndReportStatusAndMetrics(ctxWithCancel, logger, flags.StatusFilePath, flags.MetricsFilePath)) } cfgFetcher := NewConfigurationPollService(flags, logger) coreBoyar := NewCoreBoyarService(logger) // wire cfg and boyar - ctxWithCancel, cancelAndExit := context.WithCancel(ctx) supervisor.Supervise(govnr.Forever(ctxWithCancel, "apply config changes", utils.NewLogErrors("apply config changes", logger), func() { var cfg config.NodeConfiguration = nil select { @@ -49,7 +50,7 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr } // random delay when provisioning change (that is, not bootstrap flow or repairing broken system) if coreBoyar.healthy { - maybeDelayConfigUpdate(ctx, cfg, flags.MaxReloadTimeDelay, coreBoyar.logger) + maybeDelayConfigUpdate(ctxWithCancel, cfg, flags.MaxReloadTimeDelay, coreBoyar.logger) } else { logger.Info("applying new configuration immediately") } @@ -60,22 +61,22 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr return } - ctx, cancel := context.WithTimeout(ctx, flags.Timeout) + ctxWithTimeout, cancel := context.WithTimeout(ctxWithCancel, flags.Timeout) defer cancel() - err := coreBoyar.OnConfigChange(ctx, cfg) + err := coreBoyar.OnConfigChange(ctxWithTimeout, cfg) if err != nil { logger.Error("error executing configuration", log.Error(err)) cfgFetcher.Resend() } - if ctx.Err() != nil { - logger.Error("failed to apply new configuration", log.Error(ctx.Err())) + if ctxWithTimeout.Err() != nil { + logger.Error("failed to apply new configuration", log.Error(ctxWithTimeout.Err())) return } })) - supervisor.Supervise(cfgFetcher.Start(ctx)) + supervisor.Supervise(cfgFetcher.Start(ctxWithCancel)) return supervisor, nil }