From ff2eaa9b36a73d0caed52d4d81914ac559ed2c8d Mon Sep 17 00:00:00 2001 From: Berger Eugene Date: Sun, 3 Nov 2024 19:37:26 +0200 Subject: [PATCH] fix #258: fix race condition for stopping killed daemons --- src/app/daemon.go | 2 +- src/app/process.go | 35 ++++++++++++++++++----------------- src/app/project_runner.go | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/app/daemon.go b/src/app/daemon.go index 352675e..ade7dd4 100644 --- a/src/app/daemon.go +++ b/src/app/daemon.go @@ -19,7 +19,7 @@ loop: } func (p *Process) notifyDaemonStopped() { - if p.isDaemonLaunched() { + if p.procConf.IsDaemon { p.procStateChan <- types.ProcessStateCompleted } } diff --git a/src/app/process.go b/src/app/process.go index 206e8ae..907a4cc 100644 --- a/src/app/process.go +++ b/src/app/process.go @@ -337,24 +337,25 @@ func (p *Process) waitForCompletion() int { } func (p *Process) waitUntilReady() bool { - for { - select { - case <-p.procReadyCtx.Done(): - if p.procState.Health == types.ProcessHealthReady { - return true - } - log.Error().Msgf("Process %s was aborted and won't become ready", p.getName()) - p.setExitCode(1) - return false - case <-p.procLogReadyCtx.Done(): - err := context.Cause(p.procLogReadyCtx) - if errors.Is(err, context.Canceled) { - return true - } - log.Error().Err(err).Msgf("Process %s was aborted and won't become log ready", p.getName()) - return false - } + <-p.procReadyCtx.Done() + if p.procState.Health == types.ProcessHealthReady { + return true + } + log.Error().Msgf("Process %s was aborted and won't become ready", p.getName()) + p.setExitCode(1) + return false + +} + +func (p *Process) waitUntilLogReady() bool { + <-p.procLogReadyCtx.Done() + err := context.Cause(p.procLogReadyCtx) + if errors.Is(err, context.Canceled) { + return true } + log.Error().Err(err).Msgf("Process %s was aborted and won't become log ready", p.getName()) + return false + } func (p *Process) wontRun() { diff --git a/src/app/project_runner.go b/src/app/project_runner.go index 353eb0e..1a92b42 100644 --- a/src/app/project_runner.go +++ b/src/app/project_runner.go @@ -168,7 +168,7 @@ func (p *ProjectRunner) waitIfNeeded(process *types.ProcessConfig) error { } case types.ProcessConditionLogReady: log.Info().Msgf("%s is waiting for %s log line %s", process.ReplicaName, k, runningProc.procConf.ReadyLogLine) - ready := runningProc.waitUntilReady() + ready := runningProc.waitUntilLogReady() if !ready { return fmt.Errorf("process %s depended on %s to become ready, but it was terminated", process.ReplicaName, k) }