From 7adf9996c57d03211810c1ebf366c22c65af5ea6 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 23:13:34 +0100 Subject: [PATCH] E7: stop the tool runner alone and wait its drain bound MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The service manager's stop signalled the tool runner's whole group, so its bundles died with their calls. The composer carries the runner's drain-bound setting on its process; the unit stops the runner alone and waits the bound and 15 s before it kills what is left, so the new runner starts only after the old one exits or that bound passes. Builds: design 47 H2 §813 (12); H-A-353; H-A-358 --- internal/apply/process.go | 14 +++++- internal/apply/process_test.go | 10 +++++ internal/compose/resolution/runtime.go | 50 +++++++++++++++++++-- internal/compose/resolution/runtime_test.go | 26 +++++++++++ 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/internal/apply/process.go b/internal/apply/process.go index 4d434ab0..94d1a48e 100644 --- a/internal/apply/process.go +++ b/internal/apply/process.go @@ -293,6 +293,10 @@ func withNode(r *declaration.Process, node string) *declaration.Process { return &p } +// drainStopMargin is the seconds a draining process's stop timeout lies above its drain bound: the time it takes to stop +// its children after its drain (H-A-358). +const drainStopMargin = 15 + // unitFor is the unit the mesh writes for a daemon. // // **Generated whole and never edited in place**, the same rule as every other managed file: an @@ -333,7 +337,15 @@ func unitFor(r *declaration.Process) string { } // Restarted when it exits, because something that stops is not something that stays up. // Delayed, so a process that fails at once does not spin the machine. - b.WriteString("Restart=always\nRestartSec=5\n\n") + b.WriteString("Restart=always\nRestartSec=5\n") + // **A process that drains is stopped alone** (design 47 H2 §813 (12), H-A-353, H-A-358): the stop signal reaches it + // and not its children, so the calls in flight finish within its drain bound and it stops its children itself; the + // service manager waits the bound and the time they take before it kills what is left, so the new process starts only + // after the old one exits or that bound passes. + if r.DrainBound > 0 { + fmt.Fprintf(&b, "KillMode=mixed\nTimeoutStopSec=%ds\n", r.DrainBound+drainStopMargin) + } + b.WriteString("\n") b.WriteString("[Install]\nWantedBy=multi-user.target\n") return b.String() diff --git a/internal/apply/process_test.go b/internal/apply/process_test.go index 1cab4259..91ab037d 100644 --- a/internal/apply/process_test.go +++ b/internal/apply/process_test.go @@ -43,6 +43,16 @@ func TestTheUnitRunsWhatTheDaemonSaid(t *testing.T) { if strings.Contains(unit, "Type=oneshot") { t.Fatalf("a process that should stay up is declared a step:\n%s", unit) } + // Design 47 H2 §813 (12), H-A-353, H-A-358: a process that drains is stopped alone, its stop waiting its drain bound and + // 15 s for its children; one that does not keeps the service manager's defaults. + if strings.Contains(unit, "KillMode=") || strings.Contains(unit, "TimeoutStopSec=") { + t.Fatalf("a process with no drain bound has its stop written:\n%s", unit) + } + draining := aProcess() + draining.DrainBound = 30 + if got := unitFor(draining); !strings.Contains(got, "Restart=always\nRestartSec=5\nKillMode=mixed\nTimeoutStopSec=45s\n\n[Install]") { + t.Fatalf("a process with a drain bound of 30 s is not stopped alone within 45 s:\n%s", got) + } } // **Deterministic, because the unit is half the daemon's identity.** Environment held in a map diff --git a/internal/compose/resolution/runtime.go b/internal/compose/resolution/runtime.go index 675010e6..10f6722c 100644 --- a/internal/compose/resolution/runtime.go +++ b/internal/compose/resolution/runtime.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "sort" + "strconv" "strings" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" @@ -171,12 +172,17 @@ func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering, h Holding) } env[mcat.RuntimeToolEnv] = string(body) } + drain, err := drainBoundOf(*runtime, with.Settings[runtime.Module]) + if err != nil { + return nil, err + } process := map[string]any{ "id": runtime.Module + "." + RuntimeProcessID(), "type": "process", "name": runtime.Module, "source": bundle.Source, "digest": bundle.Digest, - "run": run, - "env": env, - "restart-on": toAny(restartOn), + "run": run, + "env": env, + "restart-on": toAny(restartOn), + "drain-bound": drain, } if r.Account != "" { env[mcat.RuntimeOperatorAccount] = r.Account @@ -195,6 +201,44 @@ func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering, h Holding) return process, nil } +// DrainBoundSetting is the tool runner module's setting of how long, in seconds, it finishes its calls in flight once +// stopped (design 47 H2 §813 (12)); DefaultDrainBound is its default where no layer sets it (H-A-358). +const ( + DrainBoundSetting = "drain-bound" + DefaultDrainBound = 30 +) + +// drainBoundOf is the tool runner's drain bound in seconds, from its setting drain-bound, else DefaultDrainBound (design +// 47 H2 §813 (12), H-A-358): the node-engine writes its unit's stop from it. A value that is no whole number of seconds +// above zero is refused by name. +func drainBoundOf(m mcat.Manifest, layers []mcat.Layer) (int, error) { + v, set := settingValue(WithDefaults(m, layers), DrainBoundSetting) + if !set { + return DefaultDrainBound, nil + } + var n float64 + switch x := v.(type) { + case float64: + n = x + case int: + n = float64(x) + case int64: + n = float64(x) + case string: + parsed, err := strconv.Atoi(strings.TrimSpace(x)) + if err != nil { + return 0, fmt.Errorf("%s's setting %s is %q, and it is a whole number of seconds", m.Module, DrainBoundSetting, x) + } + n = float64(parsed) + default: + return 0, fmt.Errorf("%s's setting %s is %v, and it is a whole number of seconds", m.Module, DrainBoundSetting, v) + } + if n < 1 || n != float64(int(n)) { + return 0, fmt.Errorf("%s's setting %s is %v, and it is a whole number of seconds above zero", m.Module, DrainBoundSetting, v) + } + return int(n), nil +} + // NoRuntimeToRunError is a module of its own account with no runtime program on its node to run it from. Held says the // runtime is there and held (its stand-in, Stubbed): the module cannot compose because the runtime does not, so a // composition that holds the runtime holds it too, never the node (issue 774). diff --git a/internal/compose/resolution/runtime_test.go b/internal/compose/resolution/runtime_test.go index 3fdd4216..f7ed2da6 100644 --- a/internal/compose/resolution/runtime_test.go +++ b/internal/compose/resolution/runtime_test.go @@ -168,6 +168,32 @@ func TestTheMachineRunsOneRuntimeLoadingEveryDeliveredBundle(t *testing.T) { if names[len(names)-1] != "node-tools"+"."+RuntimeProcessID() { t.Errorf("the runtime's process is not last: %v", names) } + // Design 47 H2 §813 (12), H-A-358: the runtime's process carries the module's drain bound, 30 s where no layer sets it, + // the value a layer sets, and a value that is no number of seconds is refused by name. + if process["drain-bound"] != DefaultDrainBound { + t.Errorf("the runtime's process drains for %v, not the default 30 s", process["drain-bound"]) + } + for _, row := range []struct { + value any + want int + words string + }{{float64(45), 45, ""}, {"60", 60, ""}, {"soon", 0, "whole number of seconds"}, {float64(0), 0, "above zero"}} { + set := with + set.Settings = mcat.SettingsBy{"node-tools": {{From: "mesh", Values: map[string]any{DrainBoundSetting: row.value}}}} + out, err := Declaration(r, set) + if row.words != "" { + if err == nil || !strings.Contains(err.Error(), row.words) { + t.Errorf("drain-bound %v was composed: %v", row.value, err) + } + continue + } + if err != nil { + t.Fatal(err) + } + if got := fileNamed(out, "node-tools."+RuntimeProcessID())["drain-bound"]; got != row.want { + t.Errorf("drain-bound %v composed the runtime's process with %v", row.value, got) + } + } t.Run("a machine with no account runs it as root without the operator words", func(t *testing.T) { out, err := Declaration(mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{nftables, theRuntime(t)}}, with)