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
use std::io::{self, Read, Write};
use std::process::{Command, ExitStatus, Stdio};
use std::time::Duration;

use async_trait::async_trait;
use blocking::unblock;
use wait_timeout::ChildExt as _;

#[derive(Debug)]
pub struct SpawnAsyncOutput {
    pub stdout: Vec<u8>,
    pub stderr: Vec<u8>,
    pub exit_status: ExitStatus,
}

#[async_trait]
pub trait ChildExt {
    async fn spawn_async(
        &mut self,
        stdin: Option<Vec<u8>>,
        timeout: Option<Duration>,
        max_size: Option<usize>,
    ) -> io::Result<SpawnAsyncOutput>;
}

#[async_trait]
impl ChildExt for Command {
    async fn spawn_async(
        &mut self,
        stdin: Option<Vec<u8>>,
        timeout: Option<Duration>,
        max_size: Option<usize>,
    ) -> io::Result<SpawnAsyncOutput> {
        let command = self.stdout(Stdio::piped()).stderr(Stdio::piped());
        if let Some(_) = stdin {
            command.stdin(Stdio::piped());
        }

        let mut child = command.spawn()?;

        unblock(move || {
            if let Some(stdin_bytes) = stdin {
                let stdin = match child.stdin.as_mut() {
                    Some(stdin) => stdin,
                    None => unreachable!("never"),
                };
                stdin.write_all(&stdin_bytes[..])?;
            }

            let exit_status =
                match child.wait_timeout(timeout.unwrap_or(Duration::from_millis(1000)))? {
                    Some(exit_status) => exit_status,
                    None => {
                        // child hasn't exited yet
                        child.kill()?;
                        child.wait()?;

                        return Err(io::Error::new(io::ErrorKind::TimedOut, "run timeout"));
                    }
                };

            let max_size = max_size.unwrap_or(2048);
            let mut buf = Vec::<u8>::with_capacity(max_size);
            buf.resize(max_size, 0);

            let stdout = child
                .stdout
                .as_mut()
                .ok_or(io::Error::new(io::ErrorKind::Other, "never"))?;
            let n = stdout.read(&mut buf)?;
            let stdout_bytes = buf[..n].to_vec();

            let stderr = child
                .stderr
                .as_mut()
                .ok_or(io::Error::new(io::ErrorKind::Other, "never"))?;
            let n = stderr.read(&mut buf)?;
            let stderr_bytes = buf[..n].to_vec();

            Ok(SpawnAsyncOutput {
                stdout: stdout_bytes,
                stderr: stderr_bytes,
                exit_status,
            })
        })
        .await
    }
}