T2 drain at stop: finish the calls in flight within the drain bound, cut and say past it

Once told to stop, the tool runner makes no new ask of a bundle (a call after the stop is refused),
waits for the asks in flight within the drain bound (30 s by default), cuts each still in flight at
the bound with a journal line naming the bundle, the verb and the call's age, leaves its caller
unanswered (a transport fault), and only then stops its bundles itself.

Builds: design 47 H2 §813 (12), H-A-342 (2), H-A-353
This commit is contained in:
2026-11-21 23:11:38 +01:00
parent 02675a43ea
commit f7269d9812
7 changed files with 374 additions and 13 deletions
+7
View File
@@ -155,6 +155,9 @@ func serve(ctx context.Context) error {
// First of all: the service manager signalled every bundle with this process, and none is started again
// (issue 1126).
launch.ShutDown()
// Then it drains (design 47 H2 §813 (12), H-A-353): no new call is taken, the calls in flight finish within the drain bound
// or are cut and said, and only then are the bundles stopped, by the runner itself.
launch.Drain(drainBound, log.Printf)
stopSaying()
stopProviding()
stop()
@@ -165,6 +168,10 @@ func serve(ctx context.Context) error {
return nil
}
// drainBound is how long the runner, told to stop, waits for the calls in flight to finish: the setting drain-bound (design 47
// H2 §813 (12)).
var drainBound = launch.DefaultDrainBound
// startWait is how long a runtime waits, from its start, for its broker credential to be on the machine and for the bus
// to accept the account it names (issue 1496): the node-engine starts a module's runtime before the provisioner's client
// fills the credential, and the bus carries a new account only after it reloads its users. Past it the runtime gives up,
@@ -5,6 +5,8 @@ package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
@@ -75,6 +77,8 @@ func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *te
t.Setenv("MESH_BROKER_FILE", file)
t.Setenv("MESH_TOOL_MODULES", "ledger="+mt.Fixture("whole-bundle.py"))
t.Setenv("MESH_CONSOLE_LISTEN", "127.0.0.1:0")
heldFile := filepath.Join(dir, "held")
t.Setenv("HELD_FILE", heldFile)
open := func(module, node string) *bus.Conn {
c, err := bus.Connect(bus.Credential{URL: mt.URL(t), Module: module, Node: node, User: node + "." + module, Password: "unused"})
@@ -104,20 +108,32 @@ func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *te
}
holder.Flush()
// The journal, read for the drain's line at the stop (design 47 H2 §813 (12)).
journal := &said{}
log.SetOutput(journal)
t.Cleanup(func() { log.SetOutput(os.Stderr) })
savedBound := drainBound
drainBound = 2 * time.Second
t.Cleanup(func() { drainBound = savedBound })
ctx, stop := context.WithCancel(context.Background())
ended := make(chan error, 1)
go func() { ended <- serve(ctx) }()
t.Cleanup(func() {
stop()
select {
case err := <-ended:
if err != nil {
t.Errorf("serve ended with %v", err)
var stopped sync.Once
stopServe := func() {
stopped.Do(func() {
stop()
select {
case err := <-ended:
if err != nil {
t.Errorf("serve ended with %v", err)
}
case <-time.After(30 * time.Second):
t.Error("the tool runner did not stop within 30s")
}
case <-time.After(30 * time.Second):
t.Error("the tool runner did not stop within 30s")
}
})
})
}
t.Cleanup(stopServe)
// a module's account, which the approval gate runs unmarked while the catalog half is unread (the stand-in serves no catalog half)
asker := open("shop", "workstation")
ask := func(key string) (string, error) {
@@ -161,4 +177,57 @@ func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *te
if listings != 2 || len(fresh) != 1 || fresh[0]["call"] == nil || fresh[0]["call"] == "" {
t.Errorf("the list was read %d times (want 2) and the holder of node-desk.fresh was asked %v (want once, with a call token)", listings, fresh)
}
mu.Unlock()
// at its stop the runner drains (design 47 H2 §813 (12), H-A-353; the bound 2 s here for the setting's 30 s): a call of 1 s in
// flight is answered, one of 6 s is cut at the bound, said, and its caller answered nothing; a call after the stop is refused
type answer struct {
got string
err error
}
hold := func(seconds int, within time.Duration) chan answer {
out := make(chan answer, 1)
go func() {
answered, err := asker.AskWithin("ledger.hold", map[string]any{"seconds": seconds}, "", within)
out <- answer{string(answered.Result), err}
}()
return out
}
short, long := hold(1, 10*time.Second), hold(6, 5*time.Second)
// both calls are in flight once the bundle noted them
mt.Until(t, func() error {
if noted, _ := os.ReadFile(heldFile); strings.Count(string(noted), "hold") != 2 {
return fmt.Errorf("the bundle noted %q, want both calls", noted)
}
return nil
})
stop()
mt.Until(t, func() error {
_, err := ask("ledger.who")
if err == nil || !strings.Contains(err.Error(), "takes no new call") {
return fmt.Errorf("a call after the stop was answered %v, want it refused", err)
}
return nil
})
if got := <-short; got.err != nil || !strings.Contains(got.got, `"held":1`) {
t.Errorf("the call of 1 s in flight at the stop was answered %s, %v; want it finished", got.got, got.err)
}
if got := <-long; got.err == nil || strings.Contains(got.err.Error(), "cut") {
t.Errorf("the call of 6 s was answered %s, %v; want no answer, a transport fault for its caller", got.got, got.err)
}
stopServe()
if !journal.has("cut ledger's call hold") {
t.Errorf("the journal does not say the cut call: %s", journal.String())
}
mu.Lock()
}
// said is the runner's journal in a test, safe to read while it writes.
type said struct {
mu sync.Mutex
b strings.Builder
}
func (s *said) Write(p []byte) (int, error) { s.mu.Lock(); defer s.mu.Unlock(); return s.b.Write(p) }
func (s *said) String() string { s.mu.Lock(); defer s.mu.Unlock(); return s.b.String() }
func (s *said) has(words string) bool { return strings.Contains(s.String(), words) }