Repository navigation
Fix: Name the process holding a pinned port, and stop looping silently - #1366
Conversation
A second Cortex install on one machine cannot start: the loopback ports are pinned (47600-47604). All that was ever said about it was net.Listen's own line, which names the address and nothing about who holds it — and the supervisor then doubled its backoff to 30s and repeated that line there forever, about 2,880 restarts a day with no cause among them (#1122). The companion symptom looks unrelated: requests reach whichever install won the port and are bridged with ITS CA, so the agent reports a self-signed certificate. The child classifies the failure, because it alone knows the failing address: reserveListeners now returns a *listenError carrying the socket's name and address, and reportListenFailure names the holder from the kernel's own tables via core/peerproc — no lsof, no ss, no root — then exits 3. Every branch keeps net.Listen's message verbatim in a cause field, the wording operators have learned to search for. The Cortex-specific remedy is gated on the holder's basename, so a sidecar that cannot bind :8080 in a pod is not sent looking for a laptop install. The supervisor classifies that exit status and dampens: the condition is explained once at ERROR, the ceiling moves from 30s to 10 minutes after the third consecutive bind failure (announced once), and the per-restart line drops to DEBUG while it holds. It never gives up — nothing restarts it until the next login, and the holder may stop at any moment — and a healthy run resets the backoff and says so. Any other exit comes straight back under the ordinary ceiling, so a crash loop following a port clash is not retried once an hour. `agentop service status` says it too, between "NOT answering" and the log tail: the proxy's log already explains it on every attempt, but that is five lines of supervisor output and the one line that explains them should not have to be found in it. install.sh's check covers the install-time case (#1104); this is the runtime one, which no install-time check can reach. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
mrsabath
left a comment
There was a problem hiding this comment.
Diagnostics-only change: the child classifies an EADDRINUSE bind failure, names the holder from kernel tables via core/peerproc, and exits 3; the supervisor reads that status to explain once and dampen the retry ceiling to 10 minutes; service status surfaces the same line.
The exit-status-as-contract split (rather than parsing the child's stderr) is the right call, and cause keeping net.Listen's wording verbatim on every branch preserves what operators search for.
What I verified
Checked against origin/main via git show — the local working tree was ahead 3081, behind 237, so nothing was read from it.
| Claim | Result |
|---|---|
peerproc API (New, ListenerOwner, Proc.PID/PPID/Exe, ErrUnsupported) |
matches core/peerproc/peerproc.go exactly |
"peerproc ranks exact bind ahead of wildcard, incl. the macOS IPV6_V6ONLY caveat" |
documented verbatim in the ListenerOwner interface comment |
Double peerproc.New() retry has precedent |
same pattern at cmd/cortex/main.go:973-977 |
log.Fatalf "nothing on disk open yet" |
pre-existing comment at the bind site says the same; bind precedes pipeline Start and the ledger/archive open |
slog.Error reaches the log |
bootstrap.InitLogging at main.go:338, bind at ~573 |
| Helpers reused, not duplicated | portHolder, ourProxy, holderJob, launchdBootout, portOfAddr all exist in setup_probe.go / setup_step_service.go |
Dual-path parity. The new foreignHolderLines against the existing install-time preflight at setup_step_service.go:118-134: both call portHolder(port), both gate on ourProxy(binDir, proxy.pid, pid, exe), both use the identical holderJob/darwin/default remedy switch and the same "%s is held by %s (pid %d), not this Cortex" sentence. Same source, same guard, same wording — no divergence between the two times this condition is met.
Backoff equivalence. The refactor moves advance to before the wait and logs the pre-doubling n.delay, where the old code logged delay, waited, then doubled. Both yield 2s, 4s, 8s…, so "which is how the loop read before this was extracted from it" holds.
Timer hygiene. healthy.Stop() is called on both wait: exit paths and healthy.C fires at most once, so the added inner loop cannot spin or leak a timer.
Two things I probed as likely bugs and found correct by design
portHoldermatches only loopback and wildcard binds, so aforwardAddron a LAN address finds no holder. That limitation is inherited and documented on the existing helper, and the new caller returnsnilrather than guessing — consistent with its own stated "empty when the holder cannot be named" contract.holderFieldsomittingppid <= 1andppid == pidis deliberate and covered byTestHolderFields_OmitsWhatIsNotKnown.
Notes
Areas reviewed: Go (cmd/cortex, cmd/agentop), tests, commit/PR conventions, security.
Commits: 1, signed off. Subject 70 chars, imperative, fix: prefix matching repo convention.
CI: all 29 checks passing (Spellcheck skipped).
Security: no secrets, no new dependencies, and no shell interpolation of untrusted input — portOf returns a <port> placeholder rather than an unparsable string, and holder paths only ever reach log fields and prose.
On the open question: agreed that the ports should stay pinned. HTTPS_PROXY=http://localhost:47600 is a fixed contract, and the bind-before-disk ordering in listeners.go depends on a taken port being the guard that stops two proxies writing one ~/.cortex. Diagnostics are the right fix here.
No inline comments — I looked for findings in every area and could not substantiate one.
Summary
A second Cortex install on one machine cannot start: the loopback ports are pinned
(47600–47604). All that was ever said about it was
net.Listen's own line —— which names the address and nothing about who holds it. The supervisor then doubled
its backoff to 30s and repeated
proxy exited; restarting … restart_in=30sthereforever: about 2,880 restarts a day, with no cause among them. The companion symptom
looks unrelated — requests reach whichever install won the port and are bridged with
its CA, so the agent reports a self-signed certificate rather than a connection
failure.
install.shalready refuses to install over a foreign proxy on the forward port(#1104). This is the runtime case, which no install-time check can reach: the other
install may be started, or stopped, long after this one was set up.
What changed
The child classifies the failure, because it alone knows the failing address —
net.Listenputs it only in its message.reserveListenersnow returns a*listenErrorcarrying the socket's name and address, and the newcmd/cortex/portconflict.gonames the holder from the kernel's own tables viacore/peerproc(nolsof, noss, no root), then exits3:Every branch keeps
net.Listen's message verbatim incause— the wording operatorshave learned to search for. Unknown values are omitted rather than rendered as real
ones, and the Cortex-specific remedy is gated on the holder's basename, so a sidecar
that cannot bind
:8080in a pod is not sent looking for a laptop install.The supervisor classifies that exit status and dampens. The condition is explained
once at ERROR; after the third consecutive bind failure the ceiling moves from 30s to
10 minutes (announced once); the per-restart line drops to DEBUG while that holds. It
never gives up — nothing restarts it until the next login, and the holder may stop
at any moment — and a healthy run resets the backoff and says so. Any other exit comes
straight back under the ordinary ceiling, so a crash loop that happens to follow a port
clash is not retried once an hour.
The division of labour is deliberate: the supervisor could have parsed the child's
stderr, but that turns a log line into a protocol. The exit status is the whole
contract.
agentop service statussays it too, between "NOT answering" and the log tail —the proxy's log explains it on every attempt, but that is five lines of supervisor
output and the one line that explains them should not have to be found in it:
Open question from the issue
The issue asks whether the ports should stop being pinned. Recommendation: no.
HTTPS_PROXY=http://localhost:47600is a fixed contract the docs,agentop configureand every configured agent depend on — and the bind-before-disk ordering in
listeners.goexists precisely because a taken port is the guard that stops twoproxies writing one
~/.cortex. Diagnostics are the right fix. Happy to open aseparate issue if multi-install support is wanted on its own merits.
Testing
New:
cmd/cortex/portconflict_test.go(6 tests),cmd/cortex/supervise_test.go(6 tests driving the whole policy through a run of failures rather than an hour of wall
clock), and two tests plus a stub helper in
cmd/agentop/cmd_service_test.go. Theagentop tests stub
lsof, redirectprocRootand stublaunchctl, so the holder andthe remedy are pinned on both platforms and no assertion keys off
runtime.GOOS.Both touched modules, run per-module with
GOWORK=off:cmd/cortex—gofmt -l .clean,go vet ./...ok,go test ./... -count=1ok (5.2s)cmd/agentop—gofmt -l .clean,go vet ./...ok,go test ./... -count=1ok (272s) and all ten subpackages greenFixes #1122
Assisted-By: Claude (Anthropic AI) [email protected]
Summary by CodeRabbit