Skip to content

Commit

Permalink
block and wait for accept thread to exit. (#443)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob Ede <robjtede@icloud.com>
  • Loading branch information
fakeshadow and robjtede authored Mar 2, 2022
1 parent 2a54065 commit 7804ed1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions actix-server/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changes

## Unreleased - 2021-xx-xx
- Wait for accept thread to stop before sending completion signal. [#443]

[#443]: https://github.com/actix/actix-net/pull/443


## 2.0.0 - 2022-01-19
Expand Down
6 changes: 3 additions & 3 deletions actix-server/src/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Accept {
pub(crate) fn start(
sockets: Vec<(usize, MioListener)>,
builder: &ServerBuilder,
) -> io::Result<(WakerQueue, Vec<WorkerHandleServer>)> {
) -> io::Result<(WakerQueue, Vec<WorkerHandleServer>, thread::JoinHandle<()>)> {
let handle_server = ServerHandle::new(builder.cmd_tx.clone());

// construct poll instance and its waker
Expand Down Expand Up @@ -73,12 +73,12 @@ impl Accept {
handle_server,
)?;

thread::Builder::new()
let accept_handle = thread::Builder::new()
.name("actix-server acceptor".to_owned())
.spawn(move || accept.poll_with(&mut sockets))
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;

Ok((waker_queue, handles_server))
Ok((waker_queue, handles_server, accept_handle))
}

fn new_with_sockets(
Expand Down
15 changes: 13 additions & 2 deletions actix-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
io, mem,
pin::Pin,
task::{Context, Poll},
thread,
time::Duration,
};

Expand Down Expand Up @@ -158,6 +159,7 @@ impl Future for Server {

pub struct ServerInner {
worker_handles: Vec<WorkerHandleServer>,
accept_handle: Option<thread::JoinHandle<()>>,
worker_config: ServerWorkerConfig,
services: Vec<Box<dyn InternalServiceFactory>>,
waker_queue: WakerQueue,
Expand Down Expand Up @@ -205,7 +207,7 @@ impl ServerInner {
);
}

let (waker_queue, worker_handles) = Accept::start(sockets, &builder)?;
let (waker_queue, worker_handles, accept_handle) = Accept::start(sockets, &builder)?;

let mux = ServerEventMultiplexer {
signal_fut: (builder.listen_os_signals).then(Signals::new),
Expand All @@ -214,6 +216,7 @@ impl ServerInner {

let server = ServerInner {
waker_queue,
accept_handle: Some(accept_handle),
worker_handles,
worker_config: builder.worker_config,
services: builder.factories,
Expand Down Expand Up @@ -243,7 +246,8 @@ impl ServerInner {
} => {
self.stopping = true;

// stop accept thread
// Signal accept thread to stop.
// Signal is non-blocking; we wait for thread to stop later.
self.waker_queue.wake(WakerInterest::Stop);

// send stop signal to workers
Expand All @@ -258,6 +262,13 @@ impl ServerInner {
let _ = join_all(workers_stop).await;
}

// wait for accept thread stop
self.accept_handle
.take()
.unwrap()
.join()
.expect("Accept thread must not panic in any case");

if let Some(tx) = completion {
let _ = tx.send(());
}
Expand Down

0 comments on commit 7804ed1

Please sign in to comment.