(ws_url: &str)
| 52 | } |
| 53 | |
| 54 | pub async fn run_daemon(ws_url: &str) -> Result<()> { |
| 55 | // Write PID |
| 56 | std::fs::write(pid_path(), std::process::id().to_string())?; |
| 57 | |
| 58 | #[cfg(unix)] |
| 59 | let listener = { |
| 60 | // Clean up stale socket |
| 61 | let sock = socket_path(); |
| 62 | let _ = std::fs::remove_file(&sock); |
| 63 | |
| 64 | // Bind socket FIRST so the CLI knows the daemon is alive and can connect. |
| 65 | // If we wait for CdpClient::connect first, a macOS network permission prompt |
| 66 | // can block the daemon and cause the CLI's 5-second wait_for_daemon timeout to expire. |
| 67 | UnixListener::bind(&sock)? |
| 68 | }; |
| 69 | |
| 70 | #[cfg(windows)] |
| 71 | let listener = { |
| 72 | // Clean up stale address file |
| 73 | let _ = std::fs::remove_file(addr_path()); |
| 74 | |
| 75 | // Bind listener FIRST so the CLI knows the daemon is alive and can connect. |
| 76 | // If we wait for CdpClient::connect first, a Chrome/network permission prompt |
| 77 | // can block the daemon and cause the CLI's 5-second wait_for_daemon timeout to expire. |
| 78 | let listener = TcpListener::bind("127.0.0.1:0").await?; |
| 79 | std::fs::write(addr_path(), listener.local_addr()?.to_string())?; |
| 80 | listener |
| 81 | }; |
| 82 | |
| 83 | // We don't connect immediately. We wait for the first connection from the CLI. |
| 84 | // This ensures the CLI wait_for_daemon() succeeds, and the CLI blocks on read_msg() |
| 85 | // while the daemon handles the potentially slow macOS/Chrome network permission prompt. |
| 86 | let mut client: Option<CdpClient> = None; |
| 87 | |
| 88 | // Signal readiness by socket/address existence (it's already bound) |
| 89 | run_accept_loop_body!(listener.accept(), &mut client, ws_url); |
| 90 | |
| 91 | #[cfg(unix)] |
| 92 | let _ = std::fs::remove_file(socket_path()); |
| 93 | |
| 94 | #[cfg(windows)] |
| 95 | let _ = std::fs::remove_file(addr_path()); |
| 96 | |
| 97 | let _ = std::fs::remove_file(pid_path()); |
| 98 | |
| 99 | // Shut down telemetry before exiting so the background thread |
| 100 | // flushes pending entries and exits cleanly. |
| 101 | telemetry::shutdown_logger(); |
| 102 | |
| 103 | Ok(()) |
| 104 | } |
| 105 | |
| 106 | async fn handle_connection<S>( |
| 107 | mut stream: S, |
no test coverage detected