Replace Windows app-server shutdown files with socket requests (#43308)

## What changed

Route managed Windows app-server shutdown through `/daemon/shutdown` on the local control socket. Require the server's PID and acknowledge it before triggering shutdown through the existing drain logic. Enable the endpoint only for managed Windows launches via `CODEX_DAEMON_SHUTDOWN_SOCKET`.

Watch running-turn changes whenever shutdown is requested so socket-triggered shutdown can finish draining. Keep the updater's file-based shutdown mechanism and retain forced termination as the manager's timeout fallback.

## Testing

Add a transport test covering rejection of unmanaged shutdown requests and mismatched PIDs, plus acknowledgment before emitting the shutdown event.

GitOrigin-RevId: 3e95dc1914f61e8a131f9b6fd56b6c63688d1744
This commit is contained in:
Eric Traut
2026-09-07 00:00:23 +00:00
committed by copyberry
parent a51da75131
commit 455318c202
16 changed files with 246 additions and 57 deletions
+23 -5
View File
@@ -138,6 +138,8 @@ impl PidBackend {
}
let pid = record.pid;
let started_at = tokio::time::Instant::now();
let deadline = started_at + STOP_TIMEOUT;
#[cfg(unix)]
self.terminate_process(pid)?;
#[cfg(windows)]
@@ -148,13 +150,29 @@ impl PidBackend {
if process.start_time()? != record.process_start_time {
continue;
}
fs::write(self.pid_file.with_extension("shutdown"), pid.to_string())
.await
.context("failed to request daemon shutdown")?;
match self.command_kind {
PidCommandKind::AppServer { .. } => {
let codex_home = self
.pid_file
.parent()
.and_then(Path::parent)
.context("daemon pid path has no Codex home")?;
let socket_path =
codex_app_server_transport::app_server_control_socket_path(codex_home)?;
if let Err(err) =
crate::client::request_shutdown(socket_path.as_path(), pid).await
{
tracing::warn!(%pid, %err, "managed app-server shutdown request failed; waiting for force deadline");
}
}
PidCommandKind::UpdateLoop => {
fs::write(self.pid_file.with_extension("shutdown"), pid.to_string())
.await
.context("failed to request updater shutdown")?;
}
}
process
};
let started_at = tokio::time::Instant::now();
let deadline = tokio::time::Instant::now() + STOP_TIMEOUT;
let mut forced = false;
loop {
#[cfg(unix)]
@@ -174,16 +174,25 @@ impl PidBackend {
Err(err) => return Err(err).context("failed to clear updater readiness"),
}
}
let shutdown_file = self.pid_file.with_extension("shutdown");
match fs::remove_file(&shutdown_file).await {
Ok(()) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => return Err(err).context("failed to clear daemon shutdown request"),
match self.command_kind {
PidCommandKind::AppServer { .. } => {
command.env(codex_app_server_transport::DAEMON_SHUTDOWN_SOCKET_ENV, "1");
}
PidCommandKind::UpdateLoop => {
let shutdown_file = self.pid_file.with_extension("shutdown");
match fs::remove_file(&shutdown_file).await {
Ok(()) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => {
return Err(err).context("failed to clear updater shutdown request");
}
}
command.env(
codex_app_server_transport::DAEMON_SHUTDOWN_FILE_ENV,
shutdown_file,
);
}
}
command.env(
codex_app_server_transport::DAEMON_SHUTDOWN_FILE_ENV,
shutdown_file,
);
}
let child = match command.spawn() {