node-tools: a stopping tool runner takes its subscriptions down, so callers meet no responders (issue 721)

The runner answered each call it did not take once its stop began with the typed fault
stopping (#90). Callers built before mesh-sdk read it as the callee's refusal and ended their
kept call, and no edge delivered the new answer after its readers. Now, once told to stop,
the runner first drains every subscription it answers calls on (a tool's, a seat verb's and
a seat proof's) and subscribes none again, then finishes the calls in flight within
drain-bound. A call made after the stop meets no responders at once, a transport fault every
caller of every mesh-sdk already waits out, until the new runner subscribes. A call that
still reached it is answered nothing, as H-A-369 has it. The typed answer goes whole: the
reply's error-kind, KindedError, ErrorKindStopping and the bundle's error data.

Tests: 721/AC4 in the bus (a call and a proof after the stop meet no responders within a
second, the call in flight is answered, a subject served after the stop is refused) and in
launch (a call that still reaches the runner gets no answer and no typed fault); the whole
test meets no responders while a call is still in flight, before the drain bound.

Builds: design 47 H2 H-A-570 (1), (3) (mesh-tools' part), amending §813 (12) as H-A-369 has it
This commit is contained in:
2026-11-23 16:35:15 +01:00
parent e8f58540ee
commit 662b05ca5a
9 changed files with 162 additions and 123 deletions
+52 -32
View File
@@ -200,6 +200,11 @@ type Conn struct {
// answering is every subject this runtime answers on, so a subscription the bus refused can be
// asked for again (novox/hq issue 222).
answering map[string]*answered
// proofs are the subscriptions that answer a seat's proofs (SeatAnswer), so the stop takes them down with the rest.
proofs map[*nats.Subscription]bool
// answersDown is set once the runner took down every subscription it answers calls on at its stop (StopAnswering):
// from then on it subscribes none again, so every later call meets no responders here (design 47 H2 H-A-570 (1)).
answersDown bool
// RetryAfter is how long after a refusal a subscription is asked for again, by attempt; past the
// last, it is given up and said. A variable so a test need not wait minutes.
RetryAfter []time.Duration
@@ -292,7 +297,7 @@ func (c *Conn) refused(err error) {
time.AfterFunc(wait, func() {
c.mu.Lock()
defer c.mu.Unlock()
if a.stopped {
if a.stopped || c.answersDown {
return
}
if a.sub != nil {
@@ -688,28 +693,7 @@ func (c *Conn) servedOn(module, tool string) []Served {
type reply struct {
Result any `json:"result,omitempty"`
Error string `json:"error,omitempty"`
// ErrorKind is the typed fault beside Error, when the handler's error names one (ErrorKind() string): a stopping
// tool runner's stopping (design 47 H2 H-A-542).
ErrorKind string `json:"error-kind,omitempty"`
Node string `json:"node,omitempty"`
}
// KindedError is an answer's error with its typed fault (`error-kind`, design 47 H2 H-A-542): a stopping tool runner's
// stopping. Its words are the answerer's; ErrorKind is told apart by its kind, never by its words.
type KindedError struct{ Words, Kind string }
func (e *KindedError) Error() string { return e.Words }
// ErrorKind is the typed fault the answer named.
func (e *KindedError) ErrorKind() string { return e.Kind }
// errorKindOf is the typed fault err names (ErrorKind() string), "" for none.
func errorKindOf(err error) string {
var kinded interface{ ErrorKind() string }
if errors.As(err, &kinded) {
return kinded.ErrorKind()
}
return ""
Node string `json:"node,omitempty"`
}
// answerOn answers one subject with one handler, and says which machine answered (ADR 0159).
@@ -736,7 +720,7 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e
return
}
if err != nil {
r.Error, r.ErrorKind = err.Error(), errorKindOf(err)
r.Error = err.Error()
} else {
r.Result = nullable(result)
}
@@ -749,6 +733,9 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e
}
}()
}
if err := c.answersAreDown(subject); err != nil {
return func() {}, err
}
var sub *nats.Subscription
var err error
if queue != "" {
@@ -971,18 +958,13 @@ func (c *Conn) askOnce(subject string, data []byte, within time.Duration) (Answe
// answerOf reads one answer.
func answerOf(data []byte) (Answered, error) {
var r struct {
Result json.RawMessage `json:"result"`
Error string `json:"error"`
ErrorKind string `json:"error-kind"`
Node string `json:"node"`
Result json.RawMessage `json:"result"`
Error string `json:"error"`
Node string `json:"node"`
}
if err := json.Unmarshal(data, &r); err != nil {
return Answered{}, err
}
if r.Error != "" && r.ErrorKind != "" {
// A typed fault (a stopping tool runner's stopping, design 47 H2 H-A-542) reaches the caller as such.
return Answered{}, &KindedError{Words: r.Error, Kind: r.ErrorKind}
}
if r.Error != "" {
return Answered{}, errors.New(r.Error)
}
@@ -1118,6 +1100,44 @@ func (c *Conn) MaxPayload() int64 { return c.nc.MaxPayload() }
// when this returns.
func (c *Conn) Flush() { _ = c.nc.Flush() }
// StopAnswering is the runner's first act once told to stop, before it finishes the calls in flight (design 47 H2 H-A-570
// (1)): it unsubscribes from every subject it answers calls on, a tool's, a seat verb's and a seat proof's, by the bus's
// own drain of each subscription, and subscribes none again, so a call made from now on meets no responders at once
// wherever no other tool runner serves it, until the new runner subscribes. A call already taken is answered as before:
// a reply needs no subscription. It returns once the bus holds the unsubscriptions.
func (c *Conn) StopAnswering() {
c.mu.Lock()
c.answersDown = true
var subs []*nats.Subscription
for _, a := range c.answering {
a.stopped = true
if a.sub != nil {
subs = append(subs, a.sub)
}
}
for s := range c.proofs {
subs = append(subs, s)
}
c.mu.Unlock()
for _, s := range subs {
if err := s.Drain(); err != nil && !errors.Is(err, nats.ErrBadSubscription) {
c.Logf("[mesh-tools] at its stop the tool runner could not take down its subscription to %s: %v", s.Subject, err)
}
}
_ = c.nc.Flush()
}
// answersAreDown refuses a new subscription that would answer calls once the runner took its subscriptions down at its stop
// (StopAnswering, design 47 H2 H-A-570 (1)).
func (c *Conn) answersAreDown(subject string) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.answersDown {
return fmt.Errorf("%s is not served: at its stop the tool runner took down every subscription it answers calls on", subject)
}
return nil
}
// Close unsubscribes everything and drains, so an in-flight reply is finished rather than dropped.
func (c *Conn) Close() {
c.mu.Lock()
+33 -19
View File
@@ -244,22 +244,20 @@ func TestACallWaitsTheBoundItsVerbNamesAndTimesOutThere(t *testing.T) {
}
}
// stoppingFault is the error a stopping tool runner's drain hands for a call it does not take (launch's afterStop).
type stoppingFault struct{}
func (stoppingFault) Error() string { return "not taken: the tool runner is stopping" }
func (stoppingFault) ErrorKind() string { return "stopping" }
// A call a stopping tool runner does not take is answered the typed fault stopping, never silence, and its caller is
// handed that kind as such, beside the answerer's words; a plain refusal carries no kind (design 47 H2 H-A-542).
func TestAStoppingRunnersFaultReachesItsCallerByItsKind(t *testing.T) {
// 721/AC4: once told to stop, the tool runner unsubscribes from every subject it answers calls on before it finishes the
// calls in flight (design 47 H2 H-A-570 (1)): a call heard after the stop began gets no answer from it, no typed fault, and its
// caller meets no responders at once; the call in flight at the stop is still answered; a subject served once the stop began
// is not subscribed, and says so.
func TestARunnerAtItsStopAnswersNoCallAndItsCallersMeetNoResponders(t *testing.T) {
url := mt.URL(t)
server := dial(t, url, "ledger", "anchor", "")
began, release := make(chan struct{}, 1), make(chan struct{})
stop, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.who", func(_, _ string, body json.RawMessage) (any, error) {
if strings.Contains(string(body), "plain") {
return nil, errors.New("ledger keeps no such key")
if strings.Contains(string(body), "held") {
began <- struct{}{}
<-release
}
return nil, fmt.Errorf("ledger's who: %w", stoppingFault{})
return map[string]any{"who": "ledger"}, nil
})
if err != nil {
t.Fatal(err)
@@ -267,13 +265,29 @@ func TestAStoppingRunnersFaultReachesItsCallerByItsKind(t *testing.T) {
defer stop()
server.Flush()
asker := dial(t, url, "node-tools", "shanks", "shanks.node-tools")
_, err = asker.Ask("ledger.who", map[string]any{}, "")
var kinded interface{ ErrorKind() string }
if !errors.As(err, &kinded) || kinded.ErrorKind() != "stopping" || !strings.Contains(err.Error(), "the tool runner is stopping") {
t.Errorf("the call was answered %v, want the typed fault stopping with the answerer's words", err)
held := make(chan error, 1)
go func() {
_, err := asker.AskWithin("ledger.who", map[string]any{"held": true}, "", 10*time.Second)
held <- err
}()
select {
case <-began:
case <-time.After(10 * time.Second):
t.Fatal("the call to hold never reached the runner")
}
_, err = asker.Ask("ledger.who", map[string]any{"plain": true}, "")
if err == nil || errors.As(err, &kinded) || err.Error() != "ledger keeps no such key" {
t.Errorf("a plain refusal was answered %v, want its words and no kind", err)
server.StopAnswering()
at := time.Now()
answered, err := asker.AskWithin("ledger.who", map[string]any{}, "", 5*time.Second)
if took := time.Since(at); !errors.Is(err, bus.ErrNoResponders) || took > time.Second {
t.Errorf("a call after the stop began was answered %s, %v in %s; want no responders at once", answered.Result, err, took)
}
if _, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.late", func(string, string, json.RawMessage) (any, error) {
return nil, nil
}); err == nil || !strings.Contains(err.Error(), "took down every subscription") {
t.Errorf("a subject served once the stop began was subscribed (%v), want it refused and said", err)
}
close(release)
if err := <-held; err != nil {
t.Errorf("the call in flight at the stop ended %v, want it answered", err)
}
}
+23 -11
View File
@@ -195,8 +195,8 @@ func (c *Conn) SeatProve(module, subject string, body json.RawMessage) (json.Raw
}
// SeatAnswer answers every proof on a subject the module's seat traffic lists among its answers, with what
// answer returns. A returned error is answered as `{"error": "<words>"}`, with its `error-kind` when it names one (a proof
// after the stop: stopping, design 47 H2 H-A-542), never as silence, but for one that says it is unanswered (cut at the bound).
// answer returns. A returned error is answered as `{"error": "<words>"}`, never as silence, but for one that says it is
// unanswered (a proof after the stop or cut at the bound). The runner's stop takes the subscription down (StopAnswering).
func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, body json.RawMessage) (json.RawMessage, error)) (func(), error) {
t, err := c.trafficOf(module)
if err != nil {
@@ -210,6 +210,9 @@ func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, bo
return nil, fmt.Errorf("%s does not answer %s: its membership lists %s (novox/hq ADR 0259)", module,
subject, strings.Join(t.Answers, ", "))
}
if err := c.answersAreDown(subject); err != nil {
return nil, err
}
sub, err := c.nc.Subscribe(subject, func(msg *nats.Msg) {
if msg.Reply == "" {
return
@@ -220,19 +223,14 @@ func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, bo
return
}
out, err := answer(msg.Subject, body)
// A proof cut at the runner's drain bound is answered nothing, as answerCalledOn does (design 47 H2 §813 (12),
// H-A-369); one after the stop is answered stopping below (H-A-542).
// A proof arriving after the runner's stop, or cut at its bound, is answered nothing, as answerCalledOn does
// (design 47 H2 §813 (12), H-A-369).
var unanswered interface{ Unanswered() bool }
if errors.As(err, &unanswered) && unanswered.Unanswered() {
return
}
if err != nil {
said := map[string]string{"error": err.Error()}
// A typed fault beside the words: a stopping tool runner's stopping (design 47 H2 H-A-542).
if kind := errorKindOf(err); kind != "" {
said["error-kind"] = kind
}
b, _ := json.Marshal(said)
b, _ := json.Marshal(map[string]string{"error": err.Error()})
_ = msg.Respond(b)
return
}
@@ -247,8 +245,22 @@ func (c *Conn) SeatAnswer(module, subject string, answer func(subject string, bo
return nil, err
}
c.track(sub)
// Kept among the proofs the stop takes down (design 47 H2 H-A-570 (1)).
c.mu.Lock()
if c.proofs == nil {
c.proofs = map[*nats.Subscription]bool{}
}
c.proofs[sub] = true
c.mu.Unlock()
var once sync.Once
return func() { once.Do(func() { _ = sub.Unsubscribe() }) }, nil
return func() {
once.Do(func() {
c.mu.Lock()
delete(c.proofs, sub)
c.mu.Unlock()
_ = sub.Unsubscribe()
})
}, nil
}
// Work is one message a holder took from a seat's work queue.
+12 -8
View File
@@ -181,9 +181,6 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
if string(body) == `{"cut":true}` {
return nil, unansweredProof{}
}
if string(body) == `{"stopping":true}` {
return nil, stoppingFault{}
}
return json.RawMessage(`{"linked":true}`), nil
})
if err != nil {
@@ -198,11 +195,6 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
if string(answer) != `{"linked":true}` {
t.Fatalf("the proof was answered %s", answer)
}
// A proof after the stop is answered the typed fault stopping, never silence (design 47 H2 H-A-542).
if raw, err := conn.SeatProve("telegram", "mesh.seat.intake.proof.code.telegram", json.RawMessage(`{"stopping":true}`)); err != nil ||
!strings.Contains(string(raw), `"error-kind":"stopping"`) {
t.Errorf("a proof after the stop was answered %s, %v; want its error-kind stopping", raw, err)
}
// A proof arriving after the stop, or cut at the bound, is answered nothing (design 47 H2 §813 (12), H-A-369): the
// answerer handles proofs in order, so the first reply on one inbox after a cut proof and a whole one is the whole one's.
raw, err := nats.Connect(mt.URL(t))
@@ -237,6 +229,18 @@ func TestAProofIsAskedAndAnsweredAndNeverKept(t *testing.T) {
}
}
}
// 721/AC4: at its stop the runner takes its proofs' subscriptions down with the rest (design 47 H2 H-A-570 (1)): a proof
// after it meets no responders, never an answer of the old runner's, and a proof answered after it is not subscribed.
conn.StopAnswering()
if raw, err := conn.SeatProve("telegram", "mesh.seat.intake.proof.code.telegram", json.RawMessage(`{"code":"123456"}`)); err == nil ||
!strings.Contains(err.Error(), "nothing answers") {
t.Errorf("a proof after the stop was answered %s, %v; want no responders", raw, err)
}
if _, err := conn.SeatAnswer("messenger", "mesh.seat.intake.proof.code.*", func(string, json.RawMessage) (json.RawMessage, error) {
return nil, nil
}); err == nil {
t.Error("a proof answered after the stop was subscribed")
}
}
func TestAnAskerReadsItsOwnRecordAndNoOther(t *testing.T) {