fix(lifecycle): retain parent cancellation at every shutdown return
Documentation / docs (pull_request) Successful in 16s
ci / check (pull_request) Successful in 1m33s

This commit is contained in:
end committed 2026-10-27 12:11:59 -07:00
1 parent 64c7e6b65d
commit f4abb626c6
2 files changed
+90 -30

No files matched your search

+82 -21
View File
@@ -380,28 +380,89 @@ func TestAdmissionRace(t *testing.T) {
}
func TestIndependentDescendantCancellation(t *testing.T) {
signals := make(chan os.Signal, 2)
a := newApp(t, signals, time.Second)
canceled := make(chan struct{})
allowReturn := make(chan struct{})
add(t, a, Module{Name: "owner", Setup: func(ctx context.Context) error {
child, cancel := context.WithCancel(ctx)
err := a.Go(child, "independent", func(ctx context.Context) error {
cancel()
close(canceled)
<-allowReturn
return fmt.Errorf("worker result: %w", errors.Join(ctx.Err(), errors.New("other")))
for _, mixed := range []bool{false, true} {
t.Run(map[bool]string{false: "cancellation-only", true: "mixed"}[mixed], func(t *testing.T) {
signals := make(chan os.Signal, 2)
a := newApp(t, signals, time.Second)
canceled := make(chan struct{})
allowReturn := make(chan struct{})
other := errors.New("other")
add(t, a, Module{Name: "owner", Setup: func(ctx context.Context) error {
child, cancel := context.WithCancel(ctx)
err := a.Go(child, "independent", func(ctx context.Context) error {
cancel()
close(canceled)
<-allowReturn
err := ctx.Err()
if mixed {
err = errors.Join(err, other)
}
return fmt.Errorf("worker result: %w", err)
})
if err != nil {
cancel()
}
return err
}, Teardown: func(context.Context) error { close(allowReturn); return nil }})
done := run(a, context.Background())
wait(t, canceled)
signals <- syscall.SIGTERM
if err := receive(t, done); !errors.Is(err, context.Canceled) || errors.Is(err, other) != mixed {
t.Fatalf("independent cancellation lost: %v", err)
}
})
}
}
func TestParentCancellationDuringShutdown(t *testing.T) {
for _, boundary := range []string{"completion", "deadline", "second-signal"} {
t.Run(boundary, func(t *testing.T) {
iterations := 1
if boundary == "completion" {
iterations = 1000
}
for range iterations {
parent, cancel := context.WithCancel(context.Background())
signals := make(chan os.Signal, 2)
timeout := time.Second
if boundary == "deadline" {
timeout = 20 * time.Millisecond
}
a := newApp(t, signals, timeout)
exited := make(chan struct{})
add(t, a, Module{Name: "last", Teardown: func(ctx context.Context) error {
defer close(exited)
cancel()
switch boundary {
case "deadline":
<-ctx.Done()
case "second-signal":
signals <- syscall.SIGINT
signals <- syscall.SIGTERM
<-ctx.Done()
}
return nil
}})
err := a.Run(parent)
cancel()
wait(t, exited)
if !errors.Is(err, context.Canceled) {
t.Fatalf("parent cancellation lost: %v", err)
}
if errors.Is(err, context.DeadlineExceeded) != (boundary == "deadline") {
t.Fatalf("unexpected shutdown result: %v", err)
}
parentErrors := 0
for _, child := range err.(interface{ Unwrap() []error }).Unwrap() {
if child == context.Canceled {
parentErrors++
}
}
if parentErrors != 1 {
t.Fatalf("parent error recorded %d times: %v", parentErrors, err)
}
}
})
if err != nil {
cancel()
}
return err
}, Teardown: func(context.Context) error { close(allowReturn); return nil }})
done := run(a, context.Background())
wait(t, canceled)
signals <- syscall.SIGTERM
if err := receive(t, done); !errors.Is(err, context.Canceled) || !strings.Contains(err.Error(), "other") {
t.Fatalf("independent cancellation lost: %v", err)
}
}