mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Add Windows MXC sandbox launch diagnostics (#62439)
## Why Launch errors and timeouts can be hard to diagnose without knowing which stage the launcher reached or how long each stage took. ## What changed Log MXC launch phases by process ID in the Windows sandbox runtime log. Distinguish filesystem helper launches from regular commands and record filesystem grant counts, preparation, spawn, and wait durations, and launch outcomes or errors. Reuse the runtime log's rolling file appender across entries in the same launcher process. GitOrigin-RevId: 8d55261fc9efeab8afe4b5be67b5aacb6868f3da
This commit is contained in:
@@ -166,6 +166,7 @@ pub fn run_main() -> ! {
|
||||
match windows::run() {
|
||||
Ok(exit_code) => std::process::exit(exit_code),
|
||||
Err(error) => {
|
||||
windows::log_launch(format_args!("launch failed: {error:#}"));
|
||||
eprintln!("MXC launcher: {error:#}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
//! Run an already prepared MXC request with inherited stdio and job ownership.
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::windows::log_launch;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use mxc_sdk::v1;
|
||||
@@ -12,8 +15,17 @@ unsafe extern "system" {}
|
||||
pub(super) fn launch(request: v1::ContainerRequest) -> Result<i32> {
|
||||
let mut options = v1::SpawnExtendedOptions::default();
|
||||
options.stdio = v1::SpawnStdio::Inherit;
|
||||
let spawning = Instant::now();
|
||||
let mut child = v1::spawn_with_extended_options(request, options)?;
|
||||
match child.wait().context("waiting for the MXC command")? {
|
||||
let spawn_ms = spawning.elapsed().as_millis();
|
||||
log_launch(format_args!("phase=wait spawn_ms={spawn_ms}"));
|
||||
let waiting = Instant::now();
|
||||
let outcome = child.wait().context("waiting for the MXC command")?;
|
||||
let wait_ms = waiting.elapsed().as_millis();
|
||||
log_launch(format_args!(
|
||||
"phase=finished outcome={outcome:?} wait_ms={wait_ms}"
|
||||
));
|
||||
match outcome {
|
||||
v1::WaitResult::Exited(code) => Ok(code),
|
||||
v1::WaitResult::TimedOut => anyhow::bail!("MXC command timed out"),
|
||||
}
|
||||
|
||||
@@ -136,6 +136,8 @@ pub(super) fn build_request(
|
||||
}
|
||||
let policy = materialize_temporary_paths(permissions.file_system_sandbox_policy(), &env)?;
|
||||
#[cfg(windows)]
|
||||
crate::windows::log_launch("phase=prepare_deny_parents");
|
||||
#[cfg(windows)]
|
||||
for denied in policy.get_unreadable_roots_with_cwd(cwd) {
|
||||
let Some(parent) = denied.as_path().parent() else {
|
||||
continue;
|
||||
@@ -185,6 +187,8 @@ pub(super) fn build_request(
|
||||
}
|
||||
}
|
||||
let full_disk_write = policy.has_full_disk_write_access();
|
||||
#[cfg(windows)]
|
||||
crate::windows::log_launch("phase=resolve_filesystem_policy");
|
||||
let volumes = volume_roots
|
||||
.iter()
|
||||
.map(PathUri::from_host_native_path)
|
||||
@@ -199,6 +203,8 @@ pub(super) fn build_request(
|
||||
let (roots, readable_roots, denied_paths) = if fs == policy {
|
||||
let resolved =
|
||||
fs.resolve_for_enforcement(cwd, FileSystemPolicyProjection::PreserveMutableRoots)?;
|
||||
#[cfg(windows)]
|
||||
crate::windows::log_launch("phase=resolve_deny_paths");
|
||||
let denies = expand_windows_deny_read_paths(&resolved, &absolute_cwd);
|
||||
(resolved.writable_roots, resolved.readable_roots, denies)
|
||||
} else {
|
||||
@@ -209,9 +215,12 @@ pub(super) fn build_request(
|
||||
} else {
|
||||
fs.get_writable_roots_with_cwd_preserving_mutable_paths(cwd)
|
||||
};
|
||||
let readable_roots = fs.get_readable_roots_with_cwd(cwd);
|
||||
#[cfg(windows)]
|
||||
crate::windows::log_launch("phase=resolve_deny_paths");
|
||||
(
|
||||
roots,
|
||||
fs.get_readable_roots_with_cwd(cwd),
|
||||
readable_roots,
|
||||
resolve_windows_deny_read_paths(&policy, &absolute_cwd),
|
||||
)
|
||||
};
|
||||
@@ -266,6 +275,8 @@ pub(super) fn build_request(
|
||||
.iter()
|
||||
.map(PathUri::from_host_native_path)
|
||||
.collect::<std::io::Result<HashSet<_>>>()?;
|
||||
#[cfg(windows)]
|
||||
crate::windows::log_launch("phase=expand_volume_grants");
|
||||
prune_unavailable_volume_roots(&mut write, &volume_roots);
|
||||
prune_unavailable_volume_roots(&mut read, &volume_roots);
|
||||
// BaseContainer owns alias precedence for the emitted paths. Do not call
|
||||
@@ -326,6 +337,13 @@ pub(super) fn build_request(
|
||||
Ok((key.to_owned(), value.to_owned()))
|
||||
})
|
||||
.collect::<Result<Vec<_>, PolicyError>>()?;
|
||||
#[cfg(windows)]
|
||||
crate::windows::log_launch(format_args!(
|
||||
"filesystem grants read={} write={} deny={}",
|
||||
read.len(),
|
||||
write.len(),
|
||||
deny.len()
|
||||
));
|
||||
Ok(ContainerRequest {
|
||||
working_directory: Some(
|
||||
command_cwd
|
||||
|
||||
@@ -4,6 +4,7 @@ use std::collections::HashMap;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::ensure;
|
||||
@@ -30,10 +31,17 @@ impl Drop for ComUninitializeGuard {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn log_launch(event: impl std::fmt::Display) {
|
||||
let pid = std::process::id();
|
||||
codex_windows_sandbox::log_runtime_note(&format!("MXC pid={pid} {event}"));
|
||||
}
|
||||
|
||||
pub(super) fn run() -> Result<i32> {
|
||||
if let Some(codex_home) = std::env::args_os().nth(2) {
|
||||
codex_windows_sandbox::init_runtime_log(Path::new(&codex_home));
|
||||
}
|
||||
log_launch("phase=prepare_request");
|
||||
let preparing = Instant::now();
|
||||
ensure!(
|
||||
crate::is_available(),
|
||||
"native MXC is unavailable on this Windows build"
|
||||
@@ -54,10 +62,20 @@ pub(super) fn run() -> Result<i32> {
|
||||
// Serde errors can quote arbitrary payload values; do not print them to stderr.
|
||||
let command = crate::transport::decode(&mut env)
|
||||
.map_err(|_| anyhow::anyhow!("invalid MXC launcher request"))?;
|
||||
// The transport itself proves this is an explicit child environment; it
|
||||
// may intentionally contain no variables once launcher state is removed.
|
||||
let kind = match command.command.as_slice() {
|
||||
[_, arg] if arg == "--codex-run-as-fs-helper" => "fs_helper",
|
||||
_ => "command",
|
||||
};
|
||||
log_launch(format_args!("phase=prepare_volumes kind={kind}"));
|
||||
let command_cwd = std::env::current_dir()?;
|
||||
let volumes = volume_roots(&command.sandbox_policy_cwd, &command_cwd)?;
|
||||
let volume_count = volumes.len();
|
||||
log_launch(format_args!(
|
||||
"phase=prepare_known_folders volumes={volume_count}"
|
||||
));
|
||||
let platform_roots = platform_read_roots()?;
|
||||
// The transport itself proves this is an explicit child environment; it
|
||||
// may intentionally contain no variables once launcher state is removed.
|
||||
let request = crate::policy::build_request(
|
||||
&command,
|
||||
&command_cwd,
|
||||
@@ -65,8 +83,10 @@ pub(super) fn run() -> Result<i32> {
|
||||
.map(|(key, value)| format!("{key}={value}"))
|
||||
.collect(),
|
||||
&volumes,
|
||||
&platform_read_roots()?,
|
||||
&platform_roots,
|
||||
)?;
|
||||
let prep_ms = preparing.elapsed().as_millis();
|
||||
log_launch(format_args!("phase=spawn prep_ms={prep_ms}"));
|
||||
crate::native::launch(request)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,12 +20,16 @@ pub const MAX_LOG_FILES: usize = 90;
|
||||
// Service provisioning never appends to a caller-controlled existing log entry.
|
||||
// An initialized but empty slot also prevents falling back after an open failure.
|
||||
static SETUP_LOG: OnceLock<Mutex<Option<(File, OwnedHandle)>>> = OnceLock::new();
|
||||
static RUNTIME_LOG_DIR: OnceLock<PathBuf> = OnceLock::new();
|
||||
static RUNTIME_LOG: OnceLock<(PathBuf, Mutex<Option<RollingFileAppender>>)> = OnceLock::new();
|
||||
|
||||
/// Set the default sandbox log directory for this launcher process.
|
||||
pub fn init_runtime_log(codex_home: &Path) {
|
||||
if codex_home.is_absolute() {
|
||||
let _ = RUNTIME_LOG_DIR.set(crate::sandbox_dir(codex_home));
|
||||
RUNTIME_LOG.get_or_init(|| {
|
||||
let dir = crate::sandbox_dir(codex_home);
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
(dir, Mutex::new(None))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +121,18 @@ fn append_line(line: &str, base_dir: Option<&Path>) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(dir) = base_dir
|
||||
if let Some((dir, cache)) = RUNTIME_LOG.get()
|
||||
&& base_dir == Some(dir.as_path())
|
||||
{
|
||||
if let Ok(mut cached) = cache.lock() {
|
||||
if cached.is_none() {
|
||||
*cached = log_writer(dir);
|
||||
}
|
||||
if let Some(writer) = cached.as_mut() {
|
||||
let _ = writer.write_all(format!("{line}\n").as_bytes());
|
||||
}
|
||||
}
|
||||
} else if let Some(dir) = base_dir
|
||||
&& let Some(mut f) = log_writer(dir)
|
||||
{
|
||||
let _ = writeln!(f, "{line}");
|
||||
@@ -155,7 +170,7 @@ pub fn log_note(msg: &str, base_dir: Option<&Path>) {
|
||||
|
||||
/// Log a note in a launcher process configured with a sandbox log directory.
|
||||
pub fn log_runtime_note(msg: &str) {
|
||||
if let Some(dir) = RUNTIME_LOG_DIR.get() {
|
||||
if let Some((dir, _)) = RUNTIME_LOG.get() {
|
||||
log_note(msg, Some(dir));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user