From 8a2d153817d97a7fccc64fa683df6d8259fb1a52 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Thu, 26 Feb 2026 08:44:18 +0100 Subject: [PATCH 1/2] ktesting: fix potential log panic If the goroutine happens to log after the test has already terminated, testing.T.Log panics. We must ensure that the goroutine has stopped before allowing the test to terminate. --- test/utils/ktesting/contexthelper.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/utils/ktesting/contexthelper.go b/test/utils/ktesting/contexthelper.go index 6347211e0a7..b6f6263a4a8 100644 --- a/test/utils/ktesting/contexthelper.go +++ b/test/utils/ktesting/contexthelper.go @@ -48,11 +48,17 @@ func withTimeout(ctx context.Context, tb TB, timeout time.Duration, timeoutCause cancelCtx, cancel := context.WithCancelCause(ctx) after := time.NewTimer(timeout) stopCtx, stop := context.WithCancel(ctx) // Only used internally, doesn't need a cause. + done := make(chan struct{}) tb.Cleanup(func() { cancel(cleanupErr(tb.Name())) stop() + // Wait for goroutine termination. This is important because + // otherwise the goroutine might log through tb *after* the + // test has already finished, which causes a panic. + <-done }) go func() { + defer close(done) select { case <-stopCtx.Done(): after.Stop() From 620c1b6305903ccd0b2904a77d812086a6f3d3ee Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Thu, 26 Feb 2026 08:45:35 +0100 Subject: [PATCH 2/2] ktesting: fix potential unit test flake I've not been able to trigger the flake, but it could happen: - time.Sleep unblocks some background goroutines inside the synctest bubble. - Those goroutines do not actually run yet. - The main test checks for the result of those goroutines. Adding a `synctest.Wait` ensures that all background processing is complete because it waits for all goroutines to be durably blocked. --- test/utils/ktesting/contexthelper_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/utils/ktesting/contexthelper_test.go b/test/utils/ktesting/contexthelper_test.go index 721d2eada55..6ed9c5ce52c 100644 --- a/test/utils/ktesting/contexthelper_test.go +++ b/test/utils/ktesting/contexthelper_test.go @@ -119,7 +119,11 @@ func TestCause(t *testing.T) { assert.Equal(t, tt.expectDeadline, time.Until(actualDeadline), "remaining time till Deadline()") } } + // Unblock background goroutines. time.Sleep(tt.sleep) + // Wait for them to do their work. + synctest.Wait() + // Now check. actualErr := ctx.Err() actualCause := context.Cause(ctx) assert.Equal(t, tt.expectErr, actualErr, "ctx.Err()")