From 0a4cc1143e81225c9552bc6cd077a10e71fb343f Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 29 Dec 2025 15:57:39 -0500 Subject: [PATCH] Assorted bug fixes --- environment.go | 25 ++++++++++++++----------- phases.go | 10 +++++----- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/environment.go b/environment.go index c9ef5d2..1220f19 100644 --- a/environment.go +++ b/environment.go @@ -36,6 +36,7 @@ type environment struct { ctx context.Context done context.CancelCauseFunc group sync.WaitGroup + noneLeft chan struct { } conf MutableConfig cron *cron @@ -94,6 +95,8 @@ func (this *environment) Run(name, description string, actors ...Actor) { this.addToSetsInitial(actors...) this.addToSetsInitial(this.cron) + this.noneLeft = make(chan struct { }) + if !this.phase10FlagParsing() { os.Exit(2) } if !this.phase13PidFileCreation() { os.Exit(1) } if !this.phase17PrivilegeDropping() { os.Exit(1) } @@ -118,6 +121,17 @@ func (this *environment) Add(ctx context.Context, actors ...Actor) error { this.delFromSets(actors...) } + for _, actor := range actors { + if actor, ok := actor.(Configurable); ok { + err := actor.Configure(this.conf) + if err != nil { + cleanUp() + return fmt.Errorf ( + "could not apply configuration to %s: %w", + actor.(Actor).Type(), err) + } + } + } initializable := make([]Initializable, 0, len(actors)) for _, actor := range actors { if actor, ok := actor.(Initializable); ok { @@ -132,17 +146,6 @@ func (this *environment) Add(ctx context.Context, actors ...Actor) error { cleanUp() return err } - for _, actor := range actors { - if actor, ok := actor.(Configurable); ok { - err := actor.Configure(this.conf) - if err != nil { - cleanUp() - return fmt.Errorf ( - "could not apply configuration to %s: %w", - actor.(Actor).Type(), err) - } - } - } for _, actor := range actors { _, isRunnable := actor.(Runnable) _, isRunShutdownable := actor.(RunShutdownable) diff --git a/phases.go b/phases.go index 8e3ffa5..40398f1 100644 --- a/phases.go +++ b/phases.go @@ -200,7 +200,7 @@ func (this *environment) phase50ConfigurationApplication() bool { defer done() for _, actor := range sortActors(actors, actors.configurable.all()) { if !actors.info(actor.(Actor)).initial { continue } - if this.Verb() { log.Println ("... (50) applying configuration to %s", actor.(Actor).Type())} + if this.Verb() { log.Printf ("... (50) applying configuration to %s", actor.(Actor).Type())} err := actor.Configure(this.conf) if err != nil { log.Printf ( @@ -288,15 +288,14 @@ func (this *environment) phase70RunningBody() bool { }() log.Println(".// (70) startup sequence complete") // await context cancellation or waitgroup completion - wgChannel := make(chan struct { }, 1) go func() { this.group.Wait() - wgChannel <- struct { } { } + close(this.noneLeft) }() select { case <- this.ctx.Done(): if this.Verb() { log.Println("(i) (70) canceled") } - case <- wgChannel: + case <- this.noneLeft: if this.Verb() { log.Println("(i) (70) all actors have finished") } } return true @@ -358,6 +357,7 @@ func (this *environment) phase80Shutdown() bool { log.Println(".// (80) shutdown succeeded, goodbye") log.Printf("====== [%s] END =======", this.name) }() - this.group.Wait() + // wait for all actors to shut down + <- this.noneLeft return cause == nil }