Skip to content

Commit da30a54

Browse files
TMHSDigitalclaude
andcommitted
fix(lib): make Run's done grace atomic so tests can change it race-free
TestBreakAndCancelLeaksNothing restored doneGrace while forwarders from abandoned runs were still reading it, which the race detector flagged in CI. Refs #112. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3a15fee commit da30a54

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎pkg/subenum/export_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "time"
55
// SetDoneGrace shortens how long Run waits to deliver the final event after
66
// cancellation, so leak tests need not wait; it returns a restore function.
77
func SetDoneGrace(d time.Duration) func() {
8-
old := doneGrace
9-
doneGrace = d
10-
return func() { doneGrace = old }
8+
old := doneGrace.Swap(int64(d))
9+
return func() { doneGrace.Store(old) }
1110
}

‎pkg/subenum/subenum.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"slices"
2323
"strings"
2424
"sync"
25+
"sync/atomic"
2526
"time"
2627

2728
"github.com/TMHSDigital/subenum/data"
@@ -318,7 +319,9 @@ var defaultWords = sync.OnceValue(func() []string {
318319

319320
// doneGrace is how long Run's forwarder waits to hand over the final event
320321
// after ctx is cancelled, when the caller may have stopped reading.
321-
var doneGrace = time.Second
322+
var doneGrace atomic.Int64 // a time.Duration; atomic so tests can change it while forwarders run
323+
324+
func init() { doneGrace.Store(int64(time.Second)) }
322325

323326
// ErrNoWords is returned when a Config's wordlist has no usable entries.
324327
var ErrNoWords = errors.New("subenum: wordlist has no usable entries")
@@ -382,7 +385,7 @@ func forward(ctx context.Context, out chan<- Event, ev Event) {
382385
}
383386
return
384387
}
385-
timer := time.NewTimer(doneGrace)
388+
timer := time.NewTimer(time.Duration(doneGrace.Load()))
386389
defer timer.Stop()
387390
select {
388391
case out <- ev:

0 commit comments

Comments
 (0)