Run managed daemon processes at background priority on macOS (#54401)

## What changed

- Launch managed processes through `taskpolicy -b` on macOS when a five-second probe succeeds, preserving the managed process PID. Fall back to direct launch if the probe fails or times out.
- Increase the macOS startup timeout from 10 to 30 seconds to accommodate background CPU and I/O scheduling, and serialize daemon startup test fixtures on macOS.
- Use the shared background command helper and retain `CREATE_NO_WINDOW` alongside Windows detachment flags.

## Testing

Add macOS tests for PID preservation, background priority when available, and direct-launch fallback after a probe timeout.

GitOrigin-RevId: ab3328e2e8f62218ac9602e4172563c00056875b
This commit is contained in:
Benjamin Carlsson
2026-10-15 20:00:15 +00:00
committed by copyberry
parent aabf13307a
commit 97ebf2d94d
7 changed files with 177 additions and 16 deletions
@@ -95,7 +95,6 @@ impl PidBackend {
} else {
None
};
let mut command = Command::new(&codex_bin);
let stderr_log = match self.open_stderr_log().await {
Ok(stderr_log) => stderr_log,
Err(err) => {
@@ -105,17 +104,10 @@ impl PidBackend {
return Err(err);
}
};
command
// Handoff suppression belongs to the foreground CLI, not its long-lived children.
.env_remove(crate::telemetry::HANDOFF_ENV)
.args(self.command_args().iter().map(std::borrow::Cow::as_ref))
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::from(stderr_log.into_std().await));
// Older or pinned managed binaries may predate this optional startup flag.
let managed_app_server =
matches!(self.command_kind, super::PidCommandKind::AppServer { .. });
if managed_app_server
let managed_daemon_supported = managed_app_server
&& matches!(
tokio::time::timeout(
std::time::Duration::from_secs(5),
@@ -128,10 +120,8 @@ impl PidBackend {
.status(),
).await,
Ok(Ok(status)) if status.success()
)
{
command.arg("--managed-daemon");
} else if managed_app_server {
);
if managed_app_server && !managed_daemon_supported {
let codex_home = self
.pid_file
.parent()
@@ -146,6 +136,31 @@ impl PidBackend {
}
}
}
let command = crate::background_command::managed_process(&codex_bin).await;
let mut command = match command {
Ok(command) => Command::from(command),
Err(err) => {
if replacement.is_none() {
let _ = fs::remove_file(&self.pid_file).await;
}
return Err(err).with_context(|| {
format!(
"failed to spawn detached app-server process using {}",
self.codex_bin.display()
)
});
}
};
command
// Handoff suppression belongs to the foreground CLI, not its long-lived children.
.env_remove(crate::telemetry::HANDOFF_ENV)
.args(self.command_args().iter().map(std::borrow::Cow::as_ref))
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::from(stderr_log.into_std().await));
if managed_daemon_supported {
command.arg("--managed-daemon");
}
if let Some((key, value)) = self.command_env() {
command.env(key, value);
}
@@ -172,6 +187,7 @@ impl PidBackend {
#[cfg(windows)]
{
use windows_sys::Win32::System::Threading::CREATE_BREAKAWAY_FROM_JOB;
use windows_sys::Win32::System::Threading::CREATE_NO_WINDOW;
use windows_sys::Win32::System::Threading::DETACHED_PROCESS;
// A Windows process pins its working directory for its lifetime.
// Keep both managed children out of the launching project's directory.
@@ -179,7 +195,7 @@ impl PidBackend {
command.current_dir(&workdir);
// Never retry inside the parent's Job Object: that would report a
// successful launch that dies when the terminal/SSH session closes.
command.creation_flags(DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB);
command.creation_flags(CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB);
if matches!(self.command_kind, PidCommandKind::UpdateLoop { .. }) {
match fs::remove_file(self.pid_file.with_extension("ready")).await {
Ok(()) => {}