exec-server: preserve fs helper CoreFoundation env (#25118)

## Summary
- preserve macOS `__CF_USER_TEXT_ENCODING` when launching the sandboxed
fs helper
- keep the fs-helper env narrow; this adds only the CoreFoundation
startup var instead of copying the broader MCP stdio baseline
- add focused coverage that the helper keeps that var without admitting
`HOME`

## Diagnosis
The sandboxed fs helper is not launched like a normal child process.
Exec-server rebuilds its environment from an allowlist, then calls
`env_clear()` before re-execing Codex with `--codex-run-as-fs-helper`.
That helper dispatches before the normal Codex startup path and only
needs to boot a small Tokio runtime, read one JSON request from stdin,
perform the direct filesystem operation, and write one JSON response.

The reported macOS hang sampled the helper before Rust main, in
CoreFoundation initialization while resolving the default text encoding:
`_CFStringGetUserDefaultEncoding -> getpwuid_r -> notify_register_check
-> bootstrap_look_up3 -> mach_msg2_trap`. The fs-helper allowlist kept
`PATH` and temp vars for runtime needs, but it dropped macOS
`__CF_USER_TEXT_ENCODING`. Other Codex subprocess launchers that
intentionally build a minimal Unix baseline, such as MCP stdio, already
preserve that variable.

My read is that stripping `__CF_USER_TEXT_ENCODING` forced this internal
helper down CoreFoundation's fallback user-lookup path, and that lookup
intermittently wedged on the affected machine before the helper could
read stdin or touch the target file. Preserving only this macOS startup
variable avoids that fallback without broadening the fs-helper
environment to shell-like vars such as `HOME`, `USER`, locale settings,
terminal settings, or proxy credentials.

Internal Slack thread omitted from the public PR body.

## Validation
- `cd codex-rs && just fmt`
- `git diff --check`
This commit is contained in:
starr-openai
2026-05-29 12:20:17 -07:00
committed by GitHub
parent 20da4c37c5
commit a717e4ef31

View File

@@ -230,6 +230,8 @@ fn helper_env_from_vars(
fn helper_env_key_is_allowed(key: &str) -> bool {
FS_HELPER_ENV_ALLOWLIST.contains(&key)
// CoreFoundation consults this before falling back to user lookup during helper startup.
|| (cfg!(target_os = "macos") && key == "__CF_USER_TEXT_ENCODING")
|| bazel_bwrap_env_key_is_allowed(key)
|| (cfg!(windows) && key.eq_ignore_ascii_case("PATH"))
}
@@ -434,6 +436,26 @@ mod tests {
);
}
#[cfg(target_os = "macos")]
#[test]
fn helper_env_preserves_corefoundation_text_encoding() {
let env = helper_env_from_vars(
[
("__CF_USER_TEXT_ENCODING", "0x1F6:0x0:0x0"),
("HOME", "/Users/test"),
]
.map(|(key, value)| (OsString::from(key), OsString::from(value))),
);
assert_eq!(
env,
HashMap::from([(
"__CF_USER_TEXT_ENCODING".to_string(),
"0x1F6:0x0:0x0".to_string(),
)])
);
}
#[cfg(windows)]
#[test]
fn helper_env_preserves_windows_path_key_for_system_bwrap_discovery() {