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

Remove context error check in lifecycle #1034

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Remove context error check in lifecycle
yux0 committed Feb 7, 2023
commit d36b377dcf68796587d55f7fac7f2622c5f9cf9a
11 changes: 2 additions & 9 deletions internal/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions internal/lifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -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 {