From 8652f276cdef5f888f7292ce926f23b71995c056 Mon Sep 17 00:00:00 2001 From: utam0k Date: Sun, 6 Jun 2021 18:40:37 +0900 Subject: [PATCH] add handling of WouldBlock error. --- src/process/child.rs | 18 ++++++++++++++---- src/process/parent.rs | 21 +++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/process/child.rs b/src/process/child.rs index 52f393dd2..2bdca4931 100644 --- a/src/process/child.rs +++ b/src/process/child.rs @@ -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 @@ -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), diff --git a/src/process/parent.rs b/src/process/parent.rs index ea003a64b..3ff941794 100644 --- a/src/process/parent.rs +++ b/src/process/parent.rs @@ -1,3 +1,4 @@ +use std::io::ErrorKind; use std::io::Read; use super::{MAX_EVENTS, WAIT_FOR_CHILD}; @@ -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),