Support managed app-server lifecycle on Windows (#42381)

## Why

The PID-managed app-server lifecycle previously supported Unix only, leaving Windows unable to start and stop the managed daemon.

## What changed

- Launch the Windows daemon as a detached process and fail clearly when the host Job Object prevents detachment.
- Add Windows file locking and process creation-time checks so stale or reused PIDs cannot cause an unrelated process to be terminated.
- Request graceful shutdown through a shutdown file, then terminate the verified process after the grace period.
- Prepare private daemon state directories consistently and pin the executable path across installer junction updates.

## Testing

Add Windows tests for process-query permissions, reused and inaccessible PIDs, and stale-record cleanup.

GitOrigin-RevId: 5ea579a7c3dc8bb6122ec3a02513836ccaacc41c
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-02 23:10:08 +00:00
1 parent 0588fc941c
commit 715294448f
9 files changed
+464 -37

No files matched your search

+39 -7
View File
@@ -1,4 +1,8 @@
//! Managed app-server lifecycle, serialized across CLI invocations and the updater.
mod backend;
#[cfg(windows)]
use backend::windows::try_lock_file;
mod client;
mod managed_install;
mod remote_control_client;
@@ -727,12 +731,18 @@ impl Daemon {
async fn open_operation_lock_file(&self) -> Result<tokio::fs::File> {
if let Some(parent) = self.operation_lock_file.parent() {
tokio::fs::create_dir_all(parent).await.with_context(|| {
format!(
"failed to create daemon state directory {}",
parent.display()
)
})?;
#[cfg(unix)]
if let Some(home) = parent.parent() {
tokio::fs::create_dir_all(home).await?;
}
codex_uds::prepare_private_socket_directory(parent)
.await
.with_context(|| {
format!(
"failed to create daemon state directory {}",
parent.display()
)
})?;
}
tokio::fs::OpenOptions::new()
.create(true)
@@ -842,7 +852,7 @@ fn try_lock_file(file: &tokio::fs::File) -> Result<bool> {
Err(err).context("failed to lock daemon operation")
}
#[cfg(not(unix))]
#[cfg(not(any(unix, windows)))]
fn try_lock_file(_file: &tokio::fs::File) -> Result<bool> {
Ok(true)
}
@@ -1006,6 +1016,28 @@ mod tests {
);
}
#[tokio::test]
async fn stop_creates_missing_home_parent() {
let temp = TempDir::new().expect("temp dir");
let state = temp.path().join("missing-home").join("daemon-state");
let daemon = Daemon {
socket_path: state.join("server.sock"),
pid_file: state.join("server.pid"),
update_pid_file: state.join("updater.pid"),
operation_lock_file: state.join("daemon.lock"),
settings_file: state.join("settings.json"),
managed_codex_bin: state.join("missing-codex"),
};
assert_eq!(
daemon
.run(super::LifecycleCommand::Stop)
.await
.expect("stop on fresh home")
.status,
LifecycleStatus::NotRunning,
);
}
#[tokio::test]
async fn not_ready_context_reports_daemon_app_server_before_stderr() {
let temp_dir = TempDir::new().expect("temp dir");