carlos / platform Public

region HA: drain --cancel lifts the fence only on a confirmed stop

Task 5 fix round 2 (Fable P2-3 remainder). When the drain socket does
not answer, the edge probe is read three ways: MainPID 0 is a confirmed
stop and the fence goes; a running edge keeps it (errDrainSocketDown);
anything else (a timed-out or failed systemctl, a malformed MainPID, a
failed /proc identity read) is unknown and keeps it too
(errDrainEdgeAbsent). maintenanceMainPID tells MainPID 0 apart from a
malformed answer; status's error text is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev acb910ebe8010aa167b17fd065b1fc75994664f4 parent eb6f539
3 files changed, +95 −17
  • internal/host/group_maintenance_drain.go +45 −3
  • internal/host/group_maintenance_verbs_linux.go +3 −3
  • internal/host/hadrainfence_linux_test.go +47 −11

Intent

Human

<task-notification> <task-id>a40e6294574b2458e</task-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/283be278-5c6a-4a71-a8d6-b3d32c668985/tasks/a40e6294574b2458e.output</output-file> <status>completed</status> <summary>Agent "Implement Task 5: drain driver" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE. Every listed finding is fixed: the three P1s (ack map race, connection drain barrier, transaction release log) and the P2s (event-boundary SSE close, fakes, stats timing, `drain --cancel`). Commits: `b43172bc` (fix round 1 code) and `eb6f5394` (census re-propose). Tests: the covering tests pass under `-race` in host, activator and edge. The targeted host and edge regression runs pass. In the activator run, `TestHeldE2EAppEndsStreamWhileAwake` failed once on a timing check under load; it passed 20 of 20 on its own. All 11 new mutations make their tests fail. The census test passes; the #280 verdict is still owed. Contract changes for Task 6: - `released[].kind` can now be `idle`: a host released outside the drain's own stops. - `reasons` names each unacknowledged box with its reason. - `drain --cancel` exits 1 and keeps the fence when the drain socket is down but an edge process runs. - A connection drain that can't reach zero fails the drain. - After an unclean release, rerunning the same transaction fails again; cancel and start a new one. Carried items: `TestEdgesNamedPoolsGolden` and `TestGroupNameClassCensus` now pass. The darwin vet failure comes from test files this branch never touched; darwin and windows builds pass. Concerns: - **Fable P3 1, not done:** every request still takes the drain's mutex twice; the new barrier needs every in-flight request's connection. - **Fable P3 5, not done:** treating a missing fence directory as fenced would block all starts on existing boxes until they get the tmpfiles rule, so it needs its own rollout step. Report (fix round 1 section appended): `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-held-build/.superpowers/sdd/2026-11-08-region-ha-held-requests/task-5-report.md`</result> <usage><subagent_tokens>495374</subagent_tokens><tool_uses>40</tool_uses><duration_ms>2177221</duration_ms></usage> </task-notification>

diff --git a/internal/host/group_maintenance_drain.go b/internal/host/group_maintenance_drain.go
index f2cc9d0ba..f40a0652e 100644
--- a/internal/host/group_maintenance_drain.go
+++ b/internal/host/group_maintenance_drain.go
@@ -10,14 +10,54 @@ import (
"encoding/json"
"errors"
"io"
+ "strconv"
+ "strings"
"time"
)
var (
errDrainFailed = errors.New("drain failed")
errDrainSocketDown = errors.New("the edge runs but its drain socket does not answer; the fence stays")
+ errDrainEdgeAbsent = errors.New("the drain socket does not answer and the edge process cannot be confirmed stopped; the fence stays")
)
+// The edge probe's two MainPID errors. Both read as status's existing
+// "edge MainPID unavailable"; only errEdgeStopped is a confirmed stop.
+var (
+ // errEdgeStopped: systemctl answered MainPID 0, so no edge process
+ // runs. It is the only probe answer that proves no agent is there.
+ errEdgeStopped = errors.New("edge MainPID unavailable")
+ // errEdgeMainPID: systemctl's answer was not a PID.
+ errEdgeMainPID = errors.New("edge MainPID unavailable")
+)
+
+// maintenanceMainPID reads systemctl's MainPID answer: a running PID, a
+// confirmed stop (errEdgeStopped), or a malformed answer (errEdgeMainPID).
+func maintenanceMainPID(b []byte) (int, error) {
+ pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
+ switch {
+ case err != nil || pid < 0:
+ return 0, errEdgeMainPID
+ case pid == 0:
+ return 0, errEdgeStopped
+ }
+ return pid, nil
+}
+
+// edgeConfirmedStopped is the edge probe read three ways: confirmed
+// stopped (true, nil); running (false, errDrainSocketDown); or unknown, a
+// probe that failed, timed out or could not read the process's identity
+// (false, errDrainEdgeAbsent).
+func edgeConfirmedStopped(pid int, err error) (bool, error) {
+ switch {
+ case err == nil && pid > 0:
+ return false, errDrainSocketDown
+ case errors.Is(err, errEdgeStopped):
+ return true, nil
+ }
+ return false, errDrainEdgeAbsent
+}
+
// runDrain is drain <txn>: the withdraw step, the start fence, then the
// move step. It prints the agent's last report, and succeeds only when the
// report's result is ok. A fence of another transaction refuses before
@@ -76,9 +116,11 @@ func (cmd *groupMaintenanceCommand) runDrainCancel(ctx context.Context) error {
// The socket does not answer. That proves no drain state only if
// no agent runs: an edge process whose socket failed to start may
// still be draining, and lifting its fence would leave it so,
- // unfenced.
- if pid, _, perr := cmd.edge(ctx); perr == nil && pid > 0 {
- return errDrainSocketDown
+ // unfenced. Only a confirmed stop lifts it; a running edge or a
+ // probe that cannot tell keeps it.
+ pid, _, perr := cmd.edge(ctx)
+ if _, err := edgeConfirmedStopped(pid, perr); err != nil {
+ return err
}
}
return fence.remove(cmd.txn)
diff --git a/internal/host/group_maintenance_verbs_linux.go b/internal/host/group_maintenance_verbs_linux.go
index ded9e4e30..d2a2c928b 100644
--- a/internal/host/group_maintenance_verbs_linux.go
+++ b/internal/host/group_maintenance_verbs_linux.go
@@ -174,9 +174,9 @@ func maintenanceEdgeProcess(ctx context.Context) (int, string, error) {
if err := cmd.Run(); err != nil {
return 0, "", err
}
- pid, err := strconv.Atoi(strings.TrimSpace(string(out.body)))
- if err != nil || pid <= 0 {
- return 0, "", errors.New("edge MainPID unavailable")
+ pid, err := maintenanceMainPID(out.body)
+ if err != nil {
+ return 0, "", err
}
f, err := os.Open("/proc/" + strconv.Itoa(pid) + "/stat")
if err != nil {
diff --git a/internal/host/hadrainfence_linux_test.go b/internal/host/hadrainfence_linux_test.go
index ff2691cee..2ac7fdd5f 100644
--- a/internal/host/hadrainfence_linux_test.go
+++ b/internal/host/hadrainfence_linux_test.go
@@ -317,7 +317,8 @@ func TestDrainVerbFenceAndCancel(t *testing.T) {
// drain state); fence-clear needs no agent at all.
func TestDrainCancelWithoutAgent(t *testing.T) {
cmd, l := verbDrain(t, nil)
- cmd.edge = func(context.Context) (int, string, error) { return 0, "", errors.New("edge MainPID unavailable") }
+ // systemctl answered MainPID 0: the edge is confirmed stopped.
+ cmd.edge = func(context.Context) (int, string, error) { return 0, "", errEdgeStopped }
must(t, l.install("t1", time.Now()))
if _, err := runDrainVerb(t, cmd, "drain-cancel", "t1"); err != nil {
t.Fatal(err)
@@ -383,17 +384,52 @@ func TestDrainVerbArguments(t *testing.T) {
}
}
-// drain --cancel never lifts the fence of a live agent it cannot reach:
-// an edge process runs, its drain socket does not answer.
-func TestDrainCancelRefusedWhileTheEdgeRuns(t *testing.T) {
- cmd, l := verbDrain(t, nil)
- cmd.edge = func(context.Context) (int, string, error) { return 4242, "99", nil }
- must(t, l.install("t1", time.Now()))
- if _, err := runDrainVerb(t, cmd, "drain-cancel", "t1"); !errors.Is(err, errDrainSocketDown) {
- t.Fatalf("cancel against a live edge with no socket: %v", err)
+// drain --cancel never lifts the fence of an agent it cannot reach unless
+// the edge is confirmed stopped: a running edge, or a probe that cannot
+// tell (timed out, a systemctl failure, a malformed MainPID, a failed
+// process-identity read), keeps the fence and exits non-zero.
+func TestDrainCancelNeedsAConfirmedStop(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ pid int
+ err error
+ want error
+ }{
+ {"running", 4242, nil, errDrainSocketDown},
+ {"probe timed out", 0, context.DeadlineExceeded, errDrainEdgeAbsent},
+ {"systemctl failed", 0, errors.New("exit status 1"), errDrainEdgeAbsent},
+ {"malformed MainPID", 0, errEdgeMainPID, errDrainEdgeAbsent},
+ {"identity read failed", 0, &os.PathError{Op: "open", Path: "/proc/4242/stat", Err: os.ErrNotExist}, errDrainEdgeAbsent},
+ {"no PID and no error", 0, nil, errDrainEdgeAbsent},
+ {"confirmed stopped", 0, errEdgeStopped, nil},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ cmd, l := verbDrain(t, nil)
+ cmd.edge = func(context.Context) (int, string, error) { return tc.pid, "99", tc.err }
+ must(t, l.install("t1", time.Now()))
+ _, err := runDrainVerb(t, cmd, "drain-cancel", "t1")
+ if !errors.Is(err, tc.want) && !(tc.want == nil && err == nil) {
+ t.Fatalf("cancel: %v, want %v", err, tc.want)
+ }
+ if kept := l.read().Present; kept != (tc.want != nil) {
+ t.Fatalf("fence present %v after %s", kept, tc.name)
+ }
+ })
}
- if s := l.read(); s.Transaction != "t1" {
- t.Fatalf("the fence was lifted: %+v", s)
+}
+
+// systemctl's MainPID answer: only 0 is a confirmed stop.
+func TestMaintenanceMainPID(t *testing.T) {
+ for in, want := range map[string]error{"0\n": errEdgeStopped, "": errEdgeMainPID, "x": errEdgeMainPID, "-1": errEdgeMainPID} {
+ if _, err := maintenanceMainPID([]byte(in)); err != want {
+ t.Fatalf("%q: %v, want %v", in, err, want)
+ }
+ }
+ if pid, err := maintenanceMainPID([]byte("4242\n")); pid != 4242 || err != nil {
+ t.Fatalf("4242: %d %v", pid, err)
+ }
+ if errEdgeStopped.Error() != "edge MainPID unavailable" || errEdgeMainPID.Error() != errEdgeStopped.Error() {
+ t.Fatal("status's edge error text changed")
}
}