From 0b21448e5a5e3ee78c0750cdbf0e7cd4017662e5 Mon Sep 17 00:00:00 2001 From: Radu Berinde Date: Mon, 6 Oct 2025 08:45:23 -0700 Subject: [PATCH] fix 10s logging data race The goroutine that logs every 10 seconds can race with the function exiting, which can lead to a data race or even a panic because of t.Log used after the test exits. The fix is to block until the goroutine is exiting, by writing to the unbuffered channel instead of closing it. --- datadriven.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/datadriven.go b/datadriven.go index a98048a..7437cca 100644 --- a/datadriven.go +++ b/datadriven.go @@ -347,7 +347,6 @@ func runDirective(t testing.TB, r *testDataReader, f func(testing.TB, *TestData) // Set up a goroutine to log periodically if the function is taking a long // time. This is useful to pinpoint the cause of a test timeout. done := make(chan struct{}) - defer close(done) go func() { startTime := time.Now() for { @@ -359,6 +358,11 @@ func runDirective(t testing.TB, r *testDataReader, f func(testing.TB, *TestData) } } }() + defer func() { + // Because the channel is unbuffered, we wait here until the goroutine is + // exiting. + done <- struct{}{} + }() actual := f(t, d) fReturned = true