#2213

fix(cli): wait for the daemon to exit after close

OpenGourav224wants to merge intofrom

Fixes #2134

Problem

After close, the daemon sends its reply, sleeps 100ms, and only then breaks its accept loop and removes .sock and .pid. The CLI returned as soon as the reply arrived. A command for the same session sent in that window connected to the dying daemon, which exited before replying, so it failed with:

✗ Invalid response: EOF while parsing a value at line 1 column 0 (the command reached the daemon but no response came back; ...)

Read-only commands such as tab list are retried, which hides the problem. Mutating ones are not.

Fix

After a response that means the daemon is exiting, send_command waits (up to 5s) until the session socket stops answering. This uses the same wait_for_daemon_exit that request_graceful_daemon_shutdown already uses before a restart. Because the wait is in send_command, it covers close, batches, close --all, and the MCP close tool, which goes through the CLI.

To decide when to wait, the client reuses the daemon's own close_completed_response. That function moves from native/daemon.rs to connection.rs, next to INTERNAL_DAEMON_SHUTDOWN_ACTION, so the daemon and the client share one definition of a completed close.

Trade-off: close now returns about 0.25s later, once the daemon has actually stopped.

Testing

  • New unit test send_command_waits_for_the_daemon_to_exit_after_close: a fake daemon answers close, keeps accepting connections for 300ms, then removes its socket. The test fails on main ("close returned while the daemon still accepted connections") and passes with the fix.
  • Ran the issue's loop against headless Chrome 156 on --cdp 9222, using eval 1 as the follow-up command because it isn't retried:
    • main: 2/40 and 3/100 runs failed with the EOF error above
    • this branch: 0/40 and 0/100
  • cargo fmt --check passes, and cargo test passes (1481 unit tests plus the integration test binaries). cargo clippy --all-targets adds no new warnings; its 3 existing warnings are in untouched files.

🤖 Generated with Claude Code

Activity

3
vercel[bot]6d ago

@Gourav224 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.