diff --git a/node-tools/internal/bus/bus.go b/node-tools/internal/bus/bus.go index 9997853..726dc0c 100644 --- a/node-tools/internal/bus/bus.go +++ b/node-tools/internal/bus/bus.go @@ -740,25 +740,35 @@ func (c *Conn) HandleCalled(key string, h CalledHandler) (func(), error) { "those of the modules it follows", c.self, key) } var mu sync.Mutex - var stops []func() + stops := map[Served]func(){} stopped := false + // **Make before break** (novox/hq design 47 H, H-A-158): a membership issued again keeps every subscription + // whose subject and queue it still names, subscribes those it newly names, and only then stops those it no + // longer names — a call the bus routes while the membership is followed is never dropped in silence. serve := func() { mu.Lock() defer mu.Unlock() if stopped { return } - for _, s := range stops { - s() - } - stops = nil + named := map[Served]bool{} for _, s := range c.servedOn(module, tool) { + named[s] = true + if _, kept := stops[s]; kept { + continue + } if stop, err := c.answerBoth(s.Subject, s.Queue, h); err == nil { - stops = append(stops, stop) + stops[s] = stop } else { c.Logf("[mesh-tools] cannot serve %s on %s: %v", key, s.Subject, err) } } + for s, stop := range stops { + if !named[s] { + stop() + delete(stops, s) + } + } } serve() // Its stop drops the membership handler too: a handler left behind would bind the stopped tool again @@ -773,10 +783,10 @@ func (c *Conn) HandleCalled(key string, h CalledHandler) (func(), error) { mu.Lock() defer mu.Unlock() stopped = true - for _, s := range stops { - s() + for s, stop := range stops { + stop() + delete(stops, s) } - stops = nil }, nil } diff --git a/node-tools/internal/runtime/ledgers_test.go b/node-tools/internal/runtime/ledgers_test.go index ff586ef..412809b 100644 --- a/node-tools/internal/runtime/ledgers_test.go +++ b/node-tools/internal/runtime/ledgers_test.go @@ -39,7 +39,7 @@ func TestTheLedgerRepairsAreToolsOfEveryLedgerOwner(t *testing.T) { defer stop() asker := connect(t, "console", "workstation") listed := func(module string) (string, error) { - answered, err := asker.Ask(module+"."+ToolsVerb, map[string]any{}, "") + answered, err := asker.AskWithin(module+"."+ToolsVerb, map[string]any{}, "", callWithin) if err != nil { return "", err } diff --git a/node-tools/internal/runtime/reload_test.go b/node-tools/internal/runtime/reload_test.go index 7375d77..454c0fb 100644 --- a/node-tools/internal/runtime/reload_test.go +++ b/node-tools/internal/runtime/reload_test.go @@ -263,7 +263,9 @@ func TestAReplacedBundleIsReloaded(t *testing.T) { // A replaced bundle's old tool stays gone when the module's membership is issued again: a stopped tool // leaves no membership handler behind to bind it once more, and a call on it starts no unmanaged second -// copy of the bundle (issue 1945, review round 2). +// copy of the bundle (issue 1945, review round 2). And the tool still served answers the first call asked +// after each membership, within the test's own bound: the runner keeps the subscriptions the membership +// still names, so no call is dropped while it follows one (design 47 H, H-A-158). func TestAReplacedBundlesOldToolStaysGoneOnTheNextMembership(t *testing.T) { b := newReloadBed(t) place(t, b.entry, "first") @@ -273,8 +275,13 @@ func TestAReplacedBundlesOldToolStaysGoneOnTheNextMembership(t *testing.T) { b.answers(t, "second") b.silent(t, "first") - b.mesh.Issue(t, mt.MembershipOf("ledger", "anchor", false, nil)) - b.answers(t, "second") + for round := 1; round <= 5; round++ { + b.mesh.Issue(t, mt.MembershipOf("ledger", "anchor", false, nil)) + if got, err := call(t, b.asker, "ledger.second@anchor", map[string]any{}); err != nil || !strings.Contains(got, `"tool":"second"`) { + t.Fatalf("membership %d issued again: the first call of ledger.second answered %s, %v — dropped while the runner "+ + "followed the membership\n%s", round, got, err, b.logs.All()) + } + } // Long enough for the reissued membership to have been heard, before the old tool is asked. time.Sleep(300 * time.Millisecond) if got, err := call(t, b.asker, "ledger.first@anchor", map[string]any{}); err == nil { diff --git a/node-tools/internal/runtime/runtime_test.go b/node-tools/internal/runtime/runtime_test.go index 2d07444..32bc75a 100644 --- a/node-tools/internal/runtime/runtime_test.go +++ b/node-tools/internal/runtime/runtime_test.go @@ -26,9 +26,13 @@ func connect(t *testing.T, module, node string) *bus.Conn { return c } +// callWithin is the tests' own bound on a call (principle 11, design 47 H, H-A-158): a call that is never answered +// fails the try it is part of within it, never after the tool runner's own 30 s. +const callWithin = 2 * time.Second + func call(t *testing.T, asker *bus.Conn, key string, args any) (string, error) { t.Helper() - got, err := asker.Ask(key, args, "") + got, err := asker.AskWithin(key, args, "", callWithin) return string(got.Result), err }