Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handling of WouldBlock error. #68

Merged
merged 1 commit into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/process/child.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::{MAX_EVENTS, WAIT_FOR_INIT};
use std::io::ErrorKind;
use std::io::Read;
use std::io::Write;

use anyhow::{bail, Result};
use mio::unix::pipe;
use mio::unix::pipe::Receiver;
use mio::unix::pipe::Sender;
use mio::{Events, Interest, Poll, Token};
use nix::unistd::Pid;
use std::io::Read;
use std::io::Write;

use super::{MAX_EVENTS, WAIT_FOR_INIT};
use crate::process::message::Message;

// Token is used to identify which socket generated an event
Expand Down Expand Up @@ -97,7 +99,15 @@ impl ChildProcess {
if let CHILD = event.token() {
// read message from the init process
let mut buf = [0; 1];
receiver.read_exact(&mut buf)?;
match receiver.read_exact(&mut buf) {
// This error simply means that there are no more incoming connections waiting to be accepted at this point.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(
"Failed to receive a message from the child process. {:?}",
e
),
_ => (),
}
match Message::from(u8::from_be_bytes(buf)) {
Message::InitReady => return Ok(()),
msg => bail!("receive unexpected message {:?} in child process", msg),
Expand Down
21 changes: 19 additions & 2 deletions src/process/parent.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::ErrorKind;
use std::io::Read;

use super::{MAX_EVENTS, WAIT_FOR_CHILD};
Expand Down Expand Up @@ -47,13 +48,29 @@ impl ParentProcess {
if let PARENT = event.token() {
// read data from pipe
let mut buf = [0; 1];
self.receiver.read_exact(&mut buf)?;
match self.receiver.read_exact(&mut buf) {
// This error simply means that there are no more incoming connections waiting to be accepted at this point.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(
"Failed to receive a message from the child process. {:?}",
e
),
_ => (),
};
// convert to Message wrapper
match Message::from(u8::from_be_bytes(buf)) {
Message::ChildReady => {
// read pid of init process forked by child, 4 bytes as the type is i32
let mut buf = [0; 4];
self.receiver.read_exact(&mut buf)?;
match self.receiver.read_exact(&mut buf) {
// This error simply means that there are no more incoming connections waiting to be accepted at this point.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(
"Failed to receive a message from the child process. {:?}",
e
),
_ => (),
}
return Ok(i32::from_be_bytes(buf));
}
msg => bail!("receive unexpected message {:?} in parent process", msg),
Expand Down