diff --git a/environment.go b/environment.go index b3b1da7..0f6db1a 100644 --- a/environment.go +++ b/environment.go @@ -111,6 +111,10 @@ func (this *environment) Done(cause error) { // Add implements the package-level function [Add]. func (this *environment) Add(ctx context.Context, actors ...Actor) error { this.addToSets(actors...) + cleanUp := func() { + this.delFromSets(actors...) + } + initializable := make([]Initializable, 0, len(actors)) for _, actor := range actors { if actor, ok := actor.(Initializable); ok { @@ -122,12 +126,14 @@ func (this *environment) Add(ctx context.Context, actors ...Actor) error { if this.flags.crashOnError { panic(fmt.Sprint(err)) } + 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)