Skip to content

Commit 16da6a1

Browse files
committed
Fix timeout report generation race
When fx is run with a very short timeout, it's possible for the timeout error message to be in a bad format "OnStart hook added by failed" where the name of the OnStart hook's caller hasn't been recorded before the context times out. This fixes the error message generation to not rely on the OnStart hook's caller always being known to fix this race. Fixes #815
1 parent 88cdb34 commit 16da6a1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

app.go

+5
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,11 @@ func withTimeout(ctx context.Context, param *withTimeoutParams) error {
10231023
caller,
10241024
err,
10251025
r)
1026+
} else if caller == "" {
1027+
return fmt.Errorf("%v hook failed: %w",
1028+
param.hook,
1029+
err)
1030+
10261031
}
10271032
return fmt.Errorf("%v hook added by %v failed: %w",
10281033
param.hook,

app_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,24 @@ func TestAppRunTimeout(t *testing.T) {
883883
}
884884
}
885885

886+
func TestVeryShortTimeout(t *testing.T) {
887+
type A struct{}
888+
spy := new(fxlog.Spy)
889+
app := New(
890+
WithLogger(func() fxevent.Logger { return spy }),
891+
Provide(func() *A { return &A{} }),
892+
Invoke(func(*A) {}),
893+
)
894+
895+
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
896+
err := app.Start(ctx)
897+
require.Error(t, err)
898+
// The error message should never be in the format "added by" followed by an empty string.
899+
assert.NotContains(t, err.Error(), "OnStart hook added by failed")
900+
assert.Contains(t, err.Error(), "context deadline exceeded")
901+
cancel()
902+
}
903+
886904
func TestAppStart(t *testing.T) {
887905
t.Parallel()
888906

0 commit comments

Comments
 (0)