emrg: catch AuthError in server stop/restart CLI paths (stale port file → graceful fallback, no crash) - #877
Conversation
…le → graceful fallback, no crash)
argszero
left a comment
There was a problem hiding this comment.
✅ LGTM — cycle
Fix verified: catches AuthError (raised by connect_to_server when the daemon closes the connection during the auth handshake — the stale emrgd.port scenario) in both _send_shutdown() (falls back to SIGTERM path instead of crashing) and _stop_daemon()'s ping fallback (clean 'daemon not running' message). CI: test + test-windows both PASS. The sibling emrg stop path (_stop_all.py) already handles auth failure gracefully (returns False, no crash), so this closes the remaining gap in the CLI stop/restart paths. No server stop/restart tests added — such tests are banned by design.
argszero
left a comment
There was a problem hiding this comment.
✅ LGTM — cycle (2/3)
Re-verified this cycle: head dcb8326 unchanged, CI test + test-windows both PASS, mergeStateStatus CLEAN. Diff reviewed again — 3-line change (AuthError import + added to both except clauses in _send_shutdown() and _stop_daemon() ping fallback) exactly closes the crash path when a stale emrgd.port points at a dead/old daemon. Graceful degradation verified: _send_shutdown returns False → SIGTERM fallback; _stop_daemon prints 'daemon not running' instead of a traceback; emrg server restart proceeds to spawn the new daemon.
argszero
left a comment
There was a problem hiding this comment.
✅ LGTM — cycle (3/3)
Third independent cycle review: head dcb8326 unchanged, CI test + test-windows both PASS, mergeStateStatus CLEAN. Change is minimal and correct — AuthError added to the exception lists of both _send_shutdown() (→ SIGTERM fallback) and _stop_daemon()'s ping fallback (→ clean 'daemon not running'). This closes the emrg server stop/restart crash when a stale emrgd.port points at an old/dead daemon. Merge condition met (3 consecutive ✅, no ❌).
Uh oh!
There was an error while loading. Please reload this page.
Fix daemon stopped (graceful shutdown). / restarting daemon ...
daemon not running.
daemon started (pid=30439). crashing with an unhandled
AuthErrortraceback when the daemon closes the connection during the auth handshake.Root cause (diagnosed 2026-08-19 in a host debugging session)
A stale
~/.emrg/emrgd.port(written by an old dynamic-port daemon residue, e.g. pid 3574 on port 50317) makes the CLI read the wrong port+token, connect to the fixed port 56031 (current daemon), and send an auth frame the current daemon rejects → daemon closes with 1000 →connect_to_server()raisesAuthError→_send_shutdown()/_stop_daemon()didn't catch it → traceback crash instead of graceful fallback, and_restart_daemon()never reached.Fix
AuthErrorfromemrg.connect_send_shutdown(): addAuthErrorto the connect catch list → returnsFalse→ falls through to the SIGTERM fallback (no crash)_stop_daemon()ping fallback: addAuthErrorto the catch list → printsdaemon not running.Now a stale port file degrades gracefully: graceful shutdown → SIGTERM fallback → clean message →
restartstill starts a fresh daemon.Verification
uv run pytest tests/ -v: 986 passed, 1 skippedpython -c "import emrg.__main__"/py_compile: OKemrg --help: OK