H-A-480 b: the composer's watch ends with the agent's stop

The composer's loop and its hearers outlived the node agent's run and logged after
it ended; the node agent now waits for them (Composer.Wait) once its run stops.

Builds: design 47 H2 H-A-480
This commit is contained in:
2026-11-22 16:12:10 +01:00
parent cb5eddeba8
commit a22769bbc6
4 changed files with 46 additions and 6 deletions
+1
View File
@@ -796,6 +796,7 @@ func nodeAgent(ctx context.Context, opts options, signer ed25519.PublicKey, trus
err = ag.Run(run)
stopRun()
cancel()
composer.Wait() // its watch ends with the agent's stop, and says nothing after it (H-A-480)
current.set(nil)
composerNow.Store(nil)
ownFindingsNow.Store(nil)
@@ -79,3 +79,25 @@ func composeAnsweredWhileAPassRuns(t *testing.T) {
t.Fatal("the queued pass was not made when the running one ended")
}
}
// H-A-480: the composer's watch ends with the agent's stop: once its ctx ended, Wait returns only after its loop ended,
// so nothing it says comes after the run (the integration tests at -count=15 logged after their test completed).
func theWatchEndsWithItsRun(t *testing.T) {
b := newFakeBus()
ctx, cancel := context.WithCancel(context.Background())
c := New(b.env())
out, err := c.Watch(ctx, "n1.>")
if err != nil {
t.Fatal(err)
}
cancel()
c.Wait()
select {
case _, ok := <-out:
for ok {
_, ok = <-out
}
default:
t.Fatal("the composer's watch went on after Wait returned")
}
}
@@ -91,6 +91,7 @@ func (f *fakeComposed) keep(t *testing.T, half catalogHalf, module, fp string) {
// by a want, and deleted otherwise.
func TestTheNodePublishesItsCompositions(t *testing.T) {
t.Run("the verb compose is answered while a pass runs", composeAnsweredWhileAPassRuns)
t.Run("the watch ends with its run", theWatchEndsWithItsRun)
b := newFakeBus()
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
ofp := meshcatalogclient.SourcePrefix2 + strings.Repeat("c", 64)
+22 -6
View File
@@ -197,6 +197,8 @@ type Composer struct {
AlsoEntriesAskAgain func(ctx context.Context)
env Env
// watchers counts the goroutines Watch started that end with its ctx (its loop and its hearers), for Wait (H-A-480).
watchers sync.WaitGroup
mu sync.Mutex
wants map[string]meshwants.WantsByWantNodeModuleValue // by module, the node's own under agent.NodeModule
assignments map[string]meshmoduleassigner.Assignments
@@ -582,12 +584,16 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}(i, ch)
}
if c.env.Hear != nil {
go c.hearCatalogRevisions(ctx, func() {
select {
case merged <- tagged{in: -1, ok: true}:
case <-ctx.Done():
}
})
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
c.hearCatalogRevisions(ctx, func() {
select {
case merged <- tagged{in: -1, ok: true}:
case <-ctx.Done():
}
})
}()
heard, stop, err := c.env.Hear(VaultSecretChanged)
if err != nil {
return nil, fmt.Errorf("the node composes again when a secret changes, and cannot hear the vault say so: %w", err)
@@ -604,7 +610,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
heard <-chan []byte
stop func()
}{{heard, stop}, {changed, stopChanged}} {
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
defer h.stop()
for {
select {
@@ -625,7 +633,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}
}
if c.env.SeatListChanged != nil {
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
for {
select {
case <-ctx.Done():
@@ -645,7 +655,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}
out := make(chan agent.Update, 64)
c.watching.Store(true)
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
defer close(out)
defer c.watching.Store(false)
caught := make([]bool, len(ins))
@@ -761,6 +773,10 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
return out, nil
}
// Wait waits until the loop and the hearers Watch started have ended, each once its ctx ended: the node agent's stop waits for it,
// so the composer says nothing once its run is over (design 47 H2 H-A-480).
func (c *Composer) Wait() { c.watchers.Wait() }
// hearCatalogRevisions hears the seat mesh-catalog's event catalog-revision: each one asks again the entries whose ask failed (H-A-110), clears the backoffs of the trunk builds the
// catalog gave none of and wakes a pass (A-487a). A wake-up only, the backoff bounds the asks without it: a subscription refused
// is said once with its subject and error (principle 3), the composer goes on composing, and the subscription is tried again on