The node-engine's self-compose test lets no goroutine outlive a setting it reads (fix-a-bug-met-twice) #247

Merged
mesh-tools merged 3 commits from fix/47-H1-selfcompose-races into main 2026-11-21 22:07:14 +00:00
+50 -12
View File
@@ -236,7 +236,20 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
env := b.env()
env.Entry = half.call()
// The ghost's entry (issue 1966's step below) is answered only once that step has read it as asked: answered at once,
// a pass could read it as entry-missing before the assertion (review round 2; the race H-A-303's review saw too).
ghostFp := meshcatalogclient.SourcePrefix2 + strings.Repeat("8", 64)
ghostRead := make(chan struct{})
var ghostOnce sync.Once
readGhost := func() { ghostOnce.Do(func() { close(ghostRead) }) }
defer readGhost()
entry := half.call()
env.Entry = func(key string, body any) (json.RawMessage, error) {
if in, ok := body.(meshcatalog.EntryInput); ok && in.Fingerprint == ghostFp {
<-ghostRead
}
return entry(key, body)
}
// The vault's own: keeper's password sealed to n1's key, sealed again (other bytes) at every ask.
var asks, generation int
sealedAt := map[string]int{} // each box handed, by the generation it opens to
@@ -267,6 +280,14 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// The composer's loop has ended (its out closed) before the test's cleanup restores composeAgain, laterAgain,
// passSaysEvery and slowAfter, which its passes read (fix-a-bug-met-twice: a goroutine outliving the setting it reads,
// as the shadows below).
defer func() {
cancel()
for range out {
}
}()
// oldie is placed and no want names it (a module at a build of the old form, nats on novox): the node runs it inside
// its own composition, and composes it there again, even before the agent has applied anything (Running empty).
oldie := `{"module":"oldie","version":"1","capabilities":["systemd"],"resources":[{"id":"service","type":"service","unit":"oldie.service","state":"running","boot":"enabled"}]}`
@@ -338,9 +359,9 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
b.mu.Unlock()
// Issue 774, 2026-10-31: the verb compose makes a pass now and says how it went; a composer that runs no watch
// makes none, and says so rather than waiting.
// The pass made at the start is less than askedWithin old for a moment: the ask waits that out first.
setFor(t, &askedWithin, 200*time.Millisecond)
time.Sleep(250 * time.Millisecond)
// The pass made at the start is the answer while it is less than askedWithin old: with none counted recent, the ask
// makes a pass (no real wait for the start's pass to age, review round 2).
setFor(t, &askedWithin, 0)
madeBefore := composed.Load()
cctx, ccancel := context.WithTimeout(ctx, 5*time.Second)
nowPass, err := c.ComposeNow(cctx)
@@ -349,6 +370,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
t.Errorf("compose now: %+v (%v), %d pass(es) made; want one pass, composed", nowPass, err, composed.Load()-madeBefore)
}
// Asked again at once, the last pass is the answer: a caller in a loop drives no passes back to back (review round 1).
setFor(t, &askedWithin, time.Hour)
madeBefore = composed.Load()
if again, err := c.ComposeNow(ctx); err != nil || !again.Earlier || !again.At.Equal(nowPass.At) || composed.Load() != madeBefore {
t.Errorf("compose now asked again at once: %+v (%v), %d pass(es) made; want the last pass, none made", again, err, composed.Load()-madeBefore)
@@ -370,7 +392,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
// Issue 1966 (novox, 2026-11-09): every pass names each wanted module it composed nothing of, or composed from the indexer's
// manifest for want of a done build, with why, and a want it did not take; said once; the agent's word that it waits
// hands a composition the node believes handed again.
tfp, ghostFp := toolFp, meshcatalogclient.SourcePrefix2+strings.Repeat("8", 64)
tfp := toolFp
c.holdEntry(t, "tool", tfp, toolBody)
for module, w := range map[string]meshwants.WantsByWantNodeModuleValue{"tool": {Fp: tfp, Self: true}, "ghost": {Fp: ghostFp, Self: true}, "nofp": {Self: true}} {
raw, _ := bucketstest.Want(w)
@@ -398,6 +420,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
!strings.Contains(why["nofp"], "want not taken: its want names no fingerprint") || why["lemurs"] != "" {
t.Errorf("the pass says it composed nothing of %q", why)
}
readGhost()
c.compose(ctx)
if n := said("the node composes nothing of nofp"); n != saidOfNofp || n == 0 {
t.Errorf("the journal says why nofp is not composed %d times after the second pass, %d after the first: want the same, not none", n, saidOfNofp)
@@ -1277,7 +1300,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
}
}
readOnly.mu.Unlock()
for group, want := range map[string]func(nodeengine.CandidateAnswer) bool{
groups := map[string]func(nodeengine.CandidateAnswer) bool{
"same": func(a nodeengine.CandidateAnswer) bool {
return a.Verdict == nodeengine.CandidateAnswerVerdictSame && a.Proposal == "same" && a.Revision == 3 && len(a.Same) == 2
},
@@ -1312,11 +1335,19 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
return a.Verdict == nodeengine.CandidateAnswerVerdictRefused && a.Returns == "" && a.Revision == 7 &&
strings.Contains(a.Refused, "composes nothing")
},
} {
if !want(answers[group]) {
t.Errorf("%s: %+v", group, answers[group])
}
}
// Answered as each group should be: the shadow answers again while what it reads comes in (what runs, an entry), so an
// answer before the last may still hold lemurs as it runs; the test waits for the answer that settles every group
// (fix-a-bug-met-twice: asserting on whichever answer came first flaked, and the settled one then read as "handed
// again for nothing changed").
waitFor("every group answered as it should be", func(a map[string]nodeengine.CandidateAnswer) bool {
for group, want := range groups {
if !want(a[group]) {
return false
}
}
return true
})
// The node's own key changing (its answers written) composes nothing again; a group's key deleted drops its answer.
sb.put(buckets.Have, "n1.node", rec, 2)
select {
@@ -1431,6 +1462,10 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
return !ok && len(a) == 7
})
// The shadow ends here: its tries read notReadyAfter, which the step below sets (review round 2, the race detector).
scancel()
<-kept
// Issue 1126, A7 AC1: a assignment that cannot be read composes nothing and says why; the node never applies a
// composition made elsewhere instead. (A assignment read without the node's key composes nothing either: above.)
{
@@ -1463,8 +1498,11 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
nanswered := make(chan map[string]nodeengine.CandidateAnswer, 8)
nsh := NewShadow(nenv, func(_ context.Context, a map[string]nodeengine.CandidateAnswer) { nanswered <- a })
nctx, ncancel := context.WithCancel(ctx)
defer ncancel()
go func() { _ = nsh.Run(nctx) }()
// The shadow has ended before the test's cleanup restores notReadyAfter, which its run reads (fix-a-bug-met-twice:
// a goroutine outliving the setting it reads, the race of the first shadow above too).
nDone := make(chan struct{})
defer func() { ncancel(); <-nDone }()
go func() { _ = nsh.Run(nctx); close(nDone) }()
nb.made(6)
nb.putAssigned(tp, 1)
nb.put(buckets.Have, "n1.node", rec, 1)