node-tools: a serve racing the runner's stop is taken down too (issue 721, review round 1)

A tool's or a proof's subscription made while the stop took its snapshot was registered
after it, so it stayed subscribed until the close and its callers waited out their bound
instead of meeting no responders (the review's probe: 4 of 2000 rounds). Now a new
answering subscription is kept, and the stop checked again, in one section under the lock
the stop takes its subscriptions down under; when the stop began meanwhile, it is drained
and the serve refused.

Tests: 721/AC4 rows in the bus (a tool's serve and a proof answerer each run the stop
between their subscription and its keeping, through a test hook, and are refused; their
callers meet no responders), failing without the locked check.

Builds: design 47 H2 H-A-570 (1)
This commit is contained in:
2026-11-23 16:51:00 +01:00
parent 662b05ca5a
commit 5d87f7d375
5 changed files with 82 additions and 14 deletions
+41 -5
View File
@@ -205,6 +205,9 @@ type Conn struct {
// 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
// subscribedHook, when a test set it (SetSubscribedHook), is called between making an answering subscription and keeping
// it, so a test runs the stop in between (tests only).
subscribedHook func(subject string)
// 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
@@ -746,11 +749,13 @@ func (c *Conn) answerCalledOn(subject, queue string, h CalledHandler) (func(), e
if err != nil {
return func() {}, err
}
c.track(sub)
c.subscribed(subject)
a := &answered{subject: subject, queue: queue, cb: cb, sub: sub}
c.mu.Lock()
c.answering[answeringKey(subject, queue)] = a
c.mu.Unlock()
// Kept, or taken down, under the one lock the stop takes its subscriptions down under, so a serve racing the stop is
// never missed by it (design 47 H2 H-A-570 (1)).
if err := c.keepAnswering(sub, func() { c.answering[answeringKey(subject, queue)] = a }); err != nil {
return func() {}, err
}
return func() {
c.mu.Lock()
a.stopped = true
@@ -1133,11 +1138,42 @@ 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 answersDownError(subject)
}
return nil
}
func answersDownError(subject string) error {
return fmt.Errorf("%s is not served: at its stop the tool runner took down every subscription it answers calls on", subject)
}
// keepAnswering keeps a subscription just made that answers calls, with keep registering it, in one section under c.mu,
// the lock StopAnswering takes its subscriptions down under: when the stop began meanwhile, it drains the subscription
// instead and refuses it, so none outlives the stop (design 47 H2 H-A-570 (1)).
func (c *Conn) keepAnswering(sub *nats.Subscription, keep func()) error {
c.mu.Lock()
if c.answersDown {
c.mu.Unlock()
_ = sub.Drain()
return answersDownError(sub.Subject)
}
c.subs = append(c.subs, sub)
keep()
c.mu.Unlock()
return nil
}
// subscribed calls the connection's subscribedHook, when a test set one, between making an answering subscription and
// keeping it.
func (c *Conn) subscribed(subject string) {
c.mu.Lock()
hook := c.subscribedHook
c.mu.Unlock()
if hook != nil {
hook(subject)
}
}
// Close unsubscribes everything and drains, so an in-flight reply is finished rather than dropped.
func (c *Conn) Close() {
c.mu.Lock()