Tool runner bus: long-lived loops read their settings once, as they start

A hearing's loops, SeatTake's heartbeat and followGrants' retry loop outlive the
call that started them and read the package's settings on every turn; a test
that sets a setting back as it ends then raced the loop (the race detector:
streams_test.go:232's cleanup write of RebindEvery against the rebind loop's
read, in TestAConsumerNamedBeforeItExistsIsBoundOnceItDoes at -count=30). Each
loop now takes RebindEvery, NakDelay, EventHeartbeat, WorkHeartbeat,
GrantsRetry, GrantsRetryMost, GrantsPatience and StateTimeout once, as consumes
already took IdleHeartbeat and its tick hook.

Builds: design 47 H H-A-363 (fix-a-bug-met-twice: a goroutine outliving its settings)
This commit is contained in:
2026-11-21 23:40:18 +01:00
parent a75d86236b
commit 537e44eff6
3 changed files with 29 additions and 17 deletions
+11 -5
View File
@@ -106,12 +106,18 @@ type hearing struct {
kick chan struct{}
done chan struct{}
unhear func()
// rebind, nakDelay and heartbeat are RebindEvery, NakDelay and EventHeartbeat as they stood when the hearing was
// made: its loops outlive the call that started them, so they never read a setting again, which a test may set
// back as it ends (design 47 H, H-A-363: a goroutine outliving its settings).
rebind, nakDelay, heartbeat time.Duration
}
func newHearing(c *Conn, js jetstream.JetStream, module string, deliver func(Envelope) error) *hearing {
return &hearing{c: c, js: js, module: module, deliver: deliver,
bound: map[HeardIssued]jetstream.ConsumeContext{}, made: map[HeardIssued]time.Time{}, said: map[HeardIssued]bool{}, why: map[HeardIssued]error{},
kick: make(chan struct{}, 1), done: make(chan struct{})}
kick: make(chan struct{}, 1), done: make(chan struct{}),
rebind: RebindEvery, nakDelay: NakDelay, heartbeat: EventHeartbeat}
}
// bind reads one durable consumer by name, and on failure says what it was doing. Errors the bus gives
@@ -158,7 +164,7 @@ func (h *hearing) handle(msg jetstream.Msg) {
done, stopped := make(chan struct{}), make(chan struct{})
go func() {
defer close(stopped)
tick := time.NewTicker(EventHeartbeat)
tick := time.NewTicker(h.heartbeat)
defer tick.Stop()
for {
select {
@@ -186,7 +192,7 @@ func (h *hearing) handle(msg jetstream.Msg) {
return
}
if err != nil {
_ = msg.NakWithDelay(NakDelay)
_ = msg.NakWithDelay(h.nakDelay)
return
}
_ = msg.Ack()
@@ -203,7 +209,7 @@ func (h *hearing) start() {
h.nudge()
})
go func() {
tick := time.NewTicker(RebindEvery)
tick := time.NewTicker(h.rebind)
defer tick.Stop()
for {
select {
@@ -353,7 +359,7 @@ func (h *hearing) reconcile() {
// Said once, not at every attempt; said again when it is bound.
if !h.said[heard] {
h.c.Logf("[mesh-tools] %s does not hear %s yet: %s its consumer %s failed: %v; tried again every %s",
h.module, heard.Stream, doing, heard.Consumer, err, RebindEvery)
h.module, heard.Stream, doing, heard.Consumer, err, h.rebind)
h.said[heard] = true
}
continue