carlos / platform Public

activator: KillHost opens the host cgroup through EmptyHost's seam; binary goldens pin a populated KillHost and EmptyHost (Plan 3 Task 1 carry, before N1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5077a37a37f664df4d70235db1d14252311f3d3e parent 79e5426
6 files changed, +108 −27
  • internal/activator/activator_test.go +1 −1
  • internal/activator/backend_exec.go +4 −4
  • internal/activator/binary_golden_test.go +61 −4
  • internal/activator/genbackend.go +28 −12
  • internal/activator/rootfs_invariants_test.go +7 −6
  • internal/activator/testdata/binary_golden/kill_host.txt +7 −0
diff --git a/internal/activator/activator_test.go b/internal/activator/activator_test.go
index 209fb93a3..9cd88dbfd 100644
--- a/internal/activator/activator_test.go
+++ b/internal/activator/activator_test.go
@@ -7060,7 +7060,7 @@ func TestEmptyHostDispatch(t *testing.T) {
func TestEmptyHostWithNoCgroupKillIsTheLeaderOnlyProof(t *testing.T) {
eb := testExecBackend(t)
opened := 0
- eb.openHostCleanup = func(string) (*cgroup.Cleanup, error) {
+ eb.openHostCleanup = func(string) (hostCleanup, error) {
opened++
return nil, fmt.Errorf("%w: open cgroup.kill: no such file or directory", cgroup.ErrNoKill)
}
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go
index de121e97f..50b8edef3 100644
--- a/internal/activator/backend_exec.go
+++ b/internal/activator/backend_exec.go
@@ -294,10 +294,10 @@ type ExecBackend struct {
// cgroup manager's own. Tests replace this seam, never the Cgroups
// field.
cgroupKill func(ctx context.Context, rt registry.Route, child string) error
- // openHostCleanup is EmptyHost's open of the host cgroup. Nil:
- // Cgroups.OpenCleanup. Tests replace this seam, never the Cgroups
- // field.
- openHostCleanup func(host string) (*cgroup.Cleanup, error)
+ // openHostCleanup is KillHost's and EmptyHost's open of the host
+ // cgroup (openHostCgroup). Nil: Cgroups.OpenCleanup. Tests replace this
+ // seam, never the Cgroups field.
+ openHostCleanup func(host string) (hostCleanup, error)
// strictPreflight is NextSpawnStrict's seam for PrepareStrict's runtime
// preflight. Nil: Cgroups.StrictPreflight, the very function
// PrepareStrict runs, so S2 and the spawn cannot disagree.
diff --git a/internal/activator/binary_golden_test.go b/internal/activator/binary_golden_test.go
index 393a323b4..87e876a0a 100644
--- a/internal/activator/binary_golden_test.go
+++ b/internal/activator/binary_golden_test.go
@@ -315,9 +315,11 @@ func TestGenHealthyBoundBinary(t *testing.T) {
// reaches: no delegation; a delegated root that is not cgroup v2 (the
// proof refused, never nil); and through EmptyHost's open seam an absent
// cgroup (nil), a kernel without cgroup.kill (the leader-only proof) and
-// an open that fails. Killing a populated cgroup needs a real delegated
-// cgroup v2: TestClaimContainmentCleanupRespawn's KillHost leg proves it
-// under CARLOS_CLAIM_SPAWN_KERNEL=1.
+// an open that fails; then KillHost through the same seam, and both verbs
+// on a populated and an empty host cgroup the seam hands back. Killing a
+// real populated cgroup needs a delegated cgroup v2:
+// TestClaimContainmentCleanupRespawn's KillHost leg proves it under
+// CARLOS_CLAIM_SPAWN_KERNEL=1.
func TestKillHostBinaryGolden(t *testing.T) {
var out strings.Builder
rt := registry.Route{Host: "blog.example.com", Kind: "instance"}
@@ -364,16 +366,71 @@ func TestKillHostBinaryGolden(t *testing.T) {
} {
b := testExecBackend(t)
var opened []string
- b.openHostCleanup = func(host string) (*cgroup.Cleanup, error) {
+ b.openHostCleanup = func(host string) (hostCleanup, error) {
opened = append(opened, host)
return nil, c.open
}
err := b.EmptyHost(ctx, rt)
fmt.Fprintf(&out, "EmptyHost, %s: %s (opened %v)\n", c.name, answer(err), opened)
}
+ // Added before N1 changed KillHost (Task 1's carry): KillHost through
+ // the same open seam, and both verbs on a host cgroup that is
+ // populated (killed, then closed) or empty (KillHost kills it anyway;
+ // EmptyHost only reads it).
+ for _, c := range []struct {
+ name string
+ open error
+ }{
+ {"absent cgroup", cgroup.ErrNoCgroup},
+ {"no cgroup.kill", fmt.Errorf("%w: open cgroup.kill: no such file or directory", cgroup.ErrNoKill)},
+ {"open fails", errors.New("permission denied")},
+ } {
+ b := testExecBackend(t)
+ var opened []string
+ b.openHostCleanup = func(host string) (hostCleanup, error) {
+ opened = append(opened, host)
+ return nil, c.open
+ }
+ err := b.KillHost(ctx, rt)
+ fmt.Fprintf(&out, "KillHost, %s: %s (opened %v)\n", c.name, answer(err), opened)
+ }
+ for _, populated := range []bool{true, false} {
+ for _, verb := range []string{"KillHost", "EmptyHost"} {
+ b := testExecBackend(t)
+ hc := &goldenHostCgroup{populated: populated}
+ var opened []string
+ b.openHostCleanup = func(host string) (hostCleanup, error) {
+ opened = append(opened, host)
+ return hc, nil
+ }
+ var err error
+ if verb == "KillHost" {
+ err = b.KillHost(ctx, rt)
+ } else {
+ err = b.EmptyHost(ctx, rt)
+ }
+ fmt.Fprintf(&out, "%s, host cgroup populated=%v: %s (opened %v, killed %d, closed %d, populated after %v)\n",
+ verb, populated, answer(err), opened, hc.killed, hc.closed, hc.populated)
+ }
+ }
checkBinaryGolden(t, "kill_host.txt", []byte(out.String()))
}
+// goldenHostCgroup is a host cgroup the open seam hands back: populated
+// or not, emptied by its kill.
+type goldenHostCgroup struct {
+ populated bool
+ killed, closed int
+}
+
+func (c *goldenHostCgroup) Populated() (bool, error) { return c.populated, nil }
+func (c *goldenHostCgroup) KillAndEmpty(context.Context) error {
+ c.killed++
+ c.populated = false
+ return nil
+}
+func (c *goldenHostCgroup) Close() error { c.closed++; return nil }
+
// HasSidecar (backend_exec.go) on a binary row, flat and nested: a planted
// <host>-sidecar.env in the row's data directory answers true, none false.
func TestHasSidecarBinaryGolden(t *testing.T) {
diff --git a/internal/activator/genbackend.go b/internal/activator/genbackend.go
index 23a639d17..6c13e7888 100644
--- a/internal/activator/genbackend.go
+++ b/internal/activator/genbackend.go
@@ -568,6 +568,32 @@ func (b *SplitBackend) GenPaths(rt registry.Route, n uint64) (Gen, bool) {
return Gen{}, false
}
+// hostCleanup is what KillHost and EmptyHost do with the host cgroup they
+// open: *cgroup.Cleanup's population read, kill and close.
+type hostCleanup interface {
+ Populated() (bool, error)
+ KillAndEmpty(ctx context.Context) error
+ Close() error
+}
+
+// openHostCgroup is KillHost's and EmptyHost's one open of host's own
+// cgroup (carlos-exec@<host>): the openHostCleanup seam when a test sets
+// it, else the cgroup manager's OpenCleanup. No manager at all is
+// cgroup.ErrNoDelegation.
+func (b *ExecBackend) openHostCgroup(host string) (hostCleanup, error) {
+ if b.openHostCleanup != nil {
+ return b.openHostCleanup(host)
+ }
+ if b.Cgroups == nil {
+ return nil, cgroup.ErrNoDelegation
+ }
+ c, err := b.Cgroups.OpenCleanup(host)
+ if err != nil {
+ return nil, err
+ }
+ return c, nil
+}
+
// KillHost kills whatever is left in rt's own host cgroup and returns once
// it is empty: a worker's termination proof (zero-downtime spec §7.4, §7.9).
// It does not look at the row's layout: StartWorker spawns every worker
@@ -581,10 +607,7 @@ func (b *SplitBackend) GenPaths(rt registry.Route, n uint64) (Gen, bool) {
// internal/cgroup/recreate_linux.go). That
// holds with the flag off too, where only Task 15's F6 path reaches here.
func (b *ExecBackend) KillHost(ctx context.Context, rt registry.Route) error {
- if b.Cgroups == nil {
- return cgroup.ErrNoDelegation
- }
- c, err := b.Cgroups.OpenCleanup(rt.Host)
+ c, err := b.openHostCgroup(rt.Host)
if errors.Is(err, cgroup.ErrNoCgroup) {
return nil
}
@@ -621,14 +644,7 @@ func (b *SplitBackend) KillHost(ctx context.Context, rt registry.Route) error {
// a descendant there, and reading it as an unproven kill would hold every
// flag-off stop's lease on such a box.
func (b *ExecBackend) EmptyHost(ctx context.Context, rt registry.Route) error {
- open := b.openHostCleanup
- if open == nil {
- if b.Cgroups == nil {
- return cgroup.ErrNoDelegation
- }
- open = b.Cgroups.OpenCleanup
- }
- c, err := open(rt.Host)
+ c, err := b.openHostCgroup(rt.Host)
if errors.Is(err, cgroup.ErrNoCgroup) {
return nil
}
diff --git a/internal/activator/rootfs_invariants_test.go b/internal/activator/rootfs_invariants_test.go
index a2eba103e..066930701 100644
--- a/internal/activator/rootfs_invariants_test.go
+++ b/internal/activator/rootfs_invariants_test.go
@@ -67,12 +67,13 @@ var (
// rootfsRefusedObjects: this package's childEnv (the agent's
// environment), the binary host cgroup's interfaces and the binary
// unit-name builders (unitNameFor is the runtime-aware one).
- rootfsRefusedObjects = []string{"childEnv", "hostKiller", "hostEmptier", "unitName", "runUnitName"}
+ rootfsRefusedObjects = []string{"childEnv", "hostKiller", "hostEmptier", "unitName", "runUnitName", "hostCleanup"}
// rootfsRefusedMethods: every method of these names, whatever its
- // receiver.
- rootfsRefusedMethods = []string{"KillHost", "EmptyHost"}
+ // receiver. openHostCgroup is KillHost's and EmptyHost's open of the
+ // host cgroup.
+ rootfsRefusedMethods = []string{"KillHost", "EmptyHost", "openHostCgroup"}
// rootfsRefusedFields: type -> field. ExecBackend's openHostCleanup is
- // EmptyHost's open of the host cgroup.
+ // KillHost's and EmptyHost's open seam.
rootfsRefusedFields = map[string]string{"ExecBackend": "openHostCleanup"}
// rootfsRefusedForeign: import path -> a package-level object or a
// Type.Method. The host cgroup's opens (what a Cgroups field's
@@ -945,8 +946,8 @@ var (
rootfsInvariantFloor = map[string][]string{
"rootfsBinaryVerbs": {"Control.Cmd", "Control.CmdStatus", "Control.Reset", "Control.Seed", "Control.Start", "Control.Stop",
"GenControl.StartGen", "GenControl.StartGenExe", "GenControl.StartSidecarExe", "GenControl.StopGen", "cmdExeStarter.CmdExe"},
- "rootfsRefusedObjects": {"childEnv", "hostEmptier", "hostKiller", "runUnitName", "unitName"},
- "rootfsRefusedMethods": {"EmptyHost", "KillHost"},
+ "rootfsRefusedObjects": {"childEnv", "hostCleanup", "hostEmptier", "hostKiller", "runUnitName", "unitName"},
+ "rootfsRefusedMethods": {"EmptyHost", "KillHost", "openHostCgroup"},
"rootfsRefusedFields": {"ExecBackend.openHostCleanup"},
"rootfsRefusedForeign": {"github.com/carlosframework/platform/internal/cgroup.Manager.OpenCleanup",
"github.com/carlosframework/platform/internal/cgroup.Manager.OpenCleanupChild",
diff --git a/internal/activator/testdata/binary_golden/kill_host.txt b/internal/activator/testdata/binary_golden/kill_host.txt
index c3e16bd06..861918320 100644
--- a/internal/activator/testdata/binary_golden/kill_host.txt
+++ b/internal/activator/testdata/binary_golden/kill_host.txt
@@ -7,3 +7,10 @@ EmptyHost, delegated fixture root, host cgroup present=true: ErrStrictUnsupporte
EmptyHost, absent cgroup: nil (opened [blog.example.com])
EmptyHost, no cgroup.kill: ErrNoDelegation (opened [blog.example.com])
EmptyHost, open fails: error (opened [blog.example.com])
+KillHost, absent cgroup: nil (opened [blog.example.com])
+KillHost, no cgroup.kill: ErrNoKill (opened [blog.example.com])
+KillHost, open fails: error (opened [blog.example.com])
+KillHost, host cgroup populated=true: nil (opened [blog.example.com], killed 1, closed 1, populated after false)
+EmptyHost, host cgroup populated=true: nil (opened [blog.example.com], killed 1, closed 1, populated after false)
+KillHost, host cgroup populated=false: nil (opened [blog.example.com], killed 1, closed 1, populated after false)
+EmptyHost, host cgroup populated=false: nil (opened [blog.example.com], killed 0, closed 1, populated after false)