diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index c3b8e793..5c10a5b0 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -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) diff --git a/internal/selfcompose/compose_while_passing_test.go b/internal/selfcompose/compose_while_passing_test.go index d755ae2d..d78c4cbd 100644 --- a/internal/selfcompose/compose_while_passing_test.go +++ b/internal/selfcompose/compose_while_passing_test.go @@ -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") + } +} diff --git a/internal/selfcompose/composedstate_test.go b/internal/selfcompose/composedstate_test.go index 5de5c96f..104216c0 100644 --- a/internal/selfcompose/composedstate_test.go +++ b/internal/selfcompose/composedstate_test.go @@ -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) diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index cf265bfd..4c7d33ba 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -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