Skip to content

Commit 6075bb9

Browse files
committed
Set write to non-blocking in unix::Client::new
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 466b694 commit 6075bb9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/unix.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ impl Client {
4040
limit -= n;
4141
}
4242

43+
set_nonblocking(client.write.as_raw_fd(), false)?;
44+
4345
Ok(client)
4446
}
4547

@@ -64,14 +66,20 @@ impl Client {
6466
return Err(err);
6567
}
6668
}
67-
_ => return Ok(Client::from_fds(pipes[0], pipes[1])),
69+
_ => {
70+
set_nonblocking(pipes[1], true)?;
71+
return Ok(Client::from_fds(pipes[0], pipes[1]));
72+
}
6873
}
6974
}
7075
}
7176

7277
cvt(libc::pipe(pipes.as_mut_ptr()))?;
7378
drop(set_cloexec(pipes[0], true));
7479
drop(set_cloexec(pipes[1], true));
80+
81+
set_nonblocking(pipes[1], true)?;
82+
7583
Ok(Client::from_fds(pipes[0], pipes[1]))
7684
}
7785

@@ -334,6 +342,16 @@ fn set_cloexec(fd: c_int, set: bool) -> io::Result<()> {
334342
}
335343
}
336344

345+
fn set_nonblocking(fd: c_int, set: bool) -> io::Result<()> {
346+
let status_flag = if set { libc::O_NONBLOCK } else { 0 };
347+
348+
unsafe {
349+
cvt(libc::fcntl(fd, libc::F_SETFL, status_flag))?;
350+
}
351+
352+
Ok(())
353+
}
354+
337355
fn cvt(t: c_int) -> io::Result<c_int> {
338356
if t == -1 {
339357
Err(io::Error::last_os_error())

0 commit comments

Comments
 (0)