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
+15 -11
View File
@@ -427,29 +427,33 @@ func (c *Conn) issue(module string, m Membership) {
// right after its boot, a timeout), is said once and read again, with a growing wait, until it can be or the
// connection closes: Follow runs once per module, so nothing else would ever read it again.
func (c *Conn) followGrants(module string) {
if c.watchGrants(module, true) {
// Read here, not in the loop: the loop outlives Follow, and a test sets them back as it ends (design 47 H, H-A-363).
patience, timeout := GrantsPatience, StateTimeout
if c.watchGrants(module, true, patience, timeout) {
return
}
wait, most := GrantsRetry, GrantsRetryMost
go func() {
wait := GrantsRetry
for !c.nc.IsClosed() {
time.Sleep(wait)
if c.nc.IsClosed() {
return
}
if c.watchGrants(module, false) {
if c.watchGrants(module, false, patience, timeout) {
c.Logf("[mesh-tools] the grants part's memberships are read again; %s on %s follows its key", module, c.node)
return
}
if wait *= 2; wait > GrantsRetryMost {
wait = GrantsRetryMost
if wait *= 2; wait > most {
wait = most
}
}
}()
}
// watchGrants is one attempt of followGrants: true once the key is watched. A failure is said when say is true.
func (c *Conn) watchGrants(module string, say bool) bool {
// watchGrants is one attempt of followGrants: true once the key is watched. A failure is said when say is true. Its
// bounds, patience for the key's direct get and timeout for the bucket and its current value, are GrantsPatience and
// StateTimeout as they stood when followGrants began.
func (c *Conn) watchGrants(module string, say bool, patience, timeout time.Duration) bool {
logf := func(format string, a ...any) {
if say {
c.Logf(format, a...)
@@ -459,7 +463,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
// First the key's direct get, which the account is granted alone: a bus that refuses it, or a state not there yet,
// answers at once, where binding the bucket would wait out its bound on a refusal.
bucket := membership.GrantsBucket
probe, err := c.nc.Request("$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+key, nil, GrantsPatience)
probe, err := c.nc.Request("$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+key, nil, patience)
if err != nil || probe.Header.Get("Status") == "503" {
why := "it did not answer"
if err != nil {
@@ -473,7 +477,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
if err != nil {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), StateTimeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
kv, err := js.KeyValue(ctx, bucket)
if err != nil {
@@ -506,7 +510,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
}
c.issue(module, m)
}
timer := time.NewTimer(StateTimeout)
timer := time.NewTimer(timeout)
defer timer.Stop()
for current := true; current; {
select {
@@ -522,7 +526,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
case <-timer.C:
_ = w.Stop()
logf("[mesh-tools] %s's key %s of the grants part's memberships did not answer in %s; %s has no membership "+
"until it can be", module, key, StateTimeout, module)
"until it can be", module, key, timeout, module)
return false
}
}