carlos / platform Public

host: the 3-minute exit sends SIGTERM to the agent itself

os.Exit(1) skipped the agent's normal shutdown, so on a fixed HA box the
3-minute exit was an unplanned stop: no clean marker, lease not released.
It now signals its own process, so the usual shutdown runs, and keeps
os.Exit(1) as the backstop: at once if the signal cannot be sent, and
agentShutdownBudget later if the process is still alive. The shutdown
lock from fix round 1 still orders the decision against shutdownStarted
(W6 final review F6).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev a53529c4340deb155b95095f365f19b8f5cd7bee parent 8555d1d
2 files changed, +82 −28
  • internal/host/ingressdrain.go +35 −9
  • internal/host/ingressdrain_test.go +47 −19
diff --git a/internal/host/ingressdrain.go b/internal/host/ingressdrain.go
index e85970bf6..3e6f37c01 100644
--- a/internal/host/ingressdrain.go
+++ b/internal/host/ingressdrain.go
@@ -23,6 +23,7 @@ import (
"slices"
"strings"
"sync"
+ "syscall"
"time"
"github.com/carlosframework/platform/internal/edge"
@@ -254,8 +255,10 @@ type ingressDrain struct {
health ingressHealth
reader ingressReaderFunc
clock ingressClock
- // exit ends the process; os.Exit in production.
- exit func(code int)
+ // terminate sends SIGTERM to the agent's own process (terminateSelf);
+ // exit ends it at once (os.Exit), the backstop.
+ terminate func() error
+ exit func(code int)
mu sync.Mutex
edge ingressEdge
@@ -270,7 +273,17 @@ type ingressDrain struct {
}
func newIngressDrain(arm func() ingressArming, health ingressHealth) *ingressDrain {
- return &ingressDrain{arm: arm, health: health, reader: newIngressReader, clock: realIngressClock, exit: os.Exit}
+ return &ingressDrain{arm: arm, health: health, reader: newIngressReader, clock: realIngressClock, terminate: terminateSelf, exit: os.Exit}
+}
+
+// terminateSelf sends SIGTERM to this process, so the agent's own
+// shutdown runs as at a systemd stop.
+func terminateSelf() error {
+ p, err := os.FindProcess(os.Getpid())
+ if err != nil {
+ return err
+ }
+ return p.Signal(syscall.SIGTERM)
}
// setEdge hands the driver the running edge. A drain asked for before is
@@ -412,12 +425,17 @@ func (d *ingressDrain) run(e ingressEdge, arm ingressArming, rep *ingressReport,
}
// exitWithoutStop is the 3-minute exit (spec §4.2): the drain closed the
-// listeners and no stop followed, so the agent exits and systemd restarts
-// it through admission and two passing health checks.
+// listeners and no stop followed, so the agent ends itself and systemd
+// restarts it through admission and two passing health checks. It sends
+// its own process SIGTERM, so the normal shutdown runs: on a fixed HA box
+// that writes the clean marker and releases the lease, which os.Exit
+// would skip. os.Exit(1) stays as the backstop, at once if the signal
+// cannot be sent, and agentShutdownBudget later if the process is still
+// alive; the shutdown the signal starts does not cancel it.
//
-// The decision and the exit hold the lock shutdownStarted takes: once the
-// shutdown has begun the exit never runs, and once the exit has been
-// decided the shutdown waits for it (os.Exit does not return).
+// The decision holds the lock shutdownStarted takes: once the shutdown has
+// begun the exit never runs, and once the exit has been decided the
+// shutdown waits until the signal is sent.
func (d *ingressDrain) exitWithoutStop() {
d.mu.Lock()
defer d.mu.Unlock()
@@ -425,7 +443,15 @@ func (d *ingressDrain) exitWithoutStop() {
return
}
log.Printf("agent: ingress drain ended without a stop; exiting")
- d.exit(1)
+ if err := d.terminate(); err != nil {
+ log.Printf("agent: ingress drain: SIGTERM to the agent itself: %v; exiting at once", err)
+ d.exit(1)
+ return
+ }
+ d.clock.after(agentShutdownBudget, func() {
+ log.Printf("agent: ingress drain: the agent is still running %s after its own SIGTERM; exiting", agentShutdownBudget)
+ d.exit(1)
+ })
}
// wait is step 2: poll the target groups until every one reports the box
diff --git a/internal/host/ingressdrain_test.go b/internal/host/ingressdrain_test.go
index 87f3fc993..860904179 100644
--- a/internal/host/ingressdrain_test.go
+++ b/internal/host/ingressdrain_test.go
@@ -219,6 +219,8 @@ type ingressFixture struct {
edge *fakeIngressEdge
health *fakeIngressHealth
exits *[]int
+ // terms counts the SIGTERMs the drain sent its own process.
+ terms *int
}
func newIngressFixture(t *testing.T, script func(at time.Duration) (ingresshealth.Verdict, error)) *ingressFixture {
@@ -241,8 +243,10 @@ func newIngressFixture(t *testing.T, script func(at time.Duration) (ingresshealt
}
exits := &[]int{}
d.exit = func(code int) { *exits = append(*exits, code) }
+ terms := new(int)
+ d.terminate = func() error { *terms++; return nil }
d.setEdge(e)
- return &ingressFixture{d: d, clock: c, tg: tg, edge: e, health: h, exits: exits}
+ return &ingressFixture{d: d, clock: c, tg: tg, edge: e, health: h, exits: exits, terms: terms}
}
// drain runs one drain to its end and returns the report.
@@ -616,8 +620,11 @@ func TestIngressDrainJoins(t *testing.T) {
}
}
-// Three minutes after step 3 the agent exits non-zero, unless its own
-// shutdown began first.
+// Three minutes after step 3 the agent shuts itself down, unless its own
+// shutdown began first: SIGTERM to its own process, so the normal shutdown
+// runs (clean marker, lease release), and os.Exit(1) only as a backstop if
+// the process is still alive agentShutdownBudget later (W6 final review
+// F6).
func TestIngressDrainExitsThreeMinutesAfterStepThree(t *testing.T) {
f := newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
f.drain(t)
@@ -625,16 +632,36 @@ func TestIngressDrainExitsThreeMinutesAfterStepThree(t *testing.T) {
if len(as) != 1 || as[0].d != 3*time.Minute {
t.Fatalf("scheduled %+v", as)
}
- if len(*f.exits) != 0 {
- t.Fatal("exited at step 3")
+ if len(*f.exits) != 0 || *f.terms != 0 {
+ t.Fatal("ended at step 3")
}
as[0].f()
+ if *f.terms != 1 || len(*f.exits) != 0 {
+ t.Fatalf("at 3 minutes: %d SIGTERM(s), exits %v; want one SIGTERM and no exit", *f.terms, *f.exits)
+ }
+ // The SIGTERM starts the agent's shutdown, which must not disarm the
+ // backstop: it is there for a shutdown that hangs.
+ f.d.shutdownStarted()
+ as = f.clock.afters()
+ if len(as) != 2 || as[1].d != agentShutdownBudget || *as[1].stopped {
+ t.Fatalf("backstop %+v", as)
+ }
+ as[1].f()
+ if !slices.Equal(*f.exits, []int{1}) {
+ t.Fatalf("backstop exits %v", *f.exits)
+ }
+
+ // A SIGTERM that cannot be sent: the exit at once.
+ f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
+ f.d.terminate = func() error { return errors.New("no signal") }
+ f.drain(t)
+ f.clock.afters()[0].f()
if !slices.Equal(*f.exits, []int{1}) {
t.Fatalf("exits %v", *f.exits)
}
- // The stop came: shutdown cancels the exit, and a timer that fires
- // anyway (the race) does not exit either.
+ // The stop came: shutdown cancels the timer, and a timer that fires
+ // anyway (the race) sends nothing either.
f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
f.drain(t)
f.d.shutdownStarted()
@@ -643,8 +670,8 @@ func TestIngressDrainExitsThreeMinutesAfterStepThree(t *testing.T) {
t.Fatalf("the exit was not cancelled: %+v", as)
}
as[0].f()
- if len(*f.exits) != 0 {
- t.Fatalf("exited during shutdown: %v", *f.exits)
+ if len(*f.exits) != 0 || *f.terms != 0 || len(f.clock.afters()) != 1 {
+ t.Fatalf("ended during shutdown: exits %v, SIGTERMs %d", *f.exits, *f.terms)
}
// Shutdown before step 3 ends: no exit is scheduled at all.
@@ -960,15 +987,16 @@ func TestIngressDrainBoundsHungCalls(t *testing.T) {
}
// The exit and the shutdown are one decision: a shutdown that starts while
-// the exit is running waits for it (os.Exit never returns), and an exit
-// timer that fires while the shutdown is starting does nothing after it.
+// the 3-minute exit is deciding waits for it, and a timer that fires while
+// the shutdown is starting does nothing after it.
func TestIngressDrainExitAndShutdownInterleave(t *testing.T) {
f := newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
entered, release := make(chan struct{}), make(chan struct{})
- f.d.exit = func(code int) {
+ f.d.terminate = func() error {
close(entered)
<-release
- *f.exits = append(*f.exits, code)
+ *f.terms++
+ return nil
}
f.drain(t)
timer := f.clock.afters()[0].f
@@ -978,17 +1006,17 @@ func TestIngressDrainExitAndShutdownInterleave(t *testing.T) {
go func() { f.d.shutdownStarted(); close(shut) }()
select {
case <-shut:
- t.Fatal("the shutdown began while the exit was running")
+ t.Fatal("the shutdown began while the exit was deciding")
case <-time.After(50 * time.Millisecond):
}
close(release)
<-shut
- if !slices.Equal(*f.exits, []int{1}) {
- t.Fatalf("exits %v", *f.exits)
+ if *f.terms != 1 || len(f.clock.afters()) != 2 {
+ t.Fatalf("SIGTERMs %d, scheduled %+v", *f.terms, f.clock.afters())
}
// The shutdown holds the decision: a timer fired while shutdownStarted
- // is inside its critical section exits nothing.
+ // is inside its critical section sends nothing after it.
f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
f.drain(t)
timer = f.clock.afters()[0].f
@@ -1003,8 +1031,8 @@ func TestIngressDrainExitAndShutdownInterleave(t *testing.T) {
f.d.stopping = true
f.d.mu.Unlock()
<-fired
- if len(*f.exits) != 0 {
- t.Fatalf("exited during the shutdown: %v", *f.exits)
+ if len(*f.exits) != 0 || *f.terms != 0 {
+ t.Fatalf("ended during the shutdown: exits %v, SIGTERMs %d", *f.exits, *f.terms)
}
}