Files
codex/codex-rs/rmcp-client/src/local_child.rs
T
Charlie Marsh ea7e0714da Fix relative MCP server spawning on macOS (#42117)
## Why

Rust falls back to `fork` when a macOS command combines a relative executable
path with a working directory, due to a historical `posix_spawnp` issue. This
can make local MCP server startup less reliable.

## What changed

- Spawn relative macOS MCP executables directly with `posix_spawn`, preserving
  the configured path, `argv[0]`, working directory, environment, stdio, and
  process group.
- Keep the existing launcher for `PATH` lookup and executable text files without
  a shebang.
- Use one local child wrapper for legacy and 2026-07-28 protocol framing, with
  cancellation-safe waiting and cleanup that kills and reaps dropped children.

## Testing

Add macOS regression coverage for relative scripts, argument and descriptor
preservation, launch errors, cancelled waits, and cleanup after runtime shutdown.

GitOrigin-RevId: 1cbc6b90aed6f67e0a38f551964d80496ebc4471
2026-09-01 18:53:06 +00:00

35 lines
899 B
Rust

//! Uniform child-process API for local MCP servers, with platform-specific spawning.
//!
//! Commands must use the launcher's cleared environment, process group, and default
//! argv[0]. Both implementations expose Tokio stdio handles and kill on drop.
use std::io;
use std::process::Stdio;
use tokio::process::Command;
#[cfg(target_os = "macos")]
#[path = "macos_stdio.rs"]
mod macos;
#[cfg(target_os = "macos")]
pub(super) use macos::LocalChild;
#[cfg(not(target_os = "macos"))]
pub(super) use tokio::process::Child as LocalChild;
pub(super) fn spawn(mut command: Command) -> io::Result<LocalChild> {
command
.kill_on_drop(true)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(target_os = "macos")]
{
LocalChild::spawn(command)
}
#[cfg(not(target_os = "macos"))]
{
command.spawn()
}
}