Skip to content

Commit

Permalink
server: fix server exit once a accept failed
Browse files Browse the repository at this point in the history
If the Accept error occurs, an error can be output to ensure that the
subsequent connect can be accepted normally.

Fixes: #239
Signed-off-by: Quanwei Zhou <[email protected]>
  • Loading branch information
quanweiZhou authored and quanwei.zqw committed Sep 23, 2024
1 parent 152ac12 commit f576eb3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
8 changes: 6 additions & 2 deletions src/sync/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,11 @@ impl Server {
.spawn(move || {
loop {
trace!("listening...");
let pipe_connection = match listener.accept(&listener_quit_flag) {
if listener_quit_flag.load(Ordering::SeqCst) {
info!("listener shutdown for quit flag");
break;
}
let pipe_connection = match listener.accept() {
Ok(None) => {
continue;
}
Expand All @@ -369,7 +373,7 @@ impl Server {
}
Err(e) => {
error!("listener accept got {:?}", e);
break;
continue;
}
};

Expand Down
14 changes: 2 additions & 12 deletions src/sync/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use std::os::unix::prelude::AsRawFd;
use nix::Error;

use nix::unistd::*;
use std::sync::{Arc};
use std::sync::atomic::{AtomicBool, Ordering};
use crate::common::{self, client_connect, SOCK_CLOEXEC};
#[cfg(target_os = "macos")]
use crate::common::set_fd_close_exec;
Expand Down Expand Up @@ -84,11 +82,7 @@ impl PipeListener {
// - Ok(Some(PipeConnection)) if a new connection is established
// - Ok(None) if spurious wake up with no new connection
// - Err(io::Error) if there is an error and listener loop should be shutdown
pub(crate) fn accept( &self, quit_flag: &Arc<AtomicBool>) -> std::result::Result<Option<PipeConnection>, io::Error> {
if quit_flag.load(Ordering::SeqCst) {
return Err(io::Error::new(io::ErrorKind::Other, "listener shutdown for quit flag"));
}

pub(crate) fn accept(&self) -> std::result::Result<Option<PipeConnection>, io::Error> {
let mut pollers = vec![
libc::pollfd {
fd: self.monitor_fd.0,
Expand Down Expand Up @@ -120,17 +114,13 @@ impl PipeListener {
error!("fatal error in listener_loop:{:?}", err);
return Err(err);
} else if returned < 1 {
return Ok(None)
return Ok(None);
}

if pollers[0].revents != 0 || pollers[pollers.len() - 1].revents == 0 {
return Ok(None);
}

if quit_flag.load(Ordering::SeqCst) {
return Err(io::Error::new(io::ErrorKind::Other, "listener shutdown for quit flag"));
}

#[cfg(any(target_os = "linux", target_os = "android"))]
let fd = match accept4(self.fd, SockFlag::SOCK_CLOEXEC) {
Ok(fd) => fd,
Expand Down
9 changes: 1 addition & 8 deletions src/sync/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ impl PipeListener {
// accept returns:
// - Ok(Some(PipeConnection)) if a new connection is established
// - Err(io::Error) if there is an error and listener loop should be shutdown
pub(crate) fn accept(&self, quit_flag: &Arc<AtomicBool>) -> std::result::Result<Option<PipeConnection>, io::Error> {
if quit_flag.load(Ordering::SeqCst) {
return Err(io::Error::new(
io::ErrorKind::Other,
"listener shutdown for quit flag",
));
}

pub(crate) fn accept(&self) -> std::result::Result<Option<PipeConnection>, io::Error> {
// Create a new pipe instance for every new client
let instance = self.new_instance()?;
let np = match PipeConnection::new(instance) {
Expand Down

0 comments on commit f576eb3

Please sign in to comment.