Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not run lifecycle hooks with expired context #875

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func (l *Lifecycle) Start(ctx context.Context) error {
l.mu.Unlock()

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
Expand Down Expand Up @@ -131,6 +136,9 @@ 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
Expand Down
44 changes: 44 additions & 0 deletions internal/lifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ func TestLifecycleStart(t *testing.T) {
assert.Equal(t, 2, starterCount, "expected the first and second starter to execute")
assert.Equal(t, 1, stopperCount, "expected the first stopper to execute since the second starter failed")
})

t.Run("DoNotRunStartHooksWithExpiredCtx", func(t *testing.T) {
t.Parallel()

l := New(testLogger(t), fxclock.System)
l.Append(Hook{
OnStart: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
OnStop: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := l.Start(ctx)
require.Error(t, err)
// Note: Stop does not return an error here because no hooks
// have been started, so we don't end up any of the corresponding
// stop hooks.
require.NoError(t, l.Stop(ctx))
Comment on lines +141 to +144
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. App will still error as expected on Stop, which is good.

})
}

func TestLifecycleStop(t *testing.T) {
Expand Down Expand Up @@ -247,6 +271,26 @@ func TestLifecycleStop(t *testing.T) {
assert.Equal(t, err, l.Start(context.Background()))
l.Stop(context.Background())
})

t.Run("DoNotRunStopHooksWithExpiredCtx", func(t *testing.T) {
t.Parallel()

l := New(testLogger(t), fxclock.System)
l.Append(Hook{
OnStart: func(context.Context) error {
return nil
},
OnStop: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
})
ctx, cancel := context.WithCancel(context.Background())
err := l.Start(ctx)
require.NoError(t, err)
cancel()
require.Error(t, l.Stop(ctx))
})
}

func TestHookRecordsFormat(t *testing.T) {
Expand Down