Skip to content

Commit caeec72

Browse files
author
Hui Zhu
authored
Merge pull request #240 from quanweiZhou/fix-server-accept
server: fix server exit once a accept failed
2 parents 745d2be + 37f4a2a commit caeec72

File tree

3 files changed

+11
-26
lines changed

3 files changed

+11
-26
lines changed

src/sync/server.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,11 @@ impl Server {
358358
.spawn(move || {
359359
loop {
360360
trace!("listening...");
361-
let pipe_connection = match listener.accept(&listener_quit_flag) {
361+
if listener_quit_flag.load(Ordering::SeqCst) {
362+
info!("listener shutdown for quit flag");
363+
break;
364+
}
365+
let pipe_connection = match listener.accept() {
362366
Ok(None) => {
363367
continue;
364368
}
@@ -369,7 +373,7 @@ impl Server {
369373
}
370374
Err(e) => {
371375
error!("listener accept got {:?}", e);
372-
break;
376+
continue;
373377
}
374378
};
375379

src/sync/sys/unix/net.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ use std::os::unix::prelude::AsRawFd;
2121
use nix::Error;
2222

2323
use nix::unistd::*;
24-
use std::sync::{Arc};
25-
use std::sync::atomic::{AtomicBool, Ordering};
2624
use crate::common::{self, client_connect, SOCK_CLOEXEC};
2725
#[cfg(target_os = "macos")]
2826
use crate::common::set_fd_close_exec;
@@ -84,11 +82,7 @@ impl PipeListener {
8482
// - Ok(Some(PipeConnection)) if a new connection is established
8583
// - Ok(None) if spurious wake up with no new connection
8684
// - Err(io::Error) if there is an error and listener loop should be shutdown
87-
pub(crate) fn accept( &self, quit_flag: &Arc<AtomicBool>) -> std::result::Result<Option<PipeConnection>, io::Error> {
88-
if quit_flag.load(Ordering::SeqCst) {
89-
return Err(io::Error::new(io::ErrorKind::Other, "listener shutdown for quit flag"));
90-
}
91-
85+
pub(crate) fn accept(&self) -> std::result::Result<Option<PipeConnection>, io::Error> {
9286
let mut pollers = vec![
9387
libc::pollfd {
9488
fd: self.monitor_fd.0,
@@ -127,10 +121,6 @@ impl PipeListener {
127121
return Ok(None);
128122
}
129123

130-
if quit_flag.load(Ordering::SeqCst) {
131-
return Err(io::Error::new(io::ErrorKind::Other, "listener shutdown for quit flag"));
132-
}
133-
134124
#[cfg(any(target_os = "linux", target_os = "android"))]
135125
let fd = match accept4(self.fd, SockFlag::SOCK_CLOEXEC) {
136126
Ok(fd) => fd,

src/sync/sys/windows/net.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::os::windows::ffi::OsStrExt;
2323
use std::os::windows::fs::OpenOptionsExt;
2424
use std::os::windows::io::{IntoRawHandle};
2525
use std::sync::atomic::{AtomicBool, Ordering};
26-
use std::sync::{Arc};
2726
use std::{io};
2827

2928
use windows_sys::Win32::Foundation::{ CloseHandle, ERROR_IO_PENDING, ERROR_PIPE_CONNECTED, INVALID_HANDLE_VALUE };
@@ -75,14 +74,7 @@ impl PipeListener {
7574
// accept returns:
7675
// - Ok(Some(PipeConnection)) if a new connection is established
7776
// - Err(io::Error) if there is an error and listener loop should be shutdown
78-
pub(crate) fn accept(&self, quit_flag: &Arc<AtomicBool>) -> std::result::Result<Option<PipeConnection>, io::Error> {
79-
if quit_flag.load(Ordering::SeqCst) {
80-
return Err(io::Error::new(
81-
io::ErrorKind::Other,
82-
"listener shutdown for quit flag",
83-
));
84-
}
85-
77+
pub(crate) fn accept(&self) -> std::result::Result<Option<PipeConnection>, io::Error> {
8678
// Create a new pipe instance for every new client
8779
let instance = self.new_instance()?;
8880
let np = match PipeConnection::new(instance) {
@@ -376,6 +368,7 @@ fn handle_windows_error(e: io::Error) -> Error {
376368
#[cfg(test)]
377369
mod test {
378370
use super::*;
371+
use std::sync::Arc;
379372
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;
380373

381374
#[test]
@@ -398,8 +391,7 @@ mod test {
398391

399392
let listener_server = listener.clone();
400393
let thread = std::thread::spawn(move || {
401-
let quit_flag = Arc::new(AtomicBool::new(false));
402-
match listener_server.accept(&quit_flag) {
394+
match listener_server.accept() {
403395
Ok(Some(_)) => {
404396
// pipe is working
405397
}
@@ -422,8 +414,7 @@ mod test {
422414

423415
let listener_server = listener.clone();
424416
let thread = std::thread::spawn(move || {
425-
let quit_flag = Arc::new(AtomicBool::new(false));
426-
match listener_server.accept(&quit_flag) {
417+
match listener_server.accept() {
427418
Ok(_) => {
428419
panic!("should not get pipe on close")
429420
}

0 commit comments

Comments
 (0)