diff --git a/internal/lifecycle/lifecycle.go b/internal/lifecycle/lifecycle.go index dfac47a8c..59eaa7e34 100644 --- a/internal/lifecycle/lifecycle.go +++ b/internal/lifecycle/lifecycle.go @@ -30,10 +30,11 @@ import ( "sync" "time" + "go.uber.org/multierr" + "go.uber.org/fx/fxevent" "go.uber.org/fx/internal/fxclock" "go.uber.org/fx/internal/fxreflect" - "go.uber.org/multierr" ) // Reflection types for each of the supported hook function signatures. These @@ -203,11 +204,6 @@ func (l *Lifecycle) Start(ctx context.Context) error { }() for _, hook := range l.hooks { - // if ctx has cancelled, bail out of the loop. - if err := ctx.Err(); err != nil { - return err - } - if hook.OnStart != nil { l.mu.Lock() l.runningHook = hook @@ -285,9 +281,6 @@ func (l *Lifecycle) Stop(ctx context.Context) error { // Run backward from last successful OnStart. var errs []error for ; l.numStarted > 0; l.numStarted-- { - if err := ctx.Err(); err != nil { - return err - } hook := l.hooks[l.numStarted-1] if hook.OnStop == nil { continue diff --git a/internal/lifecycle/lifecycle_test.go b/internal/lifecycle/lifecycle_test.go index 1b28191ff..fc0af987f 100644 --- a/internal/lifecycle/lifecycle_test.go +++ b/internal/lifecycle/lifecycle_test.go @@ -31,13 +31,14 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/goleak" + "go.uber.org/multierr" + "go.uber.org/fx/fxevent" "go.uber.org/fx/internal/fxclock" "go.uber.org/fx/internal/fxlog" "go.uber.org/fx/internal/fxreflect" "go.uber.org/fx/internal/testutil" - "go.uber.org/goleak" - "go.uber.org/multierr" ) func testLogger(t *testing.T) fxevent.Logger { @@ -126,8 +127,10 @@ func TestLifecycleStart(t *testing.T) { l := New(testLogger(t), fxclock.System) l.Append(Hook{ - OnStart: func(context.Context) error { - assert.Fail(t, "this hook should not run") + OnStart: func(ctx context.Context) error { + if err := ctx.Err(); err != nil { + return err + } return nil }, OnStop: func(context.Context) error {