Skip to content

Commit 16d0495

Browse files
committed
Auto merge of #11624 - vext01:poll-loop-fixes, r=ehuss
Poll loop fixes ### What does this PR try to resolve? A couple of minor issues. See individual commits. ### How should we test and review this PR? The existing test suite should have sufficient coverage. ### Additional information (I have 5 apparently unrelated failures locally when running `cargo test`)
2 parents 99ad42d + 5c3825f commit 16d0495

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

crates/cargo-util/src/read2.rs

Lines changed: 12 additions & 5 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;
@@ -32,7 +39,7 @@ mod imp {
3239
let mut errfd = 1;
3340

3441
while nfds > 0 {
35-
// wait for either pipe to become readable using `select`
42+
// wait for either pipe to become readable using `poll`
3643
let r = unsafe { libc::poll(fds.as_mut_ptr(), nfds, -1) };
3744
if r == -1 {
3845
let err = io::Error::last_os_error();

0 commit comments

Comments
 (0)