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 authored and copyberry committed 2026-09-07 00:00:23 +00:00
1 parent a51da75131
commit 455318c202
16 files changed
+246 -57

No files matched your search

@@ -1,6 +1,6 @@
//! Detached Windows processes cannot receive console signals. The daemon passes
//! a request path inside its user-only state directory. Only the addressed PID
//! consumes a request, triggering the SIGTERM drain without changing the wire API.
//! The detached Windows updater has no control socket, so its owner requests
//! termination through a file in its private state directory. Managed app-server
//! shutdown instead uses the protected local socket.
use std::io;
use std::io::Read;
@@ -13,8 +13,7 @@ use std::time::Duration;
#[cfg(windows)]
pub const DAEMON_SHUTDOWN_FILE_ENV: &str = "CODEX_DAEMON_SHUTDOWN_FILE";
/// Waits for and consumes one daemon shutdown request. Without a managed launch
/// environment this stays pending, leaving normal console shutdown unchanged.
/// Waits for and consumes one updater shutdown request.
#[cfg(windows)]
pub async fn daemon_shutdown_signal() -> io::Result<()> {
let Some(path) = std::env::var_os(DAEMON_SHUTDOWN_FILE_ENV).map(PathBuf::from) else {
+3
View File
@@ -4,6 +4,8 @@ mod daemon_shutdown;
pub use daemon_shutdown::DAEMON_SHUTDOWN_FILE_ENV;
#[cfg(windows)]
pub use daemon_shutdown::daemon_shutdown_signal;
/// Only managed app-server launches accept the local socket shutdown request.
pub const DAEMON_SHUTDOWN_SOCKET_ENV: &str = "CODEX_DAEMON_SHUTDOWN_SOCKET";
mod outgoing_message;
mod transport;
@@ -17,6 +19,7 @@ pub use transport::AppServerTransport;
pub use transport::AppServerTransportParseError;
pub use transport::CHANNEL_CAPACITY;
pub use transport::ConnectionOrigin;
pub use transport::DaemonShutdownAccess;
pub use transport::REMOTE_CONTROL_DISABLED_ENV_VAR;
pub use transport::RemoteControlDisabledByRequirements;
pub use transport::RemoteControlEnableError;
@@ -43,6 +43,7 @@ pub use remote_control::start_remote_control;
pub use remote_control::take_remote_control_disabled_env;
pub use stdio::start_stdio_connection;
pub use unix_socket::AppServerStartupLock;
pub use unix_socket::DaemonShutdownAccess;
pub use unix_socket::acquire_app_server_startup_lock;
pub use unix_socket::prepare_control_socket_path;
pub use unix_socket::start_control_socket_acceptor;
@@ -170,6 +171,8 @@ impl FromStr for AppServerTransport {
#[derive(Debug)]
pub enum TransportEvent {
/// Accepted on the managed local control socket, outside JSON-RPC.
DaemonShutdown,
ConnectionOpened {
connection_id: ConnectionId,
origin: ConnectionOrigin,
@@ -423,6 +423,7 @@ fn transport_event_name(event: &TransportEvent) -> &'static str {
TransportEvent::ConnectionOpened { .. } => "connection_opened",
TransportEvent::ConnectionClosed { .. } => "connection_closed",
TransportEvent::IncomingMessage { .. } => "incoming_message",
TransportEvent::DaemonShutdown => "daemon_shutdown",
}
}
@@ -10,11 +10,15 @@ use crate::transport::websocket::run_websocket_connection;
use codex_uds::UnixListener;
use codex_uds::UnixStream;
use codex_utils_absolute_path::AbsolutePathBuf;
use futures::SinkExt;
use futures::StreamExt;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio::time::Duration;
use tokio_tungstenite::accept_async;
use tokio_tungstenite::accept_hdr_async;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::http::Response;
use tokio_tungstenite::tungstenite::http::StatusCode;
use tokio_util::sync::CancellationToken;
use tracing::error;
use tracing::info;
@@ -23,10 +27,17 @@ use tracing::warn;
#[cfg(unix)]
const CONTROL_SOCKET_MODE: u32 = 0o600;
#[derive(Clone, Copy)]
pub enum DaemonShutdownAccess {
Disabled,
Managed,
}
pub async fn start_control_socket_acceptor(
socket_path: AbsolutePathBuf,
transport_event_tx: mpsc::Sender<TransportEvent>,
shutdown_token: CancellationToken,
daemon_shutdown_access: DaemonShutdownAccess,
) -> IoResult<JoinHandle<()>> {
#[cfg(windows)]
let (socket_path, directory_guard) = {
@@ -54,6 +65,7 @@ pub async fn start_control_socket_acceptor(
transport_event_tx,
shutdown_token,
socket_guard,
daemon_shutdown_access,
)))
}
@@ -62,6 +74,7 @@ async fn run_control_socket_acceptor(
transport_event_tx: mpsc::Sender<TransportEvent>,
shutdown_token: CancellationToken,
socket_guard: ControlSocketFileGuard,
daemon_shutdown_access: DaemonShutdownAccess,
) {
let _socket_guard = socket_guard;
loop {
@@ -90,13 +103,33 @@ async fn run_control_socket_acceptor(
let transport_event_tx = transport_event_tx.clone();
tokio::spawn(async move {
let websocket_stream = match accept_async(stream).await {
let mut shutdown_request = false;
let websocket_stream = match accept_hdr_async(
stream,
|request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| {
if request.uri().path() == "/daemon/shutdown" {
if !matches!(daemon_shutdown_access, DaemonShutdownAccess::Managed) {
let mut rejection = Response::new(Some("unmanaged server".to_string()));
*rejection.status_mut() = StatusCode::FORBIDDEN;
return Err(rejection);
}
shutdown_request = true;
}
Ok(response)
},
)
.await
{
Ok(websocket_stream) => websocket_stream,
Err(err) => {
warn!("failed to upgrade control socket websocket connection: {err}");
return;
}
};
if shutdown_request {
run_daemon_shutdown(websocket_stream, transport_event_tx).await;
return;
}
let (websocket_writer, websocket_reader) = websocket_stream.split();
run_websocket_connection(websocket_writer, websocket_reader, transport_event_tx).await;
});
@@ -104,6 +137,24 @@ async fn run_control_socket_acceptor(
info!("control socket acceptor shutting down");
}
async fn run_daemon_shutdown(
mut websocket: tokio_tungstenite::WebSocketStream<UnixStream>,
transport_event_tx: mpsc::Sender<TransportEvent>,
) {
let pid = std::process::id().to_string();
if !matches!(websocket.next().await, Some(Ok(Message::Text(request))) if request == pid) {
return;
}
if websocket.send(Message::Text(pid.into())).await.is_err() {
return;
}
// Let the manager receive the acknowledgment before the main loop closes connections.
let _ = tokio::time::timeout(Duration::from_secs(2), websocket.next()).await;
let _ = transport_event_tx
.send(TransportEvent::DaemonShutdown)
.await;
}
pub async fn prepare_control_socket_path(socket_path: &Path) -> IoResult<()> {
if let Some(parent) = socket_path.parent() {
codex_uds::prepare_private_socket_directory(parent).await?;
@@ -1,5 +1,6 @@
use super::AppServerTransport;
use super::CHANNEL_CAPACITY;
use super::DaemonShutdownAccess;
use super::TransportEvent;
use super::acquire_app_server_startup_lock;
use super::app_server_control_socket_path;
@@ -64,6 +65,7 @@ async fn control_socket_acceptor_upgrades_and_forwards_websocket_text_messages_a
socket_path.clone(),
transport_event_tx,
shutdown_token.clone(),
DaemonShutdownAccess::Disabled,
)
.await
.expect("control socket acceptor should start");
@@ -141,6 +143,87 @@ async fn control_socket_acceptor_upgrades_and_forwards_websocket_text_messages_a
assert_socket_path_removed(socket_path.as_path());
}
#[tokio::test]
async fn shutdown_is_only_accepted_on_managed_local_socket_for_its_own_pid() {
let temp_dir = tempfile::TempDir::new().expect("temp dir");
let socket_path = test_socket_path(temp_dir.path());
let (tx, mut rx) = mpsc::channel(CHANNEL_CAPACITY);
let shutdown = CancellationToken::new();
let acceptor = start_control_socket_acceptor(
socket_path.clone(),
tx,
shutdown.clone(),
DaemonShutdownAccess::Disabled,
)
.await
.expect("acceptor");
let stream = connect_to_socket(socket_path.as_path())
.await
.expect("connect");
assert!(
client_async("ws://localhost/daemon/shutdown", stream)
.await
.is_err()
);
assert!(rx.try_recv().is_err());
shutdown.cancel();
acceptor.await.expect("acceptor shutdown");
let (tx, mut rx) = mpsc::channel(CHANNEL_CAPACITY);
let shutdown = CancellationToken::new();
let acceptor = start_control_socket_acceptor(
socket_path.clone(),
tx,
shutdown.clone(),
DaemonShutdownAccess::Managed,
)
.await
.expect("managed acceptor");
let stream = connect_to_socket(socket_path.as_path())
.await
.expect("connect");
let (mut websocket, _) = client_async("ws://localhost/daemon/shutdown", stream)
.await
.expect("upgrade");
websocket
.send(WebSocketMessage::Text("0".into()))
.await
.expect("wrong pid");
assert!(!matches!(
websocket.next().await,
Some(Ok(WebSocketMessage::Text(_)))
));
assert!(rx.try_recv().is_err());
let stream = connect_to_socket(socket_path.as_path())
.await
.expect("connect");
let (mut websocket, _) = client_async("ws://localhost/daemon/shutdown", stream)
.await
.expect("upgrade");
let pid = std::process::id().to_string();
websocket
.send(WebSocketMessage::Text(pid.clone().into()))
.await
.expect("request");
assert_eq!(
websocket.next().await.expect("ack").expect("ack frame"),
WebSocketMessage::Text(pid.into())
);
assert!(
rx.try_recv().is_err(),
"server must wait until the ack is received"
);
websocket.close(None).await.expect("confirm receipt");
assert!(matches!(
timeout(Duration::from_secs(2), rx.recv()).await,
Ok(Some(TransportEvent::DaemonShutdown))
));
shutdown.cancel();
acceptor.await.expect("acceptor shutdown");
}
#[tokio::test]
async fn app_server_startup_lock_serializes_waiters() {
let temp_dir = tempfile::TempDir::new().expect("temp dir");
@@ -177,6 +260,7 @@ async fn control_socket_file_is_private_after_bind() {
socket_path.clone(),
transport_event_tx,
shutdown_token.clone(),
DaemonShutdownAccess::Disabled,
)
.await
.expect("control socket acceptor should start");
@@ -199,9 +283,14 @@ async fn control_socket_pins_directory_until_shutdown() {
let moved = temp_dir.path().join("moved");
let (tx, _rx) = mpsc::channel::<TransportEvent>(CHANNEL_CAPACITY);
let shutdown = CancellationToken::new();
let acceptor = start_control_socket_acceptor(socket_path.clone(), tx, shutdown.clone())
.await
.expect("acceptor");
let acceptor = start_control_socket_acceptor(
socket_path.clone(),
tx,
shutdown.clone(),
DaemonShutdownAccess::Disabled,
)
.await
.expect("acceptor");
assert!(std::fs::rename(directory, &moved).is_err());
shutdown.cancel();
acceptor.await.expect("shutdown");