Skip to content

Commit f9262db

Browse files
committed
prevent large shutdown timeout from panicking
closes #298
1 parent 12d3942 commit f9262db

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

actix-server/src/accept.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::time::Duration;
22
use std::{io, thread};
33

44
use actix_rt::{
5-
time::{sleep_until, Instant},
5+
time::{sleep, Instant},
66
System,
77
};
88
use log::{error, info};
@@ -16,14 +16,17 @@ use crate::worker::{Conn, WorkerHandle};
1616
use crate::Token;
1717

1818
struct ServerSocketInfo {
19-
// addr for socket. mainly used for logging.
19+
/// Address of socket. Mainly used for logging.
2020
addr: SocketAddr,
21-
// be ware this is the crate token for identify socket and should not be confused with
22-
// mio::Token
21+
22+
/// Beware this is the crate token for identify socket and should not be confused
23+
/// with `mio::Token`.
2324
token: Token,
25+
2426
lst: MioListener,
25-
// timeout is used to mark the deadline when this socket's listener should be registered again
26-
// after an error.
27+
28+
/// Timeout is used to mark the deadline when this socket's listener should be registered again
29+
/// after an error.
2730
timeout: Option<Instant>,
2831
}
2932

@@ -226,10 +229,9 @@ impl Accept {
226229
Some(WakerInterest::Stop) => {
227230
return self.deregister_all(&mut sockets);
228231
}
229-
// waker queue is drained.
232+
// waker queue is drained
230233
None => {
231-
// Reset the WakerQueue before break so it does not grow
232-
// infinitely.
234+
// Reset the WakerQueue before break so it does not grow infinitely
233235
WakerQueue::reset(&mut guard);
234236
break 'waker;
235237
}
@@ -328,8 +330,8 @@ impl Accept {
328330
}
329331
Err(tmp) => {
330332
// worker lost contact and could be gone. a message is sent to
331-
// `ServerBuilder` future to notify it a new worker should be made.
332-
// after that remove the fault worker.
333+
// `ServerBuilder` future to notify it a new worker should be made
334+
// after that remove the fault worker
333335
self.srv.worker_faulted(self.handles[self.next].idx);
334336
msg = tmp;
335337
self.handles.swap_remove(self.next);
@@ -403,15 +405,15 @@ impl Accept {
403405
error!("Can not deregister server socket {}", err);
404406
}
405407

406-
// sleep after error. write the timeout to socket info as later the poll
407-
// would need it mark which socket and when it's listener should be
408-
// registered.
408+
// sleep after error. write the timeout to socket info as later
409+
// the poll would need it mark which socket and when it's
410+
// listener should be registered
409411
info.timeout = Some(Instant::now() + Duration::from_millis(500));
410412

411413
// after the sleep a Timer interest is sent to Accept Poll
412414
let waker = self.waker.clone();
413415
System::current().arbiter().spawn(async move {
414-
sleep_until(Instant::now() + Duration::from_millis(510)).await;
416+
sleep(Duration::from_millis(510)).await;
415417
waker.wake(WakerInterest::Timer);
416418
});
417419

actix-server/src/builder.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::future::Future;
2-
use std::pin::Pin;
3-
use std::task::{Context, Poll};
4-
use std::time::Duration;
5-
use std::{io, mem};
6-
7-
use actix_rt::net::TcpStream;
8-
use actix_rt::time::{sleep_until, Instant};
9-
use actix_rt::{self as rt, System};
1+
use std::{
2+
future::Future,
3+
io, mem,
4+
pin::Pin,
5+
task::{Context, Poll},
6+
time::Duration,
7+
};
8+
9+
use actix_rt::{self as rt, net::TcpStream, time::sleep, System};
1010
use log::{error, info};
1111
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
1212
use tokio::sync::oneshot;
@@ -122,23 +122,22 @@ impl ServerBuilder {
122122
self
123123
}
124124

125-
/// Stop actix system.
125+
/// Stop Actix system.
126126
pub fn system_exit(mut self) -> Self {
127127
self.exit = true;
128128
self
129129
}
130130

131-
/// Disable signal handling
131+
/// Disable signal handling.
132132
pub fn disable_signals(mut self) -> Self {
133133
self.no_signals = true;
134134
self
135135
}
136136

137137
/// Timeout for graceful workers shutdown in seconds.
138138
///
139-
/// After receiving a stop signal, workers have this much time to finish
140-
/// serving requests. Workers still alive after the timeout are force
141-
/// dropped.
139+
/// After receiving a stop signal, workers have this much time to finish serving requests.
140+
/// Workers still alive after the timeout are force dropped.
142141
///
143142
/// By default shutdown timeout sets to 30 seconds.
144143
pub fn shutdown_timeout(mut self, sec: u64) -> Self {
@@ -147,11 +146,10 @@ impl ServerBuilder {
147146
self
148147
}
149148

150-
/// Execute external configuration as part of the server building
151-
/// process.
149+
/// Execute external configuration as part of the server building process.
152150
///
153-
/// This function is useful for moving parts of configuration to a
154-
/// different module or even library.
151+
/// This function is useful for moving parts of configuration to a different module or
152+
/// even library.
155153
pub fn configure<F>(mut self, f: F) -> io::Result<ServerBuilder>
156154
where
157155
F: Fn(&mut ServiceConfig) -> io::Result<()>,
@@ -268,6 +266,7 @@ impl ServerBuilder {
268266

269267
self.sockets
270268
.push((token, name.as_ref().to_string(), MioListener::from(lst)));
269+
271270
Ok(self)
272271
}
273272

@@ -393,7 +392,7 @@ impl ServerBuilder {
393392
}
394393
if exit {
395394
rt::spawn(async {
396-
sleep_until(Instant::now() + Duration::from_millis(300)).await;
395+
sleep(Duration::from_millis(300)).await;
397396
System::current().stop();
398397
});
399398
}
@@ -402,7 +401,7 @@ impl ServerBuilder {
402401
// we need to stop system if server was spawned
403402
if self.exit {
404403
rt::spawn(async {
405-
sleep_until(Instant::now() + Duration::from_millis(300)).await;
404+
sleep(Duration::from_millis(300)).await;
406405
System::current().stop();
407406
});
408407
}

actix-server/src/worker.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use std::task::{Context, Poll};
66
use std::time::Duration;
77

8-
use actix_rt::time::{sleep_until, Instant, Sleep};
8+
use actix_rt::time::{sleep, Sleep};
99
use actix_rt::{spawn, Arbiter};
1010
use actix_utils::counter::Counter;
1111
use futures_core::future::LocalBoxFuture;
@@ -361,8 +361,8 @@ impl Future for ServerWorker {
361361
if num != 0 {
362362
info!("Graceful worker shutdown, {} connections", num);
363363
self.state = WorkerState::Shutdown(
364-
Box::pin(sleep_until(Instant::now() + Duration::from_secs(1))),
365-
Box::pin(sleep_until(Instant::now() + self.config.shutdown_timeout)),
364+
Box::pin(sleep(Duration::from_secs(1))),
365+
Box::pin(sleep(self.config.shutdown_timeout)),
366366
Some(result),
367367
);
368368
} else {
@@ -438,7 +438,7 @@ impl Future for ServerWorker {
438438

439439
// sleep for 1 second and then check again
440440
if t1.as_mut().poll(cx).is_ready() {
441-
*t1 = Box::pin(sleep_until(Instant::now() + Duration::from_secs(1)));
441+
*t1 = Box::pin(sleep(Duration::from_secs(1)));
442442
let _ = t1.as_mut().poll(cx);
443443
}
444444

0 commit comments

Comments
 (0)