fix: prevent daemon connection from hanging in connecting state - #19
Merged
Conversation
Two fixes for the issue where /new sessions show 'connecting to daemon' indefinitely: 1. Poll for socket after spawning daemon — previously spawnDaemon() was fire-and-forget but createConnection() was called immediately, racing the daemon's socket bind. Now we poll every 100ms for up to 3s until the socket file appears before attempting to connect. 2. Add 5s handshake timeout — if the version_check/version_ok handshake never completes (e.g. stale socket file, orphaned fd), the connection is treated as dead and reconnection is scheduled. Previously there was no timeout, so the state would hang at 'connecting' forever. Co-authored-by: rapid-sloth-39 <rapid-sloth-39@pi-agent.local>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the issue where new pi sessions get stuck showing
🟡 name (connecting to daemon...)indefinitely.Root cause
Two bugs in the daemon connection logic:
Race condition on daemon spawn:
connectToDaemon()spawns the daemon viaspawnDaemon()(fire-and-forget) and immediately callscreateConnection()without waiting for the daemon to bind to the Unix socket. The connection attempt races the daemon startup.No handshake timeout: The
version_check → version_okhandshake had no timeout. If the socket connection succeeds but the peer never responds (stale socket, orphaned fd), the state stays atconnectingforever with no recovery path.Fix
Poll for socket after daemon spawn: After spawning the daemon, poll
isDaemonRunning()every 100ms for up to 3 seconds until the socket file appears before attemptingcreateConnection(). This eliminates the race.5-second handshake timeout: If the handshake doesn't complete within 5 seconds, the connection is treated as dead and reconnection is scheduled with the existing exponential backoff.
Testing
node --experimental-strip-types --test test/agent-session.test.ts— 10/10)— rapid-sloth-39