forked from openai/codex
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathspawn.rs
More file actions
182 lines (160 loc) · 6.23 KB
/
spawn.rs
File metadata and controls
182 lines (160 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::collections::HashMap;
use std::io;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;
use tokio::process::Child;
use tokio::process::Command;
use tokio::time::sleep;
use tracing::trace;
use crate::protocol::SandboxPolicy;
/// Experimental environment variable that will be set to some non-empty value
/// if both of the following are true:
///
/// 1. The process was spawned by Codex as part of a shell tool call.
/// 2. SandboxPolicy.has_full_network_access() was false for the tool call.
///
/// We may try to have just one environment variable for all sandboxing
/// attributes, so this may change in the future.
pub const CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR: &str = "CODEX_SANDBOX_NETWORK_DISABLED";
/// Should be set when the process is spawned under a sandbox. Currently, the
/// value is "seatbelt" for macOS, but it may change in the future to
/// accommodate sandboxing configuration and other sandboxing mechanisms.
pub const CODEX_SANDBOX_ENV_VAR: &str = "CODEX_SANDBOX";
const SPAWN_RETRY_DELAYS_MS: [u64; 3] = [0, 10, 50];
fn is_temporary_resource_error(err: &io::Error) -> bool {
err.kind() == io::ErrorKind::WouldBlock
|| matches!(err.raw_os_error(), Some(35) | Some(libc::ENOMEM))
}
fn spawn_with_retry_blocking<F, T>(mut spawn: F) -> io::Result<T>
where
F: FnMut() -> io::Result<T>,
{
let mut last_err: Option<io::Error> = None;
for delay_ms in SPAWN_RETRY_DELAYS_MS {
match spawn() {
Ok(child) => return Ok(child),
Err(err) if is_temporary_resource_error(&err) => {
last_err = Some(err);
if delay_ms > 0 {
std::thread::sleep(Duration::from_millis(delay_ms));
}
}
Err(err) => return Err(err),
}
}
Err(last_err.unwrap_or_else(|| io::Error::other("spawn failed")))
}
async fn spawn_with_retry_async<F, T>(mut spawn: F) -> io::Result<T>
where
F: FnMut() -> io::Result<T>,
{
let mut last_err: Option<io::Error> = None;
for delay_ms in SPAWN_RETRY_DELAYS_MS {
match spawn() {
Ok(child) => return Ok(child),
Err(err) if is_temporary_resource_error(&err) => {
last_err = Some(err);
if delay_ms > 0 {
sleep(Duration::from_millis(delay_ms)).await;
}
}
Err(err) => return Err(err),
}
}
Err(last_err.unwrap_or_else(|| io::Error::other("spawn failed")))
}
pub fn spawn_std_command_with_retry(
cmd: &mut std::process::Command,
) -> io::Result<std::process::Child> {
spawn_with_retry_blocking(|| cmd.spawn())
}
pub async fn spawn_tokio_command_with_retry(cmd: &mut Command) -> io::Result<Child> {
spawn_with_retry_async(|| cmd.spawn()).await
}
#[derive(Debug, Clone, Copy)]
pub enum StdioPolicy {
RedirectForShellTool,
Inherit,
}
/// Spawns the appropriate child process for the ExecParams and SandboxPolicy,
/// ensuring the args and environment variables used to create the `Command`
/// (and `Child`) honor the configuration.
///
/// For now, we take `SandboxPolicy` as a parameter to spawn_child() because
/// we need to determine whether to set the
/// `CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR` environment variable.
pub(crate) async fn spawn_child_async(
program: PathBuf,
args: Vec<String>,
#[cfg_attr(not(unix), allow(unused_variables))] arg0: Option<&str>,
cwd: PathBuf,
sandbox_policy: &SandboxPolicy,
stdio_policy: StdioPolicy,
env: HashMap<String, String>,
) -> std::io::Result<Child> {
trace!(
"spawn_child_async: {program:?} {args:?} {arg0:?} {cwd:?} {sandbox_policy:?} {stdio_policy:?} {env:?}"
);
let mut cmd = Command::new(&program);
#[cfg(unix)]
cmd.arg0(arg0.map_or_else(|| program.to_string_lossy().to_string(), String::from));
cmd.args(args);
cmd.current_dir(cwd);
cmd.env_clear();
cmd.envs(env);
if !sandbox_policy.has_full_network_access() {
cmd.env(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR, "1");
}
// If this Codex process dies (including being killed via SIGKILL), we want
// any child processes that were spawned as part of a `"shell"` tool call
// to also be terminated.
// Ensure children form their own process group; on timeout we can kill the group.
// Also, on Linux, set PDEATHSIG so children die if parent dies.
#[cfg(unix)]
unsafe {
#[cfg(target_os = "linux")]
let exec_memory_max_bytes = match stdio_policy {
StdioPolicy::RedirectForShellTool => crate::cgroup::default_exec_memory_max_bytes(),
StdioPolicy::Inherit => None,
};
cmd.pre_exec(move || {
// Start a new process group
let _ = libc::setpgid(0, 0);
#[cfg(target_os = "linux")]
{
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) == -1 {
return Err(std::io::Error::last_os_error());
}
if libc::getppid() == 1 {
libc::raise(libc::SIGTERM);
}
if let Some(memory_max_bytes) = exec_memory_max_bytes {
crate::cgroup::best_effort_attach_self_to_exec_cgroup(
libc::getpid() as u32,
memory_max_bytes,
);
}
}
Ok(())
});
}
match stdio_policy {
StdioPolicy::RedirectForShellTool => {
// Do not create a file descriptor for stdin because otherwise some
// commands may hang forever waiting for input. For example, ripgrep has
// a heuristic where it may try to read from stdin as explained here:
// https://github.com/BurntSushi/ripgrep/blob/e2362d4d5185d02fa857bf381e7bd52e66fafc73/crates/core/flags/hiargs.rs#L1101-L1103
cmd.stdin(Stdio::null());
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
}
StdioPolicy::Inherit => {
// Inherit stdin, stdout, and stderr from the parent process.
cmd.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
}
}
cmd.kill_on_drop(true);
spawn_tokio_command_with_retry(&mut cmd).await
}