Preserve live remote PTYs when relay lease disappears
Summary
- separate passive daemon retirement from explicit authenticated persistent-slot shutdown
- route both automatic exit reasons through one activity snapshot and require zero stdio connections plus zero live PTY sessions
- preserve detached PTYs across relay lease loss until they exit or are explicitly closed
- write timestamped automatic-exit reasons and activity counts to
daemon.log - update the remote-daemon lifecycle documentation to state the ownership boundary
Root cause
The relay .slot file and activeConnections describe transport ownership, but the lease-gone branch used those transport facts as authority to return from the daemon and run hub.closeAll(). That bypassed the live-session guard already used by the empty-idle branch and terminated persistent PTYs during a reconnect gap.
Authenticated daemon.shutdown remains intentionally destructive for final workspace cleanup. Passive lease observation now only makes an otherwise empty daemon eligible to retire.
Test-first proof
The branch preserves red-before-green history:
2dd592e8eechanges only tests. On the old production code,TestPersistentDaemonPreservesActivePTYAfterObservedSlotLeaseDisappearsfailed withpersistent daemon retired while its detached PTY was still active; the empty-idle test also exposed the missing lifecycle log.06e27e445bimplements the shared automatic-retirement gate and lifecycle diagnostics. The regression reconnects to the same detached PTY after lease removal, then proves the daemon retires only afterpty.closeleaves the hub empty.
Verification
- focused lifecycle tests: 2/2 passed
- focused race lifecycle suite, including explicit destructive shutdown: 3/3 passed
go test ./... -count=1 -timeout 10mgo vet ./...- strict test-determinism gate: 0 findings
- Linux/arm64 daemon binary cross-build: valid aarch64 ELF
- Linux/arm64 daemon test binary cross-compile: valid aarch64 ELF
gofmtandgit diff --check
go test -race ./... was attempted twice. Each run hit a different pre-existing short-deadline integration timeout under race load; both the affected shutdown sibling and every lifecycle test in this PR pass together under focused race execution.
Scope
No Swift files or warning/file-length budget files change. Explicit workspace shutdown behavior is unchanged.
Closes #9706
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
[!NOTE]
Medium Risk
Changes persistent SSH PTY daemon lifecycle and retirement timing on the remote host; behavior is well-tested but affects reconnect gaps and workspace teardown boundaries.
Overview
Fixes premature persistent daemon shutdown when the relay .slot lease disappears while a detached PTY is still running.
Passive retirement now uses one gate: automatic exit only when stdio connections and live PTY sessions (including sessions mid-teardown) are both zero. Lease loss alone no longer tears down live detached sessions; they survive until exit, explicit pty.close, or the empty idle timeout. Authenticated daemon.shutdown for workspace cleanup is unchanged.
The PTY hub counts sessions through teardown completion (sessionTeardownCount / WaitGroup) so retirement cannot race idle reaping. Automatic exits log RFC3339Nano timestamps, reason (slot_lease_removed or empty_idle_timeout), and activity counts to daemon.log. Docs describe passive lease policy and lifecycle diagnostics.
Reviewed by Cursor Bugbot for commit e539849606be7ec70382ae75b2bcb480c581eee2. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Preserves live remote PTYs when a relay slot lease disappears and retires the daemon only when it’s truly empty (no stdio and no live or tearing‑down PTY sessions). Adds timestamped exit logs with reason and activity counts, and tightens idle/lease checks and docs on passive lease policy. Addresses #9706.
-
Bug Fixes
- Detached PTYs survive
~/.cmux/relay/<port>.slotlease loss; retirement requires zero stdio connections and zero PTY sessions, including sessions mid‑teardown. - Daemon waits for PTY teardown to finish;
closeAllblocks until processes and PTY files close, and the idle reaper counts a session as active until teardown completes. Authenticateddaemon.shutdownstays destructive.
- Detached PTYs survive
-
Refactors
- Single automatic‑exit gate for lease loss and empty idle timeout; logs RFC3339Nano exit time, reason (
slot_lease_removedorempty_idle_timeout), and activity counts todaemon.log. - Defers session scans while connections are active; README/spec clarify passive retirement and that detached PTYs outlive lease loss until they exit or are closed.
- Single automatic‑exit gate for lease loss and empty idle timeout; logs RFC3339Nano exit time, reason (
Summary by CodeRabbit
-
Bug Fixes
- Persistent remote daemons now preserve active detached PTY sessions when a relay-slot lease disappears.
- Daemons shut down only after connections and PTY sessions are fully inactive, or after an empty idle timeout.
- Clean workspace teardown now safely retires eligible disconnected daemons.
-
Documentation
- Improved guidance on daemon lifecycle, shutdown behavior, and crash diagnostics.
-
Diagnostics
- Shutdown logs now clearly identify the exit reason and report active connection and session counts.