rattler_shell 0.29.3

A crate to help with activation and deactivation of a conda environment
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Running a child process within a time and an output bound.
//!
//! Both the detector executable and the shell that activates its environment
//! run through [`run_bounded`]: the clock starts when the process was spawned,
//! standard output and standard error are read concurrently against one
//! combined byte budget, and when either bound is reached the process and its
//! descendants are terminated. Standard error captured until then is kept for
//! diagnostics.

use std::{
    process::{ExitStatus, Stdio},
    time::{Duration, Instant},
};

use thiserror::Error;
use tokio::io::AsyncReadExt;

/// The bounds of one process run.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProcessLimits {
    /// How long the process may run after it was spawned.
    pub timeout: Duration,
    /// How many bytes of standard output and standard error, combined, the
    /// process may write.
    pub output_limit: usize,
}

/// A process that exited within the limits, with whatever status.
#[derive(Clone, Debug)]
pub struct ProcessOutput {
    /// The exit status.
    pub status: ExitStatus,
    /// Everything the process wrote to standard output.
    pub stdout: Vec<u8>,
    /// Everything the process wrote to standard error.
    pub stderr: Vec<u8>,
    /// How long the process ran.
    pub duration: Duration,
}

/// Why a process could not be run to completion within the limits.
#[derive(Debug, Error)]
pub enum ProcessError {
    /// The process could not be started.
    #[error("failed to start the process")]
    Spawn(#[source] std::io::Error),

    /// The process's output could not be read or its exit could not be
    /// awaited.
    #[error("failed to read the process output")]
    Io {
        /// The underlying error.
        #[source]
        source: std::io::Error,
        /// Standard error captured before the failure.
        stderr: Vec<u8>,
    },

    /// The process did not exit within the timeout and was terminated.
    #[error("the process did not finish within {timeout:?} and was terminated")]
    TimedOut {
        /// The timeout that was reached.
        timeout: Duration,
        /// Standard error captured before termination.
        stderr: Vec<u8>,
    },

    /// The process reached the combined output limit and was terminated.
    #[error("the process reached the {limit}-byte output limit and was terminated")]
    OutputLimitExceeded {
        /// The limit that was exceeded.
        limit: usize,
        /// Standard error captured before termination.
        stderr: Vec<u8>,
    },
}

impl ProcessError {
    /// Standard error captured before the run failed, where a process ran at
    /// all.
    pub fn stderr(&self) -> Option<&[u8]> {
        match self {
            Self::Spawn(_) => None,
            Self::Io { stderr, .. }
            | Self::TimedOut { stderr, .. }
            | Self::OutputLimitExceeded { stderr, .. } => Some(stderr),
        }
    }
}

/// Runs `command` with no standard input and both output streams captured,
/// within `limits`.
pub async fn run_bounded(
    command: &mut tokio::process::Command,
    limits: ProcessLimits,
) -> Result<ProcessOutput, ProcessError> {
    command
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true);
    #[cfg(unix)]
    command.process_group(0);

    let mut child = command.spawn().map_err(ProcessError::Spawn)?;
    let started = Instant::now();
    let mut tree = ProcessTree::new(&child).map_err(ProcessError::Spawn)?;
    let mut stdout_pipe = child.stdout.take().expect("stdout is piped");
    let mut stderr_pipe = child.stderr.take().expect("stderr is piped");

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let read_all = async {
        let mut stdout_buf = [0u8; 8192];
        let mut stderr_buf = [0u8; 8192];
        let mut stdout_open = true;
        let mut stderr_open = true;
        while stdout_open || stderr_open {
            let remaining = limits.output_limit - stdout.len() - stderr.len();
            if remaining == 0 {
                return Ok::<bool, std::io::Error>(false);
            }
            let capacity = remaining.min(stdout_buf.len());
            let read = tokio::select! {
                result = stdout_pipe.read(&mut stdout_buf[..capacity]), if stdout_open => match result? {
                    0 => { stdout_open = false; 0 }
                    n => { stdout.extend_from_slice(&stdout_buf[..n]); n }
                },
                result = stderr_pipe.read(&mut stderr_buf[..capacity]), if stderr_open => match result? {
                    0 => { stderr_open = false; 0 }
                    n => { stderr.extend_from_slice(&stderr_buf[..n]); n }
                },
            };
            if read > 0 && stdout.len() + stderr.len() >= limits.output_limit {
                return Ok::<bool, std::io::Error>(false);
            }
        }
        Ok(true)
    };

    let within_limits = match tokio::time::timeout(limits.timeout, read_all).await {
        Ok(Ok(within_limits)) => within_limits,
        Ok(Err(source)) => {
            tree.kill(&mut child).await;
            return Err(ProcessError::Io { source, stderr });
        }
        Err(_) => {
            tree.kill(&mut child).await;
            return Err(ProcessError::TimedOut {
                timeout: limits.timeout,
                stderr,
            });
        }
    };
    if !within_limits {
        tree.kill(&mut child).await;
        return Err(ProcessError::OutputLimitExceeded {
            limit: limits.output_limit,
            stderr,
        });
    }

    // The pipes are closed, so the process is exiting or has exited; the wait
    // still counts against the timeout.
    let remaining = limits.timeout.saturating_sub(started.elapsed());
    let Ok(status) = tokio::time::timeout(remaining, child.wait()).await else {
        tree.kill(&mut child).await;
        return Err(ProcessError::TimedOut {
            timeout: limits.timeout,
            stderr,
        });
    };
    let status = status.map_err(|source| ProcessError::Io {
        source,
        stderr: stderr.clone(),
    })?;
    #[cfg(unix)]
    {
        tree.pgid = None;
    }
    Ok(ProcessOutput {
        status,
        stdout,
        stderr,
        duration: started.elapsed(),
    })
}

/// A handle on a process and its descendants, so that all of them can be
/// terminated together.
struct ProcessTree {
    #[cfg(unix)]
    pgid: Option<libc::pid_t>,
    #[cfg(windows)]
    job: windows::JobObject,
}

impl ProcessTree {
    fn new(child: &tokio::process::Child) -> std::io::Result<Self> {
        #[cfg(unix)]
        {
            Ok(Self {
                pgid: child.id().and_then(|id| libc::pid_t::try_from(id).ok()),
            })
        }
        #[cfg(windows)]
        {
            Ok(Self {
                job: windows::JobObject::assign(child)?,
            })
        }
    }

    fn terminate(&mut self) {
        #[cfg(unix)]
        if let Some(pgid) = self.pgid.take() {
            // SAFETY: `killpg` has no memory-safety preconditions; the group id
            // is the one the child was spawned in.
            unsafe {
                libc::killpg(pgid, libc::SIGKILL);
            }
        }
        #[cfg(windows)]
        self.job.terminate();
    }

    /// Terminates the process group or job, then reaps the child.
    async fn kill(&mut self, child: &mut tokio::process::Child) {
        self.terminate();
        let _ = child.kill().await;
        let _ = child.wait().await;
    }
}

#[cfg(unix)]
impl Drop for ProcessTree {
    fn drop(&mut self) {
        self.terminate();
    }
}

#[cfg(windows)]
mod windows {
    use windows_sys::Win32::{
        Foundation::{CloseHandle, HANDLE},
        System::JobObjects::{
            AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
            JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation,
            SetInformationJobObject, TerminateJobObject,
        },
    };

    /// A job object that kills every process in it when it is closed.
    ///
    /// The child is assigned right after it was spawned. A child that starts
    /// its own children before that moment, which a batch file run through
    /// `cmd.exe` can in principle do, is not covered by the job; that is why
    /// terminating descendants is only a SHOULD.
    pub(super) struct JobObject(HANDLE);

    // SAFETY: a job object handle can be used from any thread.
    unsafe impl Send for JobObject {}
    unsafe impl Sync for JobObject {}

    impl JobObject {
        pub(super) fn assign(child: &tokio::process::Child) -> std::io::Result<Self> {
            // SAFETY: plain Win32 calls with valid arguments; the handle is
            // closed in `Drop`.
            unsafe {
                let job = CreateJobObjectW(std::ptr::null(), std::ptr::null());
                if job.is_null() {
                    return Err(std::io::Error::last_os_error());
                }
                let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = std::mem::zeroed();
                info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
                if SetInformationJobObject(
                    job,
                    JobObjectExtendedLimitInformation,
                    std::ptr::addr_of!(info).cast(),
                    std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
                ) == 0
                {
                    let error = std::io::Error::last_os_error();
                    CloseHandle(job);
                    return Err(error);
                }
                if let Some(process) = child.raw_handle()
                    && AssignProcessToJobObject(job, process as HANDLE) == 0
                {
                    let error = std::io::Error::last_os_error();
                    CloseHandle(job);
                    return Err(error);
                }
                Ok(Self(job))
            }
        }

        pub(super) fn terminate(&self) {
            // SAFETY: the handle is a valid job object owned by `self`.
            unsafe {
                TerminateJobObject(self.0, 1);
            }
        }
    }

    impl Drop for JobObject {
        fn drop(&mut self) {
            // SAFETY: the handle is a valid job object owned by `self`.
            unsafe {
                CloseHandle(self.0);
            }
        }
    }
}

#[cfg(all(test, unix))]
mod tests {
    use std::{
        io::{BufRead, BufReader, Read, Write},
        os::{fd::AsRawFd, unix::net::UnixStream},
    };

    use super::*;

    fn sh(script: &str) -> tokio::process::Command {
        let mut command = tokio::process::Command::new("sh");
        command.arg("-c").arg(script);
        command
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cancelling_a_run_terminates_its_descendants() {
        struct Cleanup {
            task: tokio::task::JoinHandle<Result<ProcessOutput, ProcessError>>,
            pgid: Option<libc::pid_t>,
        }

        impl Drop for Cleanup {
            fn drop(&mut self) {
                self.task.abort();
                if let Some(pgid) = self.pgid {
                    // SAFETY: this is the dedicated group created for the
                    // fixture, kept until all assertions and cleanup finish.
                    unsafe {
                        libc::killpg(pgid, libc::SIGKILL);
                    }
                }
            }
        }

        let (parent_socket, child_socket) = UnixStream::pair().unwrap();
        parent_socket
            .set_read_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        let descriptor = child_socket.as_raw_fd();
        let mut command = sh("printf '%s\\n' \"$$\" >&3\n\
             read gate <&3\n\
             sleep 300 &\n\
             printf 'ready\\n' >&3\n\
             wait");
        // SAFETY: only async-signal-safe descriptor operations run after
        // fork. The owning socket stays alive until the command is spawned.
        unsafe {
            command.pre_exec(move || {
                if libc::dup2(descriptor, 3) == -1 || libc::fcntl(3, libc::F_SETFD, 0) == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let mut cleanup = Cleanup {
            task: tokio::spawn(async move {
                let _socket = child_socket;
                run_bounded(
                    &mut command,
                    ProcessLimits {
                        timeout: Duration::from_secs(60),
                        output_limit: 1024,
                    },
                )
                .await
            }),
            pgid: None,
        };
        let mut reader = BufReader::new(parent_socket);
        let mut line = String::new();
        reader.read_line(&mut line).unwrap();
        cleanup.pgid = Some(line.trim().parse().unwrap());
        // No descendants are started until the fallback cleanup owns the
        // process group, including when the expected regression fails.
        reader.get_mut().write_all(b"go\n").unwrap();
        line.clear();
        reader.read_line(&mut line).unwrap();
        assert_eq!(line, "ready\n");

        cleanup.task.abort();
        assert!((&mut cleanup.task).await.unwrap_err().is_cancelled());
        let eof = reader.read_to_end(&mut Vec::new());
        drop(cleanup);
        assert_eq!(
            eof.expect("a descendant kept its inherited socket open after cancellation"),
            0
        );
    }

    #[tokio::test]
    async fn captures_both_streams_and_the_status() {
        let output = run_bounded(
            &mut sh("echo out; echo err >&2; exit 3"),
            ProcessLimits {
                timeout: Duration::from_secs(10),
                output_limit: 1024,
            },
        )
        .await
        .unwrap();
        assert_eq!(output.status.code(), Some(3));
        assert_eq!(output.stdout, b"out\n");
        assert_eq!(output.stderr, b"err\n");
    }

    #[tokio::test]
    async fn timeout_terminates_the_process_group() {
        // The background child keeps the pipes open; it must be killed too.
        let started = Instant::now();
        let err = run_bounded(
            &mut sh("echo starting >&2; sleep 30 & sleep 30"),
            ProcessLimits {
                timeout: Duration::from_millis(300),
                output_limit: 1024,
            },
        )
        .await
        .unwrap_err();
        assert!(started.elapsed() < Duration::from_secs(5));
        match err {
            ProcessError::TimedOut { stderr, .. } => assert_eq!(stderr, b"starting\n"),
            other => panic!("unexpected error {other}"),
        }
    }

    #[tokio::test]
    async fn output_budget_stops_at_the_combined_limit() {
        let limits = ProcessLimits {
            timeout: Duration::from_secs(10),
            output_limit: 4,
        };
        let below = run_bounded(&mut sh("printf 123 >&2"), limits)
            .await
            .unwrap();
        assert_eq!(below.stderr, b"123");
        for (script, expected_stderr) in [
            ("printf 1234 >&2", b"1234".as_slice()),
            ("printf 12345 >&2", b"1234".as_slice()),
            ("printf 12; printf 34 >&2", b"34".as_slice()),
        ] {
            match run_bounded(&mut sh(script), limits).await.unwrap_err() {
                ProcessError::OutputLimitExceeded { limit, stderr } => {
                    assert_eq!(limit, 4);
                    assert_eq!(stderr, expected_stderr);
                }
                other => panic!("unexpected error {other}"),
            }
        }
    }

    #[tokio::test]
    async fn output_limit_terminates_the_process() {
        let err = run_bounded(
            &mut sh("echo noisy >&2; while true; do echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; done"),
            ProcessLimits {
                timeout: Duration::from_secs(20),
                output_limit: 4096,
            },
        )
        .await
        .unwrap_err();
        match err {
            ProcessError::OutputLimitExceeded { limit, stderr } => {
                assert_eq!(limit, 4096);
                assert!(stderr.len() <= limit);
            }
            other => panic!("unexpected error {other}"),
        }
    }
}