Skip to content

emrg: catch AuthError in server stop/restart CLI paths (stale port file → graceful fallback, no crash) - #877

Merged
argszero merged 1 commit into
masterfrom
feature/cli-auth-error-fallback
Aug 19, 2026
Merged

emrg: catch AuthError in server stop/restart CLI paths (stale port file → graceful fallback, no crash)#877
argszero merged 1 commit into
masterfrom
feature/cli-auth-error-fallback

Conversation

@argszero

Copy link
Copy Markdown
Owner

Fix daemon stopped (graceful shutdown). / restarting daemon ...
daemon not running.
daemon started (pid=30439). crashing with an unhandled AuthError traceback 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() raises AuthError_send_shutdown() / _stop_daemon() didn't catch it → traceback crash instead of graceful fallback, and _restart_daemon() never reached.

Fix

  • Import AuthError from emrg.connect
  • _send_shutdown(): add AuthError to the connect catch list → returns False → falls through to the SIGTERM fallback (no crash)
  • _stop_daemon() ping fallback: add AuthError to the catch list → prints daemon not running.

Now a stale port file degrades gracefully: graceful shutdown → SIGTERM fallback → clean message → restart still starts a fresh daemon.

Verification

  • uv run pytest tests/ -v: 986 passed, 1 skipped
  • python -c "import emrg.__main__" / py_compile: OK
  • Client import + emrg --help: OK

@argszeroargszero left a comment

Copy link
Copy Markdown
OwnerAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@argszeroargszero left a comment

Copy link
Copy Markdown
OwnerAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@argszeroargszero left a comment

Copy link
Copy Markdown
OwnerAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 ❌).

@argszero
argszero merged commit 94c99d0 into masterAug 19, 2026
2 checks passed
@argszero
argszero deleted the feature/cli-auth-error-fallback branch August 19, 2026 17:35
argszero added a commit that referenced this pull request Aug 20, 2026
Co-authored-by: EMRG Evolution <emrg@argszero.dev>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@argszero