carlos / platform Public

activator: the tty handoff socket lasts until the unit is settled (rootfs plan 6 task 8 fix round 1)

Both tty templates run ExecStopPost=instance-exit with the same
StandardInput=file: socket, and systemd opens it again for the post
command. Unlinking it right after the handoff failed the post command
with EXIT_STDIN before instance-exit ran, losing the exit record and
turning a normal stop into a service failure.

The listener now stays until Close (StopAndSettle, after the stop and
the cgroup proof). Exactly one fd transfer happens, to the first root
peer; every later root peer is accepted and closed with no byte and no
fd, and a peer that is not root is refused as before.

Census overlay rootfs-apps-6: the activator envelope re-pinned, seal
unchanged (b36c59a0).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev b9e0eddd5f346ec14aae21785f5a4ab4cfbfb503 parent 49c6abb
3 files changed, +92 −28
  • internal/activator/pty_linux.go +21 −11
  • internal/activator/pty_linux_test.go +70 −16
  • internal/provision/testdata/plan14-root-reads-rootfs-apps-6.tsv +1 −1

Intent

Human

<task-notification> <task-id>bct0fnr1l</task-id> <tool-use-id>toolu_018qJZT4bHn9WwLcGzx8TGXq</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ba9fa944-a429-4788-9b3c-f2b2f42617f3/tasks/bct0fnr1l.output</output-file> <status>completed</status> <summary>Background command "Run Astra review of Plan 6 Task 8" completed (exit code 0)</summary> </task-notification>

Attributed to this commit: 8.50 g CO₂e (1,025,639 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on rootfs-apps-6-interactive, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/activator/pty_linux.go b/internal/activator/pty_linux.go
index 1a87bcf95..a5b4784ba 100644
--- a/internal/activator/pty_linux.go
+++ b/internal/activator/pty_linux.go
@@ -133,8 +133,8 @@ type ttyHandoff struct {
}
// listenTTY binds path owner-only (edge.SecureUnixListen) and hands slave
-// to the first root peer. It takes slave: Close closes it, and so does the
-// handoff once it is sent.
+// to the first root peer, once. It takes slave: Close closes it, and so
+// does the handoff once it is sent. The listener stays until Close.
func listenTTY(path string, slave *os.File) (*ttyHandoff, error) {
ln, err := edge.SecureUnixListen(path)
if err != nil {
@@ -150,13 +150,19 @@ func listenTTY(path string, slave *os.File) (*ttyHandoff, error) {
return h, nil
}
-// accept takes connections until a root peer arrives. Any other peer is
-// closed without a byte or an fd. The root peer gets one sendmsg of
-// TTYHello with the slave as SCM_RIGHTS; then the agent closes its slave
-// and stops listening, whether or not the send succeeded: the unit's
-// stdin connects once, so there is no second root peer to wait for.
+// accept takes connections until the handoff is closed. Any peer that is
+// not root is closed without a byte or an fd. The first root peer gets
+// one sendmsg of TTYHello with the slave as SCM_RIGHTS; then the agent
+// closes its slave, whether or not the send succeeded. That is the one
+// transfer: every later root peer is accepted and closed at once, with no
+// byte and no fd. The endpoint stays until Close (the unit's settle,
+// after its stop and cgroup proof), because the tty unit's ExecStopPost=
+// runs with the same StandardInput= and opens it again: with the socket
+// gone, systemd fails the post command (EXIT_STDIN) before instance-exit
+// runs (fix round 1).
func (h *ttyHandoff) accept() {
defer close(h.looped)
+ spent := false // the one transfer was attempted
for {
c, err := h.ln.AcceptUnix()
if err != nil {
@@ -167,13 +173,16 @@ func (h *ttyHandoff) accept() {
c.Close()
continue
}
+ if spent {
+ c.Close()
+ continue
+ }
+ spent = true
if err := h.send(c); err != nil {
log.Printf("activator: tty handoff %s: %v", h.path, err)
}
c.Close()
h.closeSlave()
- h.ln.Close()
- return
}
}
@@ -213,8 +222,9 @@ func (h *ttyHandoff) closeSlave() {
// Handed reports whether the slave went to a root peer.
func (h *ttyHandoff) Handed() bool { return h.handed.Load() }
-// Close stops listening, waits for the accept loop, and closes the slave
-// if it was never handed over. Safe to call more than once.
+// Close stops listening and unlinks the socket, waits for the accept
+// loop, and closes the slave if it was never handed over. Safe to call
+// more than once. Only the unit's settle (or a failed start) calls it.
func (h *ttyHandoff) Close() error {
err := h.ln.Close()
<-h.looped
diff --git a/internal/activator/pty_linux_test.go b/internal/activator/pty_linux_test.go
index d2e8b8056..0e0bfd7af 100644
--- a/internal/activator/pty_linux_test.go
+++ b/internal/activator/pty_linux_test.go
@@ -70,6 +70,10 @@ func shortSocketDir(t *testing.T) string {
return dir
}
+// productionTTYPeerOK is the production peer check, kept so a test that
+// swapped it can put it back mid-test.
+var productionTTYPeerOK = ttyPeerOK
+
// setTTYPeerOK swaps the handoff's peer check for one test.
func setTTYPeerOK(t *testing.T, ok func(*net.UnixConn) error) {
t.Helper()
@@ -220,16 +224,33 @@ func TestTTYHandoffGivesTheFdOnlyToRoot(t *testing.T) {
t.Errorf("the fd handed over is no terminal: %v", err)
}
waitFor(t, "Handed", h.Handed)
- // The agent keeps only the master: its slave is closed, and the
- // handoff listens no more.
+ // The agent keeps only the master: its slave is closed.
waitFor(t, "the agent's slave to close", func() bool { _, err := slave.Stat(); return errors.Is(err, os.ErrClosed) })
- waitFor(t, "the handoff to stop listening", func() bool {
- c, err := net.DialTimeout("unix", path, time.Second)
- if err == nil {
- c.Close()
- }
- return err != nil
- })
+
+ // The endpoint stays until the unit is settled (fix round 1): the tty
+ // unit's ExecStopPost= opens the same StandardInput= again, so a later
+ // root peer connects, and gets nothing: exactly one fd ever goes out.
+ payload, fds, err = receiveTTY(t, path)
+ if payload != "" || len(fds) != 0 || !errors.Is(err, io.EOF) {
+ t.Fatalf("a second root peer got %q and %d fds (%v), want EOF and nothing", payload, len(fds), err)
+ }
+ // A non-root peer after the handoff is refused as before.
+ ttyPeerMu.Lock()
+ ttyPeerOK = productionTTYPeerOK
+ ttyPeerMu.Unlock()
+ payload, fds, err = receiveTTY(t, path)
+ if payload != "" || len(fds) != 0 || !errors.Is(err, io.EOF) {
+ t.Fatalf("a non-root peer after the handoff got %q and %d fds (%v), want EOF and nothing", payload, len(fds), err)
+ }
+ if fi, err := os.Lstat(path); err != nil || fi.Mode().Type() != os.ModeSocket {
+ t.Fatalf("the handoff socket after the handoff: %v, want it kept until the settle", err)
+ }
+ if err := h.Close(); err != nil {
+ t.Fatalf("Close: %v", err)
+ }
+ if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
+ t.Errorf("the handoff socket after Close: %v, want it gone", err)
+ }
}
// The handoff closes its listener and its slave when it is closed before
@@ -807,8 +828,8 @@ func TestSessionRefusesWithoutEnvImageOrReplicaKey(t *testing.T) {
// A root peer that has gone before the agent sends never had the
// terminal: Handed stays false (the box reports the session as a failed
-// start), and the handoff closes its slave and stops listening, since a
-// unit's stdin connects once.
+// start), and the handoff closes its slave: the one transfer is spent,
+// and the endpoint stays for the unit's ExecStopPost=.
func TestTTYHandoffToAPeerThatLeftIsNotHanded(t *testing.T) {
master, slave, err := openPTY(80, 24)
if err != nil {
@@ -833,12 +854,45 @@ func TestTTYHandoffToAPeerThatLeftIsNotHanded(t *testing.T) {
c.Close()
close(gone)
waitFor(t, "the handoff to close its slave", func() bool { _, err := slave.Stat(); return errors.Is(err, os.ErrClosed) })
- select {
- case <-h.looped:
- case <-time.After(5 * time.Second):
- t.Fatal("the handoff still listens after its root peer")
- }
if h.Handed() {
t.Error("Handed for a peer that had gone before the send")
}
+ // The one transfer was spent on that peer: a later root peer gets
+ // nothing, and the endpoint stays for the unit's ExecStopPost=.
+ payload, fds, err := receiveTTY(t, path)
+ if payload != "" || len(fds) != 0 || !errors.Is(err, io.EOF) {
+ t.Fatalf("a later root peer got %q and %d fds (%v), want EOF and nothing", payload, len(fds), err)
+ }
+ if h.Handed() {
+ t.Error("Handed after a later peer")
+ }
+ if _, err := os.Lstat(path); err != nil {
+ t.Errorf("the handoff socket: %v, want it kept until Close", err)
+ }
+}
+
+// Through a session's unit: the handoff socket outlives the handoff, so
+// the tty unit's ExecStopPost= can open its stdin again, and goes only at
+// StopAndSettle, after the stop and the cgroup proof.
+func TestTTYHandoffSocketLastsUntilTheSettle(t *testing.T) {
+ skipAsRoot(t, "the test's own connection would be root's")
+ b, rt, _, _, u := startBinaryTTY(t)
+ sock := b.logSocketNamed(rt.Host, TTYSocketName)
+ setTTYPeerOK(t, peerIsMe)
+ if _, fds, err := receiveTTY(t, sock); err != nil || len(fds) != 1 {
+ t.Fatalf("handoff: %d fds, %v", len(fds), err)
+ }
+ // ExecStopPost='s stdin: it connects, and gets nothing.
+ if payload, fds, err := receiveTTY(t, sock); payload != "" || len(fds) != 0 || !errors.Is(err, io.EOF) {
+ t.Fatalf("the post hook's connect got %q and %d fds (%v), want EOF and nothing", payload, len(fds), err)
+ }
+ if _, err := os.Lstat(sock); err != nil {
+ t.Fatalf("the handoff socket before the settle: %v, want it kept", err)
+ }
+ if err := u.StopAndSettle(context.Background()); err != nil {
+ t.Fatalf("StopAndSettle: %v", err)
+ }
+ if _, err := os.Lstat(sock); !errors.Is(err, os.ErrNotExist) {
+ t.Errorf("the handoff socket after the settle: %v, want it gone", err)
+ }
}
diff --git a/internal/provision/testdata/plan14-root-reads-rootfs-apps-6.tsv b/internal/provision/testdata/plan14-root-reads-rootfs-apps-6.tsv
index 2d36f5737..9d868bd43 100644
--- a/internal/provision/testdata/plan14-root-reads-rootfs-apps-6.tsv
+++ b/internal/provision/testdata/plan14-root-reads-rootfs-apps-6.tsv
@@ -88,7 +88,7 @@ internal/activator/rootfs_service.go Service.rootfsTick legacy accepted-legacy a
@remove internal/instancectl/cmd.go Server.handleCmdStatus
@removed internal/instancectl/cmd.go Server.handleCmdStatus H3 census-ruling-rootfs-apps-6;absent;basis=rootfs Plan 6 Task 6: moved whole to Server.oneShotStatus (role-aware; see its claim), not deleted: no check, refusal or validation is lost
@coverage . fbc28f10f89cd03cac31bae0d798ae7f707eb7671284a6115abd8c1c214369b6
-@coverage internal/activator 501fb480acb9e534b58ad88512d12d693b7c2856ba3ab9f911fce402b04b6622
+@coverage internal/activator a101487c05860ca19b284f2ca0b014d69d8dc0f9217cf93108166634a81ae0a3
@coverage internal/appconfig cac8d8ef7538a456d034801c95ce6508a4983e5516b3bbbd302bec0fbafdd479
@coverage internal/host 69c09eff5b3b87756182f001086b391c02b867e82e8d21ea40722c992fec43a7
@coverage internal/host/units c3cc4f3b917fdb938f20d406b1704ec54e24553851c298e312056d397d10b0e1