Skip to content

Commit 5c3825f

Browse files
committed
Set non-blocking IO more robustly.
- Check for errors. - Add O_NONBLOCK on top of any existing flags. set_nonblock() is adapted from lang_tester: https://github.com/softdevteam/lang_tester/blob/e01072a0a4c5e37f2e585c48efffcf540cd7b6a4/src/tester.rs#L1041-L1048
1 parent 493ca33 commit 5c3825f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

crates/cargo-util/src/read2.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@ pub use self::imp::read2;
22

33
#[cfg(unix)]
44
mod imp {
5+
use libc::{c_int, fcntl, F_GETFL, F_SETFL, O_NONBLOCK};
56
use std::io;
67
use std::io::prelude::*;
78
use std::mem;
89
use std::os::unix::prelude::*;
910
use std::process::{ChildStderr, ChildStdout};
1011

12+
fn set_nonblock(fd: c_int) -> io::Result<()> {
13+
let flags = unsafe { fcntl(fd, F_GETFL) };
14+
if flags == -1 || unsafe { fcntl(fd, F_SETFL, flags | O_NONBLOCK) } == -1 {
15+
return Err(io::Error::last_os_error());
16+
}
17+
Ok(())
18+
}
19+
1120
pub fn read2(
1221
mut out_pipe: ChildStdout,
1322
mut err_pipe: ChildStderr,
1423
data: &mut dyn FnMut(bool, &mut Vec<u8>, bool),
1524
) -> io::Result<()> {
16-
unsafe {
17-
libc::fcntl(out_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
18-
libc::fcntl(err_pipe.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK);
19-
}
25+
set_nonblock(out_pipe.as_raw_fd())?;
26+
set_nonblock(err_pipe.as_raw_fd())?;
2027

2128
let mut out_done = false;
2229
let mut err_done = false;

0 commit comments

Comments
 (0)