Skip to main content

spawn_sidecar

Function spawn_sidecar 

Source
pub async fn spawn_sidecar(
    index: SharedIndex,
    bind_host: &str,
    repo_root: Option<PathBuf>,
    control_state_dir: Option<ControlStateDir>,
) -> Result<SidecarHandle>
Expand description

Spawn the HTTP sidecar.

  1. Reads SYMFORGE_SIDECAR_BIND env var (default "127.0.0.1").
  2. Calls port_file::check_stale(bind_host) to clean up any stale files.
  3. Creates a socket2::Socket, sets SO_REUSEADDR, binds to {bind_host}:0 (OS assigns the port), then listens with backlog 1024. SO_REUSEADDR lets the bind succeed when the chosen ephemeral port is still in TIME_WAIT from a recently-closed listener; this keeps rapid daemon restarts and parallel test fan-out from racing on Windows.
  4. Hands the socket to tokio::net::TcpListener::from_std.
  5. Writes port and PID files via port_file.
  6. Creates SidecarState with TokenStats and empty symbol cache.
  7. Builds the axum router via router::build_router.
  8. Spawns axum::serve with graceful shutdown wired to a oneshot channel.
  9. After the server completes, calls port_file::cleanup_files().
  10. Returns SidecarHandle { port, shutdown_tx, server_join, token_stats }.