diff --git a/Cargo.lock b/Cargo.lock index fd503168..5b279869 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2889,13 +2889,12 @@ checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" [[package]] name = "loom" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" +checksum = "f1611ddbf89dc6ae42cfbd51fc207836140130257646eab8d6d2fb16577db2d2" dependencies = [ "cfg-if", "generator", - "pin-utils", "scoped-tls", "serde", "serde_json", @@ -3525,12 +3524,6 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "pkcs1" version = "0.7.5" diff --git a/tako-rs-core/src/graphql/subscription.rs b/tako-rs-core/src/graphql/subscription.rs index 351ffc2c..00d94173 100644 --- a/tako-rs-core/src/graphql/subscription.rs +++ b/tako-rs-core/src/graphql/subscription.rs @@ -142,7 +142,18 @@ where keepalive_timeout, } = self; if let Some(on_upgrade) = request.extensions_mut().remove::() { - ws::spawn_session(on_upgrade, move |socket| { + let lease = match request + .extensions() + .get::() + { + Some(registry) => match registry.register() { + Some(lease) => Some(lease), + None => return ws::rejection(StatusCode::SERVICE_UNAVAILABLE), + }, + None => None, + }; + + ws::spawn_session(on_upgrade, lease, move |socket| { GraphQLWebSocket::new(socket, executor, protocol) .with_data(data) .on_connection_init(on_connection_init) diff --git a/tako-rs-core/src/server_support.rs b/tako-rs-core/src/server_support.rs index 6973df0d..fb51d9c3 100644 --- a/tako-rs-core/src/server_support.rs +++ b/tako-rs-core/src/server_support.rs @@ -8,6 +8,7 @@ mod http1_io; mod lease; mod shutdown; mod timer; +mod upgrades; use std::future::Future; use std::pin::Pin; @@ -20,10 +21,15 @@ pub use driver::ConnDriver; use futures_util::future::Either; pub use headers::sanitize_response; pub use http::http_service; +pub use http::http_service_with_upgrades; pub use http1_io::Http1Io; pub use lease::Leased; pub use shutdown::ShutdownSignal; pub use timer::ConnectionTimer; +pub use upgrades::UpgradeRegistry; +#[cfg(feature = "ws")] +pub(crate) use upgrades::UpgradeSession; +pub use upgrades::UpgradeSupervisor; use crate::router::Router; diff --git a/tako-rs-core/src/server_support/conn.rs b/tako-rs-core/src/server_support/conn.rs index a720cd3d..e8172e3f 100644 --- a/tako-rs-core/src/server_support/conn.rs +++ b/tako-rs-core/src/server_support/conn.rs @@ -69,6 +69,7 @@ impl ConnActivity { pub struct ConnCtx { router: Arc, peer: SocketAddr, + upgrades: Option, /// Request activity the header deadline watches; shared with the bodies of /// streamed responses. pub activity: Arc, @@ -79,9 +80,15 @@ impl ConnCtx { Self { router, peer, + upgrades: None, activity: Arc::default(), } } + + pub fn with_upgrades(mut self, upgrades: super::UpgradeRegistry) -> Self { + self.upgrades = Some(upgrades); + self + } } /// Response body that keeps the deadline suspended until Hyper consumes and flushes it. @@ -135,7 +142,10 @@ where hyper::service::service_fn(move |req: hyper::Request| { let ctx = ctx.clone(); ctx.activity.request_started(); - let req = req.map(TakoBody::incoming); + let mut req = req.map(TakoBody::incoming); + if let Some(upgrades) = &ctx.upgrades { + upgrades.attach(&mut req); + } // Boxing the full pipeline keeps this future small: hyper moves it into // place on every request, and the full pipeline's state is large. let pending = match ctx.router.begin(req, Some(ctx.peer)) { diff --git a/tako-rs-core/src/server_support/http.rs b/tako-rs-core/src/server_support/http.rs index 934fedd9..1cfc6ecc 100644 --- a/tako-rs-core/src/server_support/http.rs +++ b/tako-rs-core/src/server_support/http.rs @@ -25,9 +25,39 @@ pub fn http_service( Response = Response, Error = Infallible, Future = impl Future>, +> { + service(router, info, None) +} + +/// HTTP service with supervised WebSocket sessions. +pub fn http_service_with_upgrades( + router: Arc, + info: ConnInfo, + upgrades: super::UpgradeRegistry, +) -> impl Service< + hyper::Request, + Response = Response, + Error = Infallible, + Future = impl Future>, +> { + service(router, info, Some(upgrades)) +} + +fn service( + router: Arc, + info: ConnInfo, + upgrades: Option, +) -> impl Service< + hyper::Request, + Response = Response, + Error = Infallible, + Future = impl Future>, > { let router = connection_router(&router); - hyper::service::service_fn(move |req: hyper::Request| { + hyper::service::service_fn(move |mut req: hyper::Request| { + if let Some(upgrades) = &upgrades { + upgrades.attach(&mut req); + } start_request(Arc::clone(&router), req.map(TakoBody::incoming), &info) }) } diff --git a/tako-rs-core/src/server_support/upgrades.rs b/tako-rs-core/src/server_support/upgrades.rs new file mode 100644 index 00000000..9fb2ff19 --- /dev/null +++ b/tako-rs-core/src/server_support/upgrades.rs @@ -0,0 +1,183 @@ +use std::sync::Arc; + +use futures_util::future::AbortHandle; +use parking_lot::Mutex; +use tokio::sync::Notify; + +#[cfg(all(test, feature = "ws"))] +mod tests; + +/// Owns upgraded sessions and aborts them on drop. +#[derive(Default)] +pub struct UpgradeSupervisor { + registry: UpgradeRegistry, +} + +/// Registers upgraded sessions with their server's shutdown lifecycle. +#[derive(Clone, Default)] +pub struct UpgradeRegistry(Arc); + +#[derive(Default)] +struct Inner { + state: Mutex, + stopping: Notify, + drained: Notify, +} + +#[derive(Default)] +struct State { + stopping: bool, + tasks: Vec>, + #[cfg(feature = "ws")] + free: Vec, + active: usize, +} + +impl UpgradeSupervisor { + pub fn new(registry: UpgradeRegistry) -> Self { + Self { registry } + } + + pub fn registry(&self) -> UpgradeRegistry { + self.registry.clone() + } + + /// Rejects new sessions and asks existing sessions to close gracefully. + pub fn trigger(&self) { + self.registry.0.state.lock().stopping = true; + self.registry.0.stopping.notify_waiters(); + } + + /// Cancels sessions on their next poll; [`Self::join`] waits for socket cleanup. + pub fn abort(&self) { + self.trigger(); + let tasks = self + .registry + .0 + .state + .lock() + .tasks + .iter() + .flatten() + .cloned() + .collect::>(); + + for task in tasks { + task.abort(); + } + } + + pub async fn join(&self) { + self.registry.join().await; + } +} + +impl Drop for UpgradeSupervisor { + fn drop(&mut self) { + self.abort(); + } +} + +impl UpgradeRegistry { + /// Waits until all registered session futures have been dropped. + pub async fn join(&self) { + loop { + let notified = self.0.drained.notified(); + tokio::pin!(notified); + notified.as_mut().enable(); + + if self.0.state.lock().active == 0 { + return; + } + + notified.await; + } + } + + /// Attaches the registry to requests that support HTTP upgrades. + pub fn attach( + &self, + #[cfg_attr(not(feature = "ws"), allow(unused_variables))] request: &mut http::Request, + ) { + #[cfg(feature = "ws")] + if request + .extensions() + .get::() + .is_some() + { + request.extensions_mut().insert(self.clone()); + } + } + + #[cfg(feature = "ws")] + pub(crate) fn register(&self) -> Option { + let mut state = self.0.state.lock(); + if state.stopping { + return None; + } + + let (handle, registration) = AbortHandle::new_pair(); + let slot = if let Some(slot) = state.free.pop() { + state.tasks[slot] = Some(handle); + slot + } else { + state.tasks.push(Some(handle)); + state.tasks.len() - 1 + }; + state.active += 1; + + Some(UpgradeSession { + registry: self.clone(), + slot, + registration: Some(registration), + }) + } + + #[cfg(feature = "ws")] + pub(crate) async fn stopping(&self) { + loop { + let notified = self.0.stopping.notified(); + tokio::pin!(notified); + notified.as_mut().enable(); + + if self.0.state.lock().stopping { + return; + } + + notified.await; + } + } +} + +#[cfg(feature = "ws")] +pub(crate) struct UpgradeSession { + registry: UpgradeRegistry, + slot: usize, + registration: Option, +} + +#[cfg(feature = "ws")] +impl UpgradeSession { + pub(crate) fn registry(&self) -> UpgradeRegistry { + self.registry.clone() + } + + pub(crate) async fn run(mut self, future: impl std::future::Future) { + let registration = self.registration.take().expect("session runs once"); + let _ = futures_util::future::Abortable::new(future, registration).await; + } +} + +#[cfg(feature = "ws")] +impl Drop for UpgradeSession { + fn drop(&mut self) { + let mut state = self.registry.0.state.lock(); + state.tasks[self.slot] = None; + state.free.push(self.slot); + state.active -= 1; + + if state.active == 0 { + self.registry.0.drained.notify_waiters(); + } + } +} diff --git a/tako-rs-core/src/server_support/upgrades/tests.rs b/tako-rs-core/src/server_support/upgrades/tests.rs new file mode 100644 index 00000000..44fa99d8 --- /dev/null +++ b/tako-rs-core/src/server_support/upgrades/tests.rs @@ -0,0 +1,66 @@ +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; + +use futures_util::FutureExt; + +use super::UpgradeSupervisor; + +struct Dropped(Arc); + +impl Drop for Dropped { + fn drop(&mut self) { + self.0.store(true, Ordering::SeqCst); + } +} + +#[tokio::test] +async fn abort_waits_for_session_resources_to_be_dropped() { + let supervisor = UpgradeSupervisor::default(); + let registry = supervisor.registry(); + let lease = registry.register().unwrap(); + let dropped = Arc::new(AtomicBool::new(false)); + let guard = Dropped(dropped.clone()); + let (started, ready) = tokio::sync::oneshot::channel(); + let task = tokio::spawn(lease.run(async move { + let _guard = guard; + started.send(()).unwrap(); + std::future::pending::<()>().await; + })); + ready.await.unwrap(); + + supervisor.trigger(); + assert!(registry.register().is_none()); + assert!(supervisor.join().now_or_never().is_none()); + + supervisor.abort(); + supervisor.join().await; + assert!(dropped.load(Ordering::SeqCst)); + task.await.unwrap(); +} + +#[tokio::test] +async fn dropping_owner_cancels_unpolled_and_running_sessions() { + let supervisor = UpgradeSupervisor::default(); + let registry = supervisor.registry(); + let unpolled = registry.register().unwrap(); + let running = registry.register().unwrap(); + let task = tokio::spawn(running.run(std::future::pending())); + + drop(supervisor); + unpolled.run(std::future::pending()).await; + registry.join().await; + task.await.unwrap(); + assert!(registry.register().is_none()); +} + +#[tokio::test] +async fn a_session_panic_does_not_leave_shutdown_waiting() { + let supervisor = UpgradeSupervisor::default(); + let lease = supervisor.registry().register().unwrap(); + let task = tokio::spawn(lease.run(async { panic!("session failed") })); + + assert!(task.await.unwrap_err().is_panic()); + supervisor.trigger(); + supervisor.join().await; +} diff --git a/tako-rs-core/src/ws/driver.rs b/tako-rs-core/src/ws/driver.rs index 88c0e478..95238e9d 100644 --- a/tako-rs-core/src/ws/driver.rs +++ b/tako-rs-core/src/ws/driver.rs @@ -32,6 +32,7 @@ pub(crate) struct Driver { shared: Shared, handler: Option>>>>, lifetime: Option, + shutdown: Option + Send>>>, close_timer: Option, phase: Phase, } @@ -42,11 +43,17 @@ impl> Driver { shared, handler: Some(Box::pin(AssertUnwindSafe(handler).catch_unwind())), lifetime: max_lifetime.map(sleep), + shutdown: None, close_timer: None, phase: Phase::Running, } } + pub(crate) fn with_shutdown(mut self, registry: crate::server_support::UpgradeRegistry) -> Self { + self.shutdown = Some(Box::pin(async move { registry.stopping().await })); + self + } + fn close(&mut self, code: CloseCode) { self.handler = None; { @@ -64,6 +71,10 @@ impl> Driver { .lifetime .as_mut() .is_some_and(|lifetime| lifetime.as_mut().poll(cx).is_ready()) + || self + .shutdown + .as_mut() + .is_some_and(|shutdown| shutdown.as_mut().poll(cx).is_ready()) } fn poll_handler(&mut self, cx: &mut Context<'_>) -> Poll<()> { @@ -149,3 +160,9 @@ impl> Future for Driver { #[cfg(test)] mod tests; + +impl Drop for Driver { + fn drop(&mut self) { + lock(&self.shared).terminate(); + } +} diff --git a/tako-rs-core/src/ws/socket.rs b/tako-rs-core/src/ws/socket.rs index 3517093e..eb14fe27 100644 --- a/tako-rs-core/src/ws/socket.rs +++ b/tako-rs-core/src/ws/socket.rs @@ -61,7 +61,7 @@ impl Default for Config { } pub(crate) struct Core { - io: Upgraded, + io: Option, read_buf: BytesMut, write_buf: BytesMut, protocol: Protocol, @@ -80,7 +80,7 @@ pub(crate) struct Core { impl Core { pub(crate) fn new(io: Upgraded, config: Config) -> Self { Self { - io, + io: Some(io), read_buf: BytesMut::with_capacity(config.read_buffer_size), write_buf: BytesMut::new(), protocol: Protocol::new(config.limits), @@ -100,6 +100,15 @@ impl Core { self.protocol.close_if_open(code); } + pub(crate) fn terminate(&mut self) { + self.finished = true; + self.read_done = true; + self.io = None; + self.read_buf = BytesMut::new(); + self.write_buf = BytesMut::new(); + self.wake_waiting(); + } + pub(crate) fn wake_waiting(&self) { self.read_wake.wake_all(); self.write_wake.wake_all(); @@ -150,6 +159,10 @@ impl Core { } fn poll_read_more(&mut self, cx: &mut Context<'_>) -> Poll> { + let Some(io) = &mut self.io else { + return Poll::Ready(Ok(0)); + }; + let want = self .protocol .wanted(&self.read_buf) @@ -165,7 +178,7 @@ impl Core { let spare = self.read_buf.spare_capacity_mut(); let len = spare.len().min(want); let mut buf = ReadBuf::uninit(&mut spare[..len]); - ready!(Pin::new(&mut self.io).poll_read(&mut proxy, buf.unfilled()))?; + ready!(Pin::new(io).poll_read(&mut proxy, buf.unfilled()))?; buf.filled().len() }; // SAFETY: the transport initialized `filled` bytes of the spare capacity. @@ -201,6 +214,10 @@ impl Core { } pub(crate) fn poll_write_out(&mut self, cx: &mut Context<'_>) -> Poll> { + let Some(io) = &mut self.io else { + return Poll::Ready(Err(Error::Closed)); + }; + if let Some(kind) = self.write_failed { return Poll::Ready(Err(Error::Io(kind.into()))); } @@ -212,9 +229,9 @@ impl Core { } self.protocol.drain_close(&mut self.write_buf); if self.write_buf.is_empty() { - break ready!(Pin::new(&mut self.io).poll_flush(&mut proxy)); + break ready!(Pin::new(&mut *io).poll_flush(&mut proxy)); } - match ready!(Pin::new(&mut self.io).poll_write(&mut proxy, &self.write_buf)) { + match ready!(Pin::new(&mut *io).poll_write(&mut proxy, &self.write_buf)) { Ok(0) => break Err(io::ErrorKind::WriteZero.into()), Ok(written) => self.write_buf.advance(written), Err(error) => break Err(error), @@ -227,8 +244,12 @@ impl Core { } pub(crate) fn poll_shutdown(&mut self, cx: &mut Context<'_>) -> Poll> { + let Some(io) = &mut self.io else { + return Poll::Ready(Ok(())); + }; + let mut proxy = Context::from_waker(self.write_wake.register(cx)); - ready!(Pin::new(&mut self.io).poll_shutdown(&mut proxy))?; + ready!(Pin::new(io).poll_shutdown(&mut proxy))?; Poll::Ready(Ok(())) } } diff --git a/tako-rs-core/src/ws/upgrade.rs b/tako-rs-core/src/ws/upgrade.rs index d3060fe1..3e9e9d07 100644 --- a/tako-rs-core/src/ws/upgrade.rs +++ b/tako-rs-core/src/ws/upgrade.rs @@ -16,6 +16,8 @@ use super::socket::Config; use super::socket::Core; use crate::body::TakoBody; use crate::responder::Responder; +use crate::server_support::UpgradeRegistry; +use crate::server_support::UpgradeSession; use crate::types::Request; use crate::types::Response; @@ -156,13 +158,26 @@ where let session = request .extensions_mut() .remove::() - .map(|on_upgrade| Session { - on_upgrade, - handler, - config, - upgrade_timeout, - max_lifetime, - }); + .map(|on_upgrade| { + let lease = match request.extensions().get::() { + Some(registry) => Some(registry.register().ok_or(())?), + None => None, + }; + + Ok::<_, ()>(Session { + on_upgrade, + handler, + config, + upgrade_timeout, + max_lifetime, + lease, + }) + }) + .transpose(); + let Ok(session) = session else { + return (handshake::rejection(StatusCode::SERVICE_UNAVAILABLE), None); + }; + (response, session) } } @@ -173,36 +188,88 @@ struct Session { config: Config, upgrade_timeout: Option, max_lifetime: Option, + lease: Option, } impl Session { - async fn run(self) + async fn run(mut self) + where + H: FnOnce(WebSocket) -> Fut, + Fut: Future, + { + let lease = self.lease.take(); + let registry = lease.as_ref().map(UpgradeSession::registry); + let run = self.run_socket(registry); + + match lease { + Some(lease) => lease.run(run).await, + None => run.await, + } + } + + async fn run_socket(self, registry: Option) where H: FnOnce(WebSocket) -> Fut, Fut: Future, { - let upgraded = match self.upgrade_timeout { + let upgraded = { + let stopping = async { + match ®istry { + Some(registry) => registry.stopping().await, + None => std::future::pending().await, + } + }; + let stopping = std::pin::pin!(stopping); + let upgrade = std::pin::pin!(Self::upgrade(self.on_upgrade, self.upgrade_timeout)); + + match futures_util::future::select(upgrade, stopping).await { + Either::Left((Some(upgraded), _)) => upgraded, + _ => return, + } + }; + + let core = runtime::shared(Core::new(upgraded, self.config)); + let socket = WebSocket::new(core.clone()); + let mut driver = Driver::new( + core, + async move { (self.handler)(socket).await }, + self.max_lifetime, + ); + if let Some(registry) = registry { + driver = driver.with_shutdown(registry); + } + + driver.await; + } + + async fn upgrade( + mut on_upgrade: OnUpgrade, + timeout: Option, + ) -> Option { + let upgraded = match timeout { Some(limit) => { - match futures_util::future::select(self.on_upgrade, runtime::sleep(limit)).await { + match futures_util::future::select(&mut on_upgrade, runtime::sleep(limit)).await { Either::Left((Ok(upgraded), _)) => upgraded, - _ => return, + _ => return None, } } - None => match self.on_upgrade.await { + None => match on_upgrade.await { Ok(upgraded) => upgraded, - Err(_) => return, + Err(_) => return None, }, }; - let core = runtime::shared(Core::new(upgraded, self.config)); - let socket = WebSocket::new(core.clone()); - Driver::new(core, (self.handler)(socket), self.max_lifetime).await; + + Some(upgraded) } } /// Runs `handler` on the connection once the server hands it over, with default settings. #[cfg(all(feature = "async-graphql", not(feature = "compio")))] -pub(crate) fn spawn_session(on_upgrade: OnUpgrade, handler: H) -where +pub(crate) fn spawn_session( + on_upgrade: OnUpgrade, + lease: Option, + handler: H, +) where H: FnOnce(WebSocket) -> Fut + Send + 'static, Fut: Future + Send + 'static, { @@ -212,6 +279,7 @@ where config: Config::default(), upgrade_timeout: None, max_lifetime: None, + lease, }; runtime::spawn(session.run()); } diff --git a/tako-rs-core/src/ws/upgrade/tests.rs b/tako-rs-core/src/ws/upgrade/tests.rs index 8ccfd0e0..fe953539 100644 --- a/tako-rs-core/src/ws/upgrade/tests.rs +++ b/tako-rs-core/src/ws/upgrade/tests.rs @@ -44,6 +44,21 @@ fn unrepresentable_limits_are_rejected_before_upgrading() { async fn handler(_: crate::ws::WebSocket) {} +#[test] +fn an_upgrade_requested_before_shutdown_is_rejected_if_it_finishes_during_drain() { + let supervisor = crate::server_support::UpgradeSupervisor::default(); + let mut request = request(); + let upgrade = hyper::upgrade::on(&mut request); + request.extensions_mut().insert(upgrade); + supervisor.registry().attach(&mut request); + let pending = WebSocketUpgrade::new(request, handler); + + supervisor.trigger(); + let (response, session) = pending.accept(); + assert_eq!(response.status(), http::StatusCode::SERVICE_UNAVAILABLE); + assert!(session.is_none()); +} + #[test] fn a_protocol_from_a_later_header_is_returned_in_the_upgrade() { let mut request = request(); diff --git a/tako-rs-server-pt/src/config.rs b/tako-rs-server-pt/src/config.rs index 72fab6a7..877df218 100644 --- a/tako-rs-server-pt/src/config.rs +++ b/tako-rs-server-pt/src/config.rs @@ -1,4 +1,5 @@ use std::time::Duration; +use std::time::Instant; /// Configuration for [`serve_per_thread`](crate::serve_per_thread) and /// [`spawn_per_thread`](crate::spawn_per_thread). @@ -9,7 +10,7 @@ pub struct PerThreadConfig { /// Pins worker N to core N (`affinity` feature). Off by default, since a pinned worker cannot /// leave a core another pinning process keeps busy; use it when the server owns the machine. pub pin_to_core: bool, - /// `SO_REUSEPORT` listen backlog. + /// Positive `SO_REUSEPORT` listen backlog. pub backlog: i32, /// How long each worker waits for in-flight connections after shutdown before dropping them. pub drain_timeout: Duration, @@ -24,6 +25,42 @@ pub struct PerThreadConfig { pub balance_connections: bool, } +impl PerThreadConfig { + /// Rejects invalid counts and deadlines. Zero timeouts expire immediately and are valid. + pub fn validate(&self) -> std::io::Result<()> { + let invalid = |message| std::io::Error::new(std::io::ErrorKind::InvalidInput, message); + + if self.workers == 0 { + return Err(invalid("workers must be positive")); + } + + if self.backlog <= 0 { + return Err(invalid("backlog must be positive")); + } + + if let Some(limit) = self.max_connections + && (limit == 0 || limit > tokio::sync::Semaphore::MAX_PERMITS) + { + return Err(invalid( + "max_connections must be between 1 and Semaphore::MAX_PERMITS", + )); + } + + let now = Instant::now(); + if now.checked_add(self.drain_timeout).is_none() + || self + .header_read_timeout + .is_some_and(|timeout| now.checked_add(timeout).is_none()) + { + return Err(invalid( + "timeouts must fit within the monotonic clock's range", + )); + } + + Ok(()) + } +} + impl Default for PerThreadConfig { fn default() -> Self { Self { diff --git a/tako-rs-server-pt/src/error.rs b/tako-rs-server-pt/src/error.rs new file mode 100644 index 00000000..c2609568 --- /dev/null +++ b/tako-rs-server-pt/src/error.rs @@ -0,0 +1,79 @@ +use std::any::Any; +use std::fmt; +use std::io; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[non_exhaustive] +pub enum PerThreadErrorKind { + /// No listener could bind. Individual bind failures may be tolerated. + Bind, + /// Runtime initialization or plugin setup failed. + Startup, + Io, + Panic, + /// Returned without a shutdown request. + UnexpectedExit, +} + +/// Worker failure reported by [`PerThreadHandle`](crate::PerThreadHandle). +#[derive(Clone, Debug)] +pub struct PerThreadError { + worker_id: usize, + kind: PerThreadErrorKind, + source: std::sync::Arc, +} + +impl PerThreadError { + /// The zero-based worker index. + pub fn worker_id(&self) -> usize { + self.worker_id + } + + pub fn kind(&self) -> PerThreadErrorKind { + self.kind + } + + pub(crate) fn new(worker_id: usize, kind: PerThreadErrorKind, error: io::Error) -> Self { + Self { + worker_id, + kind, + source: std::sync::Arc::new(error), + } + } + + pub(crate) fn panic(worker_id: usize, payload: &(dyn Any + Send)) -> Self { + let message = payload + .downcast_ref::() + .map(String::as_str) + .or_else(|| payload.downcast_ref::<&str>().copied()) + .unwrap_or("non-string panic payload"); + + Self::new( + worker_id, + PerThreadErrorKind::Panic, + io::Error::other(message.to_owned()), + ) + } +} + +impl fmt::Display for PerThreadError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + formatter, + "per-thread worker {} ({:?}): {}", + self.worker_id, self.kind, self.source + ) + } +} + +impl std::error::Error for PerThreadError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(self.source.as_ref()) + } +} + +impl From for io::Error { + fn from(error: PerThreadError) -> Self { + Self::new(error.source.kind(), error) + } +} diff --git a/tako-rs-server-pt/src/handle.rs b/tako-rs-server-pt/src/handle.rs new file mode 100644 index 00000000..5c45fb09 --- /dev/null +++ b/tako-rs-server-pt/src/handle.rs @@ -0,0 +1,68 @@ +use std::io; + +use crate::PerThreadError; +use crate::PerThreadReadiness; +use crate::PerThreadShutdown; + +/// Owns worker threads; dropping it requests shutdown without blocking. +pub struct PerThreadHandle { + workers: Vec>, + shutdown: PerThreadShutdown, +} + +impl PerThreadHandle { + pub(crate) fn new( + workers: Vec>, + shutdown: PerThreadShutdown, + ) -> Self { + Self { workers, shutdown } + } + + /// Remains usable after the server handle is dropped. + pub fn readiness(&self) -> PerThreadReadiness { + self.shutdown.readiness.clone() + } + + pub fn shutdown_handle(&self) -> PerThreadShutdown { + self.shutdown.clone() + } + + /// Stops accepting and starts the configured drain period. + pub fn shutdown(&self) { + self.shutdown.trigger(); + } + + pub async fn wait_ready(&self) -> io::Result<()> { + self.shutdown.readiness.wait_ready().await + } + + /// Resolves at the first terminal failure, or after every worker stops cleanly. + /// Use [`Self::join`] to wait for peer cleanup after a failure. + pub async fn result(&self) -> Result<(), PerThreadError> { + self.shutdown.readiness.result().await + } + + /// Blocks until worker threads exit and returns the retained terminal result. + /// Call `shutdown()` first when the server is still running. + pub fn join(mut self) -> Result<(), PerThreadError> { + let mut join_error = None; + for (worker_id, worker) in std::mem::take(&mut self.workers).into_iter().enumerate() { + if let Err(payload) = worker.join() { + join_error.get_or_insert_with(|| PerThreadError::panic(worker_id, payload.as_ref())); + } + } + + self + .shutdown + .readiness + .failure() + .or(join_error) + .map_or(Ok(()), Err) + } +} + +impl Drop for PerThreadHandle { + fn drop(&mut self) { + self.shutdown.trigger(); + } +} diff --git a/tako-rs-server-pt/src/lib.rs b/tako-rs-server-pt/src/lib.rs index c15261b5..7cff6038 100644 --- a/tako-rs-server-pt/src/lib.rs +++ b/tako-rs-server-pt/src/lib.rs @@ -18,17 +18,26 @@ //! serve_per_thread("0.0.0.0:8080", router, config) //! } //! ``` +//! +//! [`spawn_per_thread_supervised`] returns a [`PerThreadHandle`] for readiness, shutdown, +//! and worker failure reporting. #[cfg(not(feature = "compio"))] mod balance; mod config; +mod error; +mod handle; mod listener; +mod readiness; mod shutdown; #[cfg(not(feature = "compio"))] mod worker; #[cfg(feature = "compio")] mod worker_compio; +#[cfg(test)] +mod supervision_tests; + use std::io; use std::net::SocketAddr; use std::str::FromStr; @@ -36,20 +45,24 @@ use std::str::FromStr; use tako_rs_core::router::Router; pub use crate::config::PerThreadConfig; +pub use crate::error::PerThreadError; +pub use crate::error::PerThreadErrorKind; +pub use crate::handle::PerThreadHandle; +pub use crate::readiness::PerThreadReadiness; +pub use crate::readiness::PerThreadWorkerState; pub use crate::shutdown::PerThreadShutdown; #[cfg(not(feature = "compio"))] use crate::worker::worker_main; #[cfg(feature = "compio")] use crate::worker_compio::worker_main_compio; -/// Runs workers until Ctrl+C or SIGTERM, then drains requests; fails if no worker could bind. +/// Runs until Ctrl+C, SIGTERM or worker failure, then drains and reports the terminal result. pub fn serve_per_thread(addr: &str, router: Router, cfg: PerThreadConfig) -> io::Result<()> { - let workers = cfg.workers; - let (handles, shutdown) = spawn_per_thread(addr, router, cfg)?; - wait_for_shutdown(handles, shutdown, workers) + wait_for_shutdown(spawn_per_thread_supervised(addr, router, cfg)?) } /// Starts workers and returns their thread handles and the trigger that stops them. +/// Worker failures cancel peers; use [`spawn_per_thread_supervised`] to observe the failure. pub fn spawn_per_thread( addr: &str, router: Router, @@ -65,6 +78,17 @@ pub fn spawn_per_thread( } } +/// Starts supervised workers. Validation and thread creation errors return directly; +/// worker failures reach the handle and stop peers. +pub fn spawn_per_thread_supervised( + addr: &str, + router: Router, + cfg: PerThreadConfig, +) -> io::Result { + let (workers, shutdown) = spawn_per_thread(addr, router, cfg)?; + Ok(PerThreadHandle::new(workers, shutdown)) +} + #[cfg(not(feature = "compio"))] type WorkerFn = fn( usize, @@ -73,9 +97,15 @@ type WorkerFn = fn( PerThreadConfig, PerThreadShutdown, Option, -); +) -> Result<(), PerThreadError>; #[cfg(feature = "compio")] -type WorkerFn = fn(usize, SocketAddr, std::sync::Arc, PerThreadConfig, PerThreadShutdown); +type WorkerFn = fn( + usize, + SocketAddr, + std::sync::Arc, + PerThreadConfig, + PerThreadShutdown, +) -> Result<(), PerThreadError>; fn spawn_workers( addr: &str, @@ -83,24 +113,22 @@ fn spawn_workers( cfg: PerThreadConfig, worker: WorkerFn, ) -> io::Result<(Vec>, PerThreadShutdown)> { - if cfg.workers == 0 || cfg.max_connections == Some(0) { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "worker and connection counts must be positive", - )); - } + cfg.validate()?; + let addr = SocketAddr::from_str(addr).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; let router = std::sync::Arc::new(router); if !cfg.balance_connections || cfg!(feature = "compio") { listener::warn_reuseport_platform_once(); } - let shutdown = PerThreadShutdown::new(); + + let shutdown = PerThreadShutdown::new(cfg.workers); #[cfg(not(feature = "compio"))] let mut balances = cfg .balance_connections .then(|| balance::Balancer::for_workers(cfg.workers).into_iter()); let mut handles = Vec::with_capacity(cfg.workers); + for id in 0..cfg.workers { let cfg = cfg.clone(); let router = router.clone(); @@ -108,11 +136,26 @@ fn spawn_workers( #[cfg(not(feature = "compio"))] let balance = balances.as_mut().and_then(Iterator::next); let run = move || { - #[cfg(not(feature = "compio"))] - worker(id, addr, router, cfg, worker_shutdown, balance); - #[cfg(feature = "compio")] - worker(id, addr, router, cfg, worker_shutdown); + let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + #[cfg(not(feature = "compio"))] + { + worker(id, addr, router, cfg, worker_shutdown.clone(), balance) + } + #[cfg(feature = "compio")] + { + worker(id, addr, router, cfg, worker_shutdown.clone()) + } + })); + + match outcome { + Ok(result) => worker_shutdown.report_exit(id, result), + Err(payload) => { + worker_shutdown.report_exit(id, Err(PerThreadError::panic(id, payload.as_ref()))); + std::panic::resume_unwind(payload); + } + } }; + match std::thread::Builder::new() .name(format!("tako-pt-{id}")) .spawn(run) @@ -123,30 +166,28 @@ fn spawn_workers( for handle in handles { let _ = handle.join(); } + return Err(error); } } } + Ok((handles, shutdown)) } -fn wait_for_shutdown( - handles: Vec>, - shutdown: PerThreadShutdown, - workers: usize, -) -> io::Result<()> { +fn wait_for_shutdown(handle: PerThreadHandle) -> io::Result<()> { let result = tokio::runtime::Builder::new_current_thread() .enable_all() .build() .and_then(|rt| { rt.block_on(async { - shutdown.wait_for_bind_outcome(workers).await?; - tako_rs_core::server_support::shutdown_signal().await + tokio::select! { + result = handle.result() => result.map_err(io::Error::from), + signal = tako_rs_core::server_support::shutdown_signal() => signal, + } }) }); - shutdown.trigger(); - for handle in handles { - let _ = handle.join(); - } - result + handle.shutdown(); + let joined = handle.join().map_err(io::Error::from); + result.and(joined) } diff --git a/tako-rs-server-pt/src/readiness.rs b/tako-rs-server-pt/src/readiness.rs new file mode 100644 index 00000000..4a21090e --- /dev/null +++ b/tako-rs-server-pt/src/readiness.rs @@ -0,0 +1,222 @@ +use std::io; +use std::sync::Arc; +use std::sync::Mutex; + +use crate::PerThreadError; +use crate::PerThreadErrorKind; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[non_exhaustive] +pub enum PerThreadWorkerState { + Starting, + Listening, + /// Receives handoffs from other Tokio workers after its own bind failed. + HandoffOnly, + Stopped, + /// Failed, but runtime cleanup may still be running. + Failed, +} + +struct State { + workers: Vec, + remaining: usize, + stopping: bool, + failure: Option, + bind_failure: Option, +} + +impl State { + fn record_failure(&mut self, worker_id: usize, error: PerThreadError) { + self.workers[worker_id] = PerThreadWorkerState::Failed; + if error.kind() == PerThreadErrorKind::Bind { + self.bind_failure.get_or_insert(error); + } else { + self.failure.get_or_insert(error); + } + + self.check_bind_failure(); + } + + fn is_ready(&self) -> bool { + !self.stopping + && self.failure.is_none() + && self.workers.contains(&PerThreadWorkerState::Listening) + } + + fn check_bind_failure(&mut self) { + if !self.workers.iter().any(|state| { + matches!( + state, + PerThreadWorkerState::Starting | PerThreadWorkerState::Listening + ) + }) && self.failure.is_none() + && !self.stopping + { + self.failure = self.bind_failure.clone(); + } + } +} + +struct Shared { + state: Mutex, + notify: tokio::sync::Notify, +} + +/// Shared readiness for health probes. Requires a listener and clears on shutdown or +/// terminal failure; a bind failure is tolerated while another worker listens. +#[derive(Clone)] +pub struct PerThreadReadiness { + shared: Arc, +} + +impl PerThreadReadiness { + pub(crate) fn new(workers: usize) -> Self { + Self { + shared: Arc::new(Shared { + state: Mutex::new(State { + workers: vec![PerThreadWorkerState::Starting; workers], + remaining: workers, + stopping: false, + failure: None, + bind_failure: None, + }), + notify: tokio::sync::Notify::new(), + }), + } + } + + fn state(&self) -> std::sync::MutexGuard<'_, State> { + self + .shared + .state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } + + pub fn is_ready(&self) -> bool { + self.state().is_ready() + } + + /// States indexed by worker ID. + pub fn workers(&self) -> Vec { + self.state().workers.clone() + } + + /// The first terminal failure, retained for all observers. + pub fn failure(&self) -> Option { + self.state().failure.clone() + } + + /// Waits for a listener, failing on worker failure or shutdown before readiness. + pub async fn wait_ready(&self) -> io::Result<()> { + loop { + let notified = self.shared.notify.notified(); + tokio::pin!(notified); + notified.as_mut().enable(); + + { + let state = self.state(); + if let Some(error) = &state.failure { + return Err(error.clone().into()); + } + + if state.is_ready() { + return Ok(()); + } + + if state.stopping || state.remaining == 0 { + return Err(io::Error::new( + io::ErrorKind::Interrupted, + "per-thread server stopped", + )); + } + } + + notified.await; + } + } + + pub(crate) async fn result(&self) -> Result<(), PerThreadError> { + loop { + let notified = self.shared.notify.notified(); + tokio::pin!(notified); + notified.as_mut().enable(); + + { + let state = self.state(); + if let Some(error) = &state.failure { + return Err(error.clone()); + } + + if state.remaining == 0 { + return Ok(()); + } + } + + notified.await; + } + } + + pub(crate) fn stop(&self) { + self.state().stopping = true; + self.shared.notify.notify_waiters(); + } + + pub(crate) fn listening(&self, worker_id: usize) { + self.state().workers[worker_id] = PerThreadWorkerState::Listening; + self.shared.notify.notify_waiters(); + } + + #[cfg(not(feature = "compio"))] + pub(crate) fn handoff_only(&self, worker_id: usize, error: io::Error) -> bool { + let failed = { + let mut state = self.state(); + state.workers[worker_id] = PerThreadWorkerState::HandoffOnly; + state + .bind_failure + .get_or_insert_with(|| PerThreadError::new(worker_id, PerThreadErrorKind::Bind, error)); + state.check_bind_failure(); + state.failure.is_some() + }; + + self.shared.notify.notify_waiters(); + failed + } + + pub(crate) fn finished(&self, worker_id: usize, outcome: Result<(), PerThreadError>) -> bool { + let failed = { + let mut state = self.state(); + let outcome = if outcome.is_ok() && !state.stopping { + Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::UnexpectedExit, + io::Error::other("worker exited without a shutdown request"), + )) + } else { + outcome + }; + + state.remaining -= 1; + match outcome { + Ok(()) => state.workers[worker_id] = PerThreadWorkerState::Stopped, + Err(error) => state.record_failure(worker_id, error), + } + state.check_bind_failure(); + state.failure.is_some() + }; + + self.shared.notify.notify_waiters(); + failed + } + + pub(crate) fn failed(&self, worker_id: usize, error: PerThreadError) -> bool { + let failed = { + let mut state = self.state(); + state.record_failure(worker_id, error); + state.failure.is_some() + }; + + self.shared.notify.notify_waiters(); + failed + } +} diff --git a/tako-rs-server-pt/src/shutdown.rs b/tako-rs-server-pt/src/shutdown.rs index c7014035..a08fbd8f 100644 --- a/tako-rs-server-pt/src/shutdown.rs +++ b/tako-rs-server-pt/src/shutdown.rs @@ -1,33 +1,27 @@ use std::io; -/// Worker bind results, so the parent can fail when every worker failed to bind instead of -/// waiting for a signal with no listener up. -#[derive(Default)] -struct BindStatus { - succeeded: std::sync::atomic::AtomicUsize, - failed: std::sync::atomic::AtomicUsize, - first_err: std::sync::Mutex>, - notify: tokio::sync::Notify, -} +use crate::PerThreadError; +use crate::PerThreadErrorKind; +use crate::PerThreadReadiness; -/// Stops the workers of [`spawn_per_thread`](crate::spawn_per_thread); the signal is sticky, so -/// a worker that starts waiting after [`trigger`](Self::trigger) still sees it. +/// Sticky shutdown signal: workers that start waiting later still receive it. #[derive(Clone)] pub struct PerThreadShutdown { pub(crate) inner: tokio_util::sync::CancellationToken, - bind_status: std::sync::Arc, + pub(crate) readiness: PerThreadReadiness, } impl PerThreadShutdown { - pub(crate) fn new() -> Self { + pub(crate) fn new(workers: usize) -> Self { Self { inner: tokio_util::sync::CancellationToken::new(), - bind_status: std::sync::Arc::default(), + readiness: PerThreadReadiness::new(workers), } } - /// Tells every worker to stop accepting and drain; later calls do nothing. + /// Clears readiness and starts draining all workers. pub fn trigger(&self) { + self.readiness.stop(); self.inner.cancel(); } @@ -36,60 +30,63 @@ impl PerThreadShutdown { self.inner.cancelled().await; } - pub(crate) fn report_bind_success(&self) { - self - .bind_status - .succeeded - .fetch_add(1, std::sync::atomic::Ordering::SeqCst); - self.bind_status.notify.notify_waiters(); + pub(crate) fn report_bind_success(&self, worker_id: usize) { + self.readiness.listening(worker_id); } - /// Records a worker's bind failure; only the first error is kept for the caller. - pub(crate) fn report_bind_failure(&self, err: io::Error) { - { - let mut guard = self.bind_status.first_err.lock().unwrap(); - if guard.is_none() { - *guard = Some(err); - } + #[cfg(not(feature = "compio"))] + pub(crate) fn report_handoff_only(&self, worker_id: usize, error: io::Error) { + if self.readiness.handoff_only(worker_id, error) { + self.trigger(); } - self - .bind_status - .failed - .fetch_add(1, std::sync::atomic::Ordering::SeqCst); - self.bind_status.notify.notify_waiters(); } - /// Waits until one worker has bound, or returns the first bind error once all `total` failed. - pub async fn wait_for_bind_outcome(&self, total: usize) -> io::Result<()> { - use std::sync::atomic::Ordering; + pub(crate) fn report_exit(&self, worker_id: usize, outcome: Result<(), PerThreadError>) { + if self.readiness.finished(worker_id, outcome) { + self.trigger(); + } + } - loop { - // Arm the notified future BEFORE reading state so a wake fired - // between the load and the await is not lost. - let notified = self.bind_status.notify.notified(); - tokio::pin!(notified); - notified.as_mut().enable(); + /// Publishes failure before the caller drops its runtime and waits for blocking tasks. + /// Final thread completion is accounted for separately by `report_exit`. + pub(crate) fn supervise_runtime( + &self, + worker_id: usize, + run: impl FnOnce() -> Result<(), PerThreadError>, + ) -> Result<(), PerThreadError> { + match std::panic::catch_unwind(std::panic::AssertUnwindSafe(run)) { + Ok(result) => { + let result = if result.is_ok() && !self.inner.is_cancelled() { + Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::UnexpectedExit, + io::Error::other("worker exited without a shutdown request"), + )) + } else { + result + }; - let succ = self.bind_status.succeeded.load(Ordering::SeqCst); - let fail = self.bind_status.failed.load(Ordering::SeqCst); + if let Err(error) = &result + && self.readiness.failed(worker_id, error.clone()) + { + self.trigger(); + } - if succ > 0 { - return Ok(()); + result } - if succ + fail >= total { - let err = self - .bind_status - .first_err - .lock() - .unwrap() - .take() - .unwrap_or_else(|| { - io::Error::other(format!("all {total} per-thread workers failed to bind")) - }); - return Err(err); + Err(payload) => { + self.readiness.failed( + worker_id, + PerThreadError::panic(worker_id, payload.as_ref()), + ); + self.trigger(); + std::panic::resume_unwind(payload); } - - notified.await; } } + + /// Waits for readiness, failure, or shutdown. `total` is ignored; spawning sets the count. + pub async fn wait_for_bind_outcome(&self, _total: usize) -> io::Result<()> { + self.readiness.wait_ready().await + } } diff --git a/tako-rs-server-pt/src/supervision_tests.rs b/tako-rs-server-pt/src/supervision_tests.rs new file mode 100644 index 00000000..cff1ffed --- /dev/null +++ b/tako-rs-server-pt/src/supervision_tests.rs @@ -0,0 +1,305 @@ +use std::time::Duration; + +use tako_rs_core::router::Router; + +use crate::PerThreadConfig; +use crate::PerThreadError; +use crate::PerThreadErrorKind; +use crate::PerThreadHandle; +use crate::PerThreadShutdown; +use crate::PerThreadWorkerState; +use crate::spawn_workers; + +const WORKER_TIMEOUT: Duration = Duration::from_secs(5); + +#[derive(Clone, Default)] +struct FailureControls { + fail: tokio_util::sync::CancellationToken, + peer_ready: tokio_util::sync::CancellationToken, + release_peer: tokio_util::sync::CancellationToken, +} + +fn post_startup_panic(id: usize, router: std::sync::Arc, shutdown: PerThreadShutdown) { + let controls = router.router_state().get::().unwrap(); + shutdown.report_bind_success(id); + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(async { + if id == 0 { + controls.fail.cancelled().await; + panic!("failure after readiness"); + } + + controls.peer_ready.cancel(); + shutdown.notified().await; + controls.release_peer.cancelled().await; + }); +} + +#[tokio::test] +async fn post_startup_panic_clears_readiness_and_reports_before_peers_finish() { + let controls = FailureControls::default(); + let mut router = Router::new(); + router.with_state(controls.clone()); + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + router, + PerThreadConfig { + workers: 2, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |id, _, router, _, shutdown, _| { + post_startup_panic(id, router, shutdown); + Ok(()) + }, + #[cfg(feature = "compio")] + |id, _, router, _, shutdown| { + post_startup_panic(id, router, shutdown); + Ok(()) + }, + ) + .unwrap(); + let handle = PerThreadHandle::new(workers, shutdown); + let readiness = handle.readiness(); + handle.wait_ready().await.unwrap(); + controls.peer_ready.cancelled().await; + assert!(readiness.is_ready()); + + controls.fail.cancel(); + let result = tokio::time::timeout(WORKER_TIMEOUT, handle.result()).await; + controls.release_peer.cancel(); + let error = result + .expect("result must not wait for a draining peer") + .unwrap_err(); + + assert_eq!(error.kind(), PerThreadErrorKind::Panic); + assert_eq!(error.worker_id(), 0); + assert!(!readiness.is_ready()); + assert!(handle.wait_ready().await.is_err()); + assert_eq!( + handle.result().await.unwrap_err().kind(), + PerThreadErrorKind::Panic + ); + assert!(handle.join().is_err()); + assert_eq!( + readiness.workers(), + [PerThreadWorkerState::Failed, PerThreadWorkerState::Stopped] + ); +} + +#[tokio::test] +async fn startup_and_io_failures_reach_every_result_observer() { + for kind in [PerThreadErrorKind::Startup, PerThreadErrorKind::Io] { + let mut router = Router::new(); + router.with_state(kind); + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + router, + PerThreadConfig { + workers: 1, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |id, _, router, _, _, _| injected_error(id, router), + #[cfg(feature = "compio")] + |id, _, router, _, _| injected_error(id, router), + ) + .unwrap(); + let handle = PerThreadHandle::new(workers, shutdown); + let ready = tokio::time::timeout(WORKER_TIMEOUT, handle.wait_ready()).await; + + assert!(ready.unwrap().is_err()); + for _ in 0..2 { + assert_eq!(handle.result().await.unwrap_err().kind(), kind); + } + + assert_eq!(handle.join().unwrap_err().kind(), kind); + } +} + +fn injected_error(id: usize, router: std::sync::Arc) -> Result<(), PerThreadError> { + let kind = *router.router_state().get::().unwrap(); + + Err(PerThreadError::new( + id, + kind, + std::io::Error::other("injected failure"), + )) +} + +#[tokio::test] +async fn unexpected_return_is_a_terminal_failure() { + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + Router::new(), + PerThreadConfig { + workers: 1, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |_, _, _, _, _, _| Ok(()), + #[cfg(feature = "compio")] + |_, _, _, _, _| Ok(()), + ) + .unwrap(); + let handle = PerThreadHandle::new(workers, shutdown); + let result = tokio::time::timeout(WORKER_TIMEOUT, handle.result()).await; + + assert_eq!( + result.unwrap().unwrap_err().kind(), + PerThreadErrorKind::UnexpectedExit + ); + assert!(handle.join().is_err()); +} + +#[test] +fn serving_returns_worker_failure_without_waiting_for_an_os_signal() { + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + Router::new(), + PerThreadConfig { + workers: 1, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |_, _, _, _, _, _| panic!("failed server worker"), + #[cfg(feature = "compio")] + |_, _, _, _, _| panic!("failed server worker"), + ) + .unwrap(); + let (sent, received) = std::sync::mpsc::channel(); + let serving = std::thread::spawn(move || { + sent + .send(crate::wait_for_shutdown(PerThreadHandle::new( + workers, shutdown, + ))) + .unwrap(); + }); + let result = received.recv_timeout(WORKER_TIMEOUT); + + assert!( + result + .expect("serving waited for an OS signal after worker failure") + .is_err() + ); + serving.join().unwrap(); +} + +#[tokio::test] +async fn one_failed_bind_does_not_stop_a_listening_worker() { + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + Router::new(), + PerThreadConfig { + workers: 2, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |id, _, _, _, shutdown, _| bind_or_wait(id, shutdown), + #[cfg(feature = "compio")] + |id, _, _, _, shutdown| bind_or_wait(id, shutdown), + ) + .unwrap(); + let handle = PerThreadHandle::new(workers, shutdown); + let readiness = handle.readiness(); + handle.wait_ready().await.unwrap(); + assert!(readiness.is_ready()); + + handle.shutdown(); + assert!(!readiness.is_ready()); + handle.result().await.unwrap(); + handle.join().unwrap(); + assert_eq!( + readiness.workers(), + [PerThreadWorkerState::Failed, PerThreadWorkerState::Stopped] + ); +} + +fn bind_or_wait(id: usize, shutdown: PerThreadShutdown) -> Result<(), PerThreadError> { + if id == 0 { + return Err(PerThreadError::new( + id, + PerThreadErrorKind::Bind, + std::io::Error::new(std::io::ErrorKind::AddrInUse, "occupied listener"), + )); + } + + shutdown.report_bind_success(id); + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(shutdown.notified()); + Ok(()) +} + +#[tokio::test] +async fn worker_panic_cancels_its_peers() { + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + Router::new(), + PerThreadConfig { + workers: 2, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |id, _, _, _, shutdown, _| { + panic_or_wait(id, shutdown); + Ok(()) + }, + #[cfg(feature = "compio")] + |id, _, _, _, shutdown| { + panic_or_wait(id, shutdown); + Ok(()) + }, + ) + .unwrap(); + let stopped = tokio::time::timeout(WORKER_TIMEOUT, shutdown.notified()).await; + shutdown.trigger(); + for worker in workers { + let _ = worker.join(); + } + + assert!( + stopped.is_ok(), + "worker panic did not stop surviving workers" + ); +} + +fn panic_or_wait(id: usize, shutdown: crate::PerThreadShutdown) { + assert_ne!(id, 0, "worker failure"); + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(shutdown.notified()); +} + +#[tokio::test] +async fn panic_before_bind_wakes_readiness_waiters() { + let (workers, shutdown) = spawn_workers( + "127.0.0.1:0", + Router::new(), + PerThreadConfig { + workers: 1, + ..PerThreadConfig::default() + }, + #[cfg(not(feature = "compio"))] + |_, _, _, _, _, _| panic!("startup panic"), + #[cfg(feature = "compio")] + |_, _, _, _, _| panic!("startup panic"), + ) + .unwrap(); + let ready = tokio::time::timeout(WORKER_TIMEOUT, shutdown.wait_for_bind_outcome(1)).await; + + shutdown.trigger(); + for worker in workers { + assert!( + worker.join().is_err(), + "legacy thread joins must preserve worker panics" + ); + } + + assert!(ready.is_ok(), "worker panic left readiness pending forever"); + assert!(ready.unwrap().is_err()); +} diff --git a/tako-rs-server-pt/src/worker.rs b/tako-rs-server-pt/src/worker.rs index f93afde3..a109006a 100644 --- a/tako-rs-server-pt/src/worker.rs +++ b/tako-rs-server-pt/src/worker.rs @@ -11,6 +11,7 @@ use tako_rs_core::server_support::ConnDriver; use tako_rs_core::server_support::Http1Io; use tako_rs_core::server_support::Leased; use tako_rs_core::server_support::ShutdownSignal; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::tcp_service; use tokio::net::TcpListener; use tokio::net::TcpStream; @@ -18,6 +19,8 @@ use tokio::runtime::Builder; use tokio::sync::mpsc; use tokio::task::LocalSet; +use crate::PerThreadError; +use crate::PerThreadErrorKind; use crate::balance::Handoff; use crate::balance::LiveConnection; use crate::balance::WorkerBalance; @@ -32,7 +35,7 @@ pub(crate) fn worker_main( cfg: PerThreadConfig, shutdown: PerThreadShutdown, balance: Option, -) { +) -> Result<(), PerThreadError> { #[cfg(feature = "affinity")] if cfg.pin_to_core { if let Some(ids) = core_affinity::get_core_ids() { @@ -63,21 +66,27 @@ pub(crate) fn worker_main( Err(e) => { tracing::error!("worker {worker_id}: failed to build runtime: {e}"); - shutdown.report_bind_failure(io::Error::other(format!( - "worker {worker_id}: failed to build runtime: {e}" - ))); - return; + return Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::Startup, + e, + )); } }; + let supervisor = shutdown.clone(); // The accept loop runs as its own task, so the `LocalSet` re-polls only its // join handle, not the whole accept `select!`, each time a connection wakes. let accept_loop = async move { #[cfg(feature = "plugins")] if let Err(error) = router.setup_plugins_once() { - shutdown.report_bind_failure(io::Error::other(error.clone())); - return; + return Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::Startup, + io::Error::other(error.clone()), + )); } + let mut backoff = Duration::from_millis(5); let semaphore = cfg .max_connections @@ -88,18 +97,17 @@ pub(crate) fn worker_main( }; let listener = match bind_reuseport(addr, cfg.backlog) { Ok(listener) => { - shutdown.report_bind_success(); + shutdown.report_bind_success(worker_id); Some(listener) } Err(e) if balancer.is_some() => { tracing::warn!("worker {worker_id}: bind failed, serving handed-over connections: {e}"); - shutdown.report_bind_failure(e); + shutdown.report_handoff_only(worker_id, e); None } Err(e) => { tracing::error!("worker {worker_id}: bind failed: {e}"); - shutdown.report_bind_failure(e); - return; + return Err(PerThreadError::new(worker_id, PerThreadErrorKind::Bind, e)); } }; tracing::debug!("tako-pt worker {worker_id} listening on {addr}"); @@ -118,6 +126,7 @@ pub(crate) fn worker_main( let mut connection_handles = tokio::task::JoinSet::new(); let stopping = Arc::new(ShutdownSignal::default()); + let upgrades = UpgradeSupervisor::default(); loop { let (stream, peer, live) = tokio::select! { @@ -171,7 +180,7 @@ pub(crate) fn worker_main( } else { None }; - let ctx = Rc::new(ConnCtx::new(Arc::clone(&router), peer)); + let ctx = Rc::new(ConnCtx::new(Arc::clone(&router), peer).with_upgrades(upgrades.registry())); let io = Http1Io::new( hyper_util::rt::TokioIo::new(Leased::new(stream, (live, permit))), Arc::clone(&ctx.activity), @@ -207,23 +216,36 @@ pub(crate) fn worker_main( } stopping.trigger(); + upgrades.trigger(); let drain = tokio::time::timeout(cfg.drain_timeout, async { while connection_handles.join_next().await.is_some() {} + upgrades.join().await; }); if drain.await.is_err() { + upgrades.abort(); connection_handles.shutdown().await; + upgrades.join().await; } + #[cfg(feature = "signals")] tako_rs_core::signals::transport::emit_server_stopped(&local_addr, "tcp", false).await; + Ok(()) }; + let local = LocalSet::new(); - local.block_on(&rt, async move { - if let Err(error) = tokio::task::spawn_local(accept_loop).await - && error.is_panic() - { - std::panic::resume_unwind(error.into_panic()); - } - }); + supervisor.supervise_runtime(worker_id, || { + local.block_on(&rt, async move { + match tokio::task::spawn_local(accept_loop).await { + Ok(result) => result, + Err(error) if error.is_panic() => std::panic::resume_unwind(error.into_panic()), + Err(error) => Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::Io, + io::Error::other(error), + )), + } + }) + }) } async fn accept(listener: Option<&TcpListener>) -> io::Result<(TcpStream, SocketAddr)> { diff --git a/tako-rs-server-pt/src/worker_compio.rs b/tako-rs-server-pt/src/worker_compio.rs index bb538eb5..4f9157b8 100644 --- a/tako-rs-server-pt/src/worker_compio.rs +++ b/tako-rs-server-pt/src/worker_compio.rs @@ -1,13 +1,14 @@ -use std::convert::Infallible; +#[cfg(feature = "plugins")] use std::io; use std::net::SocketAddr; use std::time::Duration; use hyper::server::conn::http1; -use hyper::service::service_fn; use tako_rs_core::conn_info::ConnInfo; use tako_rs_core::router::Router; +use crate::PerThreadError; +use crate::PerThreadErrorKind; use crate::config::PerThreadConfig; use crate::listener::bind_reuseport_compio; use crate::shutdown::PerThreadShutdown; @@ -22,15 +23,16 @@ pub(crate) fn worker_main_compio( router: std::sync::Arc, cfg: PerThreadConfig, shutdown: PerThreadShutdown, -) { +) -> Result<(), PerThreadError> { use std::sync::Arc; use cyper_core::HyperStream; use futures_util::FutureExt; use futures_util::StreamExt; use tako_rs_core::server_support::Leased; - use tako_rs_core::server_support::connection_router; + use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::drive_connection; + use tako_rs_core::server_support::http_service_with_upgrades; #[cfg(feature = "affinity")] if cfg.pin_to_core { @@ -62,31 +64,36 @@ pub(crate) fn worker_main_compio( Err(e) => { tracing::error!("worker {worker_id}: failed to build compio runtime: {e}"); - shutdown.report_bind_failure(io::Error::other(format!( - "worker {worker_id}: failed to build compio runtime: {e}" - ))); - return; + return Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::Startup, + e, + )); } }; - rt.block_on(async move { + let supervisor = shutdown.clone(); + let accept_loop = async move { #[cfg(feature = "plugins")] if let Err(error) = router.setup_plugins_once() { - shutdown.report_bind_failure(io::Error::other(error.clone())); - return; + return Err(PerThreadError::new( + worker_id, + PerThreadErrorKind::Startup, + io::Error::other(error.clone()), + )); } + let semaphore = cfg .max_connections .map(|n| Arc::new(tokio::sync::Semaphore::new(n))); let listener = match bind_reuseport_compio(addr, cfg.backlog) { Ok(l) => { - shutdown.report_bind_success(); + shutdown.report_bind_success(worker_id); l } Err(e) => { tracing::error!("worker {worker_id}: bind failed: {e}"); - shutdown.report_bind_failure(e); - return; + return Err(PerThreadError::new(worker_id, PerThreadErrorKind::Bind, e)); } }; tracing::debug!("tako-pt-compio worker {worker_id} listening on {addr}"); @@ -104,6 +111,7 @@ pub(crate) fn worker_main_compio( let cancel = shutdown.inner.child_token(); let mut backoff = compio_accept_backoff(); let mut connections = futures_util::stream::FuturesUnordered::new(); + let upgrades = UpgradeSupervisor::default(); loop { let accept_fut = listener.accept(); @@ -148,29 +156,21 @@ pub(crate) fn worker_main_compio( None }; let io = Leased::new(HyperStream::new_plain(stream), permit); - let router = connection_router(&router); + let service = http_service_with_upgrades( + Arc::clone(&router), + ConnInfo::tcp(peer), + upgrades.registry(), + ); let conn_cancel = cancel.clone(); connections.push(compio::runtime::spawn(async move { - let svc = service_fn(move |mut req| { - let router = router.clone(); - async move { - req.extensions_mut().insert(peer); - req.extensions_mut().insert(ConnInfo::tcp(peer)); - let resp = router - .dispatch(req.map(tako_rs_core::body::TakoBody::new)) - .await; - Ok::<_, Infallible>(resp) - } - }); - let mut http = http1::Builder::new(); http.keep_alive(true); http .timer(cyper_core::CompioTimer) .header_read_timeout(cfg.header_read_timeout); if let Err(err) = drive_connection( - http.serve_connection(io, svc).with_upgrades(), + http.serve_connection(io, service).with_upgrades(), conn_cancel.cancelled(), hyper::server::conn::http1::UpgradeableConnection::graceful_shutdown, ) @@ -186,16 +186,21 @@ pub(crate) fn worker_main_compio( while connections.next().now_or_never().flatten().is_some() {} } + upgrades.trigger(); if compio::time::timeout(cfg.drain_timeout, async { while connections.next().await.is_some() {} + upgrades.join().await; }) .await .is_err() { + upgrades.abort(); for connection in connections { connection.cancel().await; } + upgrades.join().await; } + #[cfg(feature = "signals")] tako_rs_core::signals::transport::emit_server_stopped( &listener.local_addr().unwrap_or(addr).to_string(), @@ -203,5 +208,8 @@ pub(crate) fn worker_main_compio( false, ) .await; - }); + Ok(()) + }; + + supervisor.supervise_runtime(worker_id, || rt.block_on(accept_loop)) } diff --git a/tako-rs-server-pt/tests/supervised.rs b/tako-rs-server-pt/tests/supervised.rs new file mode 100644 index 00000000..076ef3d2 --- /dev/null +++ b/tako-rs-server-pt/tests/supervised.rs @@ -0,0 +1,91 @@ +use std::time::Duration; + +use tako_rs_core::router::Router; +use tako_rs_server_pt::PerThreadConfig; +use tako_rs_server_pt::PerThreadErrorKind; +use tako_rs_server_pt::PerThreadWorkerState; +use tako_rs_server_pt::spawn_per_thread_supervised; + +#[tokio::test] +async fn clean_shutdown_clears_readiness_and_joins_every_worker() { + let handle = spawn_per_thread_supervised( + "127.0.0.1:0", + Router::new(), + PerThreadConfig { + workers: 2, + ..PerThreadConfig::default() + }, + ) + .unwrap(); + tokio::time::timeout(Duration::from_secs(2), handle.wait_ready()) + .await + .unwrap() + .unwrap(); + let readiness = handle.readiness(); + assert!(readiness.is_ready()); + + handle.shutdown(); + assert!(!readiness.is_ready()); + tokio::time::timeout(Duration::from_secs(2), handle.result()) + .await + .unwrap() + .unwrap(); + handle.join().unwrap(); + assert_eq!(readiness.workers(), [PerThreadWorkerState::Stopped; 2]); +} + +#[tokio::test] +async fn every_bind_failing_reports_an_error_instead_of_waiting_forever() { + let occupied = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + + for balance_connections in [false, true] { + let handle = spawn_per_thread_supervised( + &occupied.local_addr().unwrap().to_string(), + Router::new(), + PerThreadConfig { + workers: 2, + balance_connections, + ..PerThreadConfig::default() + }, + ) + .unwrap(); + let ready = tokio::time::timeout(Duration::from_secs(2), handle.wait_ready()).await; + let result = tokio::time::timeout(Duration::from_secs(2), handle.result()).await; + handle.shutdown(); + let joined = handle.join(); + + assert!(ready.unwrap().is_err()); + assert_eq!( + result.unwrap().unwrap_err().kind(), + PerThreadErrorKind::Bind + ); + assert_eq!(joined.unwrap_err().kind(), PerThreadErrorKind::Bind); + } +} + +#[test] +fn invalid_configuration_fails_before_workers_are_spawned() { + let invalid = [ + PerThreadConfig { + max_connections: Some(tokio::sync::Semaphore::MAX_PERMITS + 1), + ..PerThreadConfig::default() + }, + PerThreadConfig { + backlog: -1, + ..PerThreadConfig::default() + }, + PerThreadConfig { + drain_timeout: Duration::MAX, + ..PerThreadConfig::default() + }, + PerThreadConfig { + header_read_timeout: Some(Duration::MAX), + ..PerThreadConfig::default() + }, + ]; + + for config in invalid { + let spawned = spawn_per_thread_supervised("127.0.0.1:0", Router::new(), config); + assert!(matches!(spawned, Err(error) if error.kind() == std::io::ErrorKind::InvalidInput)); + } +} diff --git a/tako-rs-server/src/builder/compio_server.rs b/tako-rs-server/src/builder/compio_server.rs index 94a773ae..4fc7cc4e 100644 --- a/tako-rs-server/src/builder/compio_server.rs +++ b/tako-rs-server/src/builder/compio_server.rs @@ -14,6 +14,8 @@ use super::tls_cert::TlsCert; use super::tls_cert::build_rustls_server_config; use crate::ServerConfig; +mod raw; + /// Fluent constructor for the compio-runtime [`CompioServer`]. #[derive(Debug, Default, Clone)] pub struct CompioServerBuilder { @@ -25,6 +27,13 @@ pub struct CompioServerBuilder { } impl CompioServerBuilder { + /// Builds the server after validating its configuration. + pub fn try_build(self) -> Result { + self.config.validate()?; + + Ok(self.build()) + } + /// Override the [`ServerConfig`]. #[must_use] pub fn config(mut self, config: ServerConfig) -> Self { @@ -48,7 +57,7 @@ impl CompioServerBuilder { self } - /// Finalize and produce the [`CompioServer`]. + /// Defers validation to startup; use [`Self::try_build`] to validate now. pub fn build(self) -> CompioServer { CompioServer { config: self.config, @@ -97,12 +106,20 @@ impl CompioServer { listener: compio::net::TcpListener, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); spawn_done_compio( &handle, - crate::server_compio::run(listener, router, signal, self.config.clone()), + crate::server_compio::run( + listener, + router, + signal, + self.config.clone(), + handle.upgrade_supervisor(), + ), ); Ok(handle) } @@ -122,6 +139,8 @@ impl CompioServer { listener: compio::net::TcpListener, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); @@ -151,12 +170,20 @@ impl CompioServer { listener: compio::net::TcpListener, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); spawn_done_compio( &handle, - crate::server_compio::run_proxy_protocol(listener, router, signal, self.config.clone()), + crate::server_compio::run_proxy_protocol( + listener, + router, + signal, + self.config.clone(), + handle.upgrade_supervisor(), + ), ); Ok(handle) } @@ -164,12 +191,17 @@ impl CompioServer { /// Spawns HTTP/1 on a Unix domain socket; bind failures are returned by `result()`. #[cfg(unix)] pub fn spawn_unix_http(&self, path: impl Into, router: Router) -> ServerHandle { + if let Err(error) = self.config.validate() { + return super::spawn::failed_handle(error.into(), self.config.drain_timeout); + } + let path = path.into(); let config = self.config.clone(); let (handle, signal) = make_handle(config.drain_timeout, None); + let upgrades = handle.upgrade_supervisor(); spawn_done_compio(&handle, async move { let listener = crate::server_compio::unix::UnixSocketListener::bind(path).await?; - crate::server_compio::run_unix(listener, router, signal, config).await + crate::server_compio::run_unix(listener, router, signal, config, upgrades).await }); handle } @@ -181,6 +213,8 @@ impl CompioServer { path: impl Into, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let listener = crate::server_compio::unix::UnixSocketListener::bind(path.into()).await?; @@ -188,7 +222,13 @@ impl CompioServer { let (handle, signal) = make_handle(config.drain_timeout, None); spawn_done_compio( &handle, - crate::server_compio::run_unix(listener, router, signal, config), + crate::server_compio::run_unix( + listener, + router, + signal, + config, + handle.upgrade_supervisor(), + ), ); Ok(handle) } @@ -208,6 +248,8 @@ impl CompioServer { listener: compio::net::TcpListener, router: Router, ) -> Result { + self.config.validate()?; + let tls = self .tls .as_ref() @@ -218,12 +260,13 @@ impl CompioServer { let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); spawn_done_compio( &handle, - crate::server_tls_compio::run_with_config( + crate::server_tls_compio::run_supervised( listener, router, tls, Some(signal), self.config.clone(), + handle.upgrade_supervisor(), ), ); Ok(handle) @@ -244,6 +287,8 @@ impl CompioServer { addr: impl Into, router: Router, ) -> Result { + self.config.validate()?; + let tls = self .tls .as_ref() diff --git a/tako-rs-server/src/builder/compio_server/raw.rs b/tako-rs-server/src/builder/compio_server/raw.rs new file mode 100644 index 00000000..b40d4f49 --- /dev/null +++ b/tako-rs-server/src/builder/compio_server/raw.rs @@ -0,0 +1,150 @@ +use std::future::Future; +use std::net::SocketAddr; +#[cfg(unix)] +use std::path::PathBuf; +use std::pin::Pin; +#[cfg(feature = "udp")] +use std::sync::Arc; + +use super::CompioServer; +use crate::ServerHandle; +use crate::builder::spawn::failed_handle; +use crate::builder::spawn::make_handle; +use crate::builder::spawn::spawn_done_compio; + +impl CompioServer { + /// Startup errors are returned by [`ServerHandle::result`]. + pub fn spawn_tcp_raw(&self, addr: impl Into, handler: F) -> ServerHandle + where + F: Fn(compio::net::TcpStream, SocketAddr) -> Pin>>> + + Send + + Sync + + 'static, + { + self + .try_spawn_tcp_raw(addr, handler) + .unwrap_or_else(|error| failed_handle(error, self.config.drain_timeout)) + } + + /// Returns configuration and bind errors before spawning. + pub fn try_spawn_tcp_raw( + &self, + addr: impl Into, + handler: F, + ) -> Result + where + F: Fn(compio::net::TcpStream, SocketAddr) -> Pin>>> + + Send + + Sync + + 'static, + { + self.config.validate()?; + + let listener = compio::net::TcpListener::from_std(std::net::TcpListener::bind(addr.into())?)?; + let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); + let config = self.config.clone(); + spawn_done_compio(&handle, async move { + crate::server_tcp::run_listener(listener, handler, signal, config) + .await + .map_err(Into::into) + }); + Ok(handle) + } + + /// Drops datagrams received at the handler cap; startup errors reach [`ServerHandle::result`]. + #[cfg(feature = "udp")] + pub fn spawn_udp_raw(&self, addr: impl Into, handler: F) -> ServerHandle + where + F: Fn(Vec, SocketAddr, Arc) -> Pin>> + + Send + + Sync + + 'static, + { + self + .try_spawn_udp_raw(addr, handler) + .unwrap_or_else(|error| failed_handle(error, self.config.drain_timeout)) + } + + /// Returns configuration and bind errors before spawning. + #[cfg(feature = "udp")] + pub fn try_spawn_udp_raw( + &self, + addr: impl Into, + handler: F, + ) -> Result + where + F: Fn(Vec, SocketAddr, Arc) -> Pin>> + + Send + + Sync + + 'static, + { + self.config.validate()?; + + let socket = compio::net::UdpSocket::from_std(std::net::UdpSocket::bind(addr.into())?)?; + let (handle, signal) = make_handle(self.config.drain_timeout, Some(socket.local_addr()?)); + let config = self.config.clone(); + spawn_done_compio(&handle, async move { + crate::server_udp::run_socket(socket, handler, signal, config) + .await + .map_err(Into::into) + }); + Ok(handle) + } + + /// Startup errors are returned by [`ServerHandle::result`]. + #[cfg(unix)] + pub fn spawn_unix_raw(&self, path: impl Into, handler: F) -> ServerHandle + where + F: Fn( + compio::net::UnixStream, + tako_rs_core::conn_info::UnixPeerAddr, + ) -> Pin>>> + + Send + + Sync + + 'static, + { + if let Err(error) = self.config.validate() { + return failed_handle(error.into(), self.config.drain_timeout); + } + + let path = path.into(); + let config = self.config.clone(); + let (handle, signal) = make_handle(config.drain_timeout, None); + spawn_done_compio(&handle, async move { + crate::server_unix::serve_unix_with_config(path, handler, signal, config) + .await + .map_err(Into::into) + }); + handle + } + + /// Returns configuration and bind errors before spawning. + #[cfg(unix)] + pub async fn try_spawn_unix_raw( + &self, + path: impl Into, + handler: F, + ) -> Result + where + F: Fn( + compio::net::UnixStream, + tako_rs_core::conn_info::UnixPeerAddr, + ) -> Pin>>> + + Send + + Sync + + 'static, + { + self.config.validate()?; + + let path = path.into(); + let listener = crate::server_compio::unix::bind_listener(&path).await?; + let config = self.config.clone(); + let (handle, signal) = make_handle(config.drain_timeout, None); + spawn_done_compio(&handle, async move { + crate::server_unix::run_listener(listener, &path, handler, signal, config) + .await + .map_err(Into::into) + }); + Ok(handle) + } +} diff --git a/tako-rs-server/src/builder/handle.rs b/tako-rs-server/src/builder/handle.rs index 62d1ca48..5e00d3fd 100644 --- a/tako-rs-server/src/builder/handle.rs +++ b/tako-rs-server/src/builder/handle.rs @@ -34,9 +34,14 @@ pub struct ServerHandle { pub(crate) drain_timeout: Duration, pub(crate) result: Arc>>>, pub(crate) local_addr: Option, + pub(crate) upgrades: tako_rs_core::server_support::UpgradeRegistry, } impl ServerHandle { + pub(crate) fn upgrade_supervisor(&self) -> tako_rs_core::server_support::UpgradeSupervisor { + tako_rs_core::server_support::UpgradeSupervisor::new(self.upgrades.clone()) + } + /// Stops accepting and starts draining without waiting for it. pub fn trigger(&self) { self.shutdown.cancel(); diff --git a/tako-rs-server/src/builder/spawn.rs b/tako-rs-server/src/builder/spawn.rs index af7f6ee1..e1a604bf 100644 --- a/tako-rs-server/src/builder/spawn.rs +++ b/tako-rs-server/src/builder/spawn.rs @@ -31,6 +31,7 @@ pub(crate) fn make_handle( drain_timeout, result: std::sync::Arc::default(), local_addr, + upgrades: tako_rs_core::server_support::UpgradeRegistry::default(), }, async move { shutdown_for_task.cancelled().await }, ) @@ -73,6 +74,8 @@ where outcome: None, }; let abort = handle.abort.clone(); + let upgrades = tako_rs_core::server_support::UpgradeSupervisor::new(handle.upgrades.clone()); + async move { let outcome = { let task = std::pin::pin!(std::panic::AssertUnwindSafe(fut).catch_unwind()); @@ -90,6 +93,9 @@ where )), } }; + + upgrades.abort(); + upgrades.join().await; completion.outcome = Some(outcome); drop(completion); } diff --git a/tako-rs-server/src/builder/tokio_server.rs b/tako-rs-server/src/builder/tokio_server.rs index 6acdb32e..136719df 100644 --- a/tako-rs-server/src/builder/tokio_server.rs +++ b/tako-rs-server/src/builder/tokio_server.rs @@ -18,6 +18,9 @@ use super::tls_cert::TlsCert; use super::tls_cert::build_rustls_server_config; use crate::ServerConfig; +#[cfg(unix)] +mod raw_unix; + /// Fluent constructor for the tokio-runtime [`Server`]. #[derive(Debug, Default, Clone)] pub struct ServerBuilder { @@ -28,6 +31,13 @@ pub struct ServerBuilder { } impl ServerBuilder { + /// Builds the server after validating its configuration. + pub fn try_build(self) -> Result { + self.config.validate()?; + + Ok(self.build()) + } + /// Override the [`ServerConfig`] (drain timeout, h2 caps, `max_connections`, …). #[must_use] pub fn config(mut self, config: ServerConfig) -> Self { @@ -50,7 +60,7 @@ impl ServerBuilder { self } - /// Finalize and produce the [`Server`]. + /// Defers validation to startup; use [`Self::try_build`] to validate now. pub fn build(self) -> Server { Server { config: self.config, @@ -98,12 +108,20 @@ impl Server { listener: TcpListener, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); spawn_done( &handle, - crate::server::run(listener, router, signal, self.config.clone()), + crate::server::run( + listener, + router, + signal, + self.config.clone(), + handle.upgrade_supervisor(), + ), ); Ok(handle) } @@ -123,6 +141,8 @@ impl Server { listener: TcpListener, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); @@ -148,6 +168,8 @@ impl Server { listener: TcpListener, router: Router, ) -> Result { + self.config.validate()?; + let tls = self .tls .as_ref() @@ -158,7 +180,14 @@ impl Server { let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); spawn_done( &handle, - crate::server_tls::run_with_config(listener, router, tls, Some(signal), self.config.clone()), + crate::server_tls::run_supervised( + listener, + router, + tls, + Some(signal), + self.config.clone(), + handle.upgrade_supervisor(), + ), ); Ok(handle) } @@ -178,6 +207,8 @@ impl Server { addr: impl Into, router: Router, ) -> Result { + self.config.validate()?; + let tls = self .tls .as_ref() @@ -206,11 +237,16 @@ impl Server { /// Spawns HTTP over a Unix socket. Bind failures are returned by `result()`. #[cfg(unix)] pub fn spawn_unix_http(&self, path: impl Into, router: Router) -> ServerHandle { + if let Err(error) = self.config.validate() { + return super::spawn::failed_handle(error.into(), self.config.drain_timeout); + } + let path = path.into(); let config = self.config.clone(); let (handle, signal) = make_handle(config.drain_timeout, None); + let upgrades = handle.upgrade_supervisor(); spawn_done(&handle, async move { - crate::server_unix::http::run_http(&path, router, signal, config).await + crate::server_unix::http::run_http(&path, router, signal, config, upgrades).await }); handle } @@ -222,14 +258,18 @@ impl Server { path: impl Into, router: Router, ) -> Result { + self.config.validate()?; + let path = path.into(); #[cfg(feature = "plugins")] router.setup_plugins_once()?; let listener = crate::server_unix::listener::bind_unix_listener(&path).await?; let config = self.config.clone(); let (handle, signal) = make_handle(config.drain_timeout, None); + let upgrades = handle.upgrade_supervisor(); spawn_done(&handle, async move { - crate::server_unix::http::run_listener(listener, &path, router, signal, config).await + crate::server_unix::http::run_listener(listener, &path, router, signal, config, upgrades) + .await }); Ok(handle) } @@ -250,6 +290,8 @@ impl Server { port: u32, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let listener = tokio_vsock::VsockListener::bind(tokio_vsock::VsockAddr::new(cid, port))?; @@ -257,7 +299,15 @@ impl Server { let (handle, signal) = make_handle(config.drain_timeout, None); spawn_done( &handle, - crate::server_vsock::run_listener(listener, cid, port, router, signal, config), + crate::server_vsock::run_listener( + listener, + cid, + port, + router, + signal, + config, + handle.upgrade_supervisor(), + ), ); Ok(handle) } @@ -277,6 +327,8 @@ impl Server { listener: TcpListener, router: Router, ) -> Result { + self.config.validate()?; + #[cfg(feature = "plugins")] router.setup_plugins_once()?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); @@ -287,6 +339,7 @@ impl Server { router, signal, self.config.clone(), + handle.upgrade_supervisor(), ), ); Ok(handle) @@ -323,13 +376,15 @@ impl Server { + Sync + 'static, { + self.config.validate()?; + let bound = std::net::TcpListener::bind(addr.into())?; bound.set_nonblocking(true)?; let listener = tokio::net::TcpListener::from_std(bound)?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(listener.local_addr()?)); - let drain_timeout = self.config.drain_timeout; + let config = self.config.clone(); spawn_done(&handle, async move { - crate::server_tcp::run_listener(listener, handler, signal, drain_timeout) + crate::server_tcp::run_listener(listener, handler, signal, config) .await .map_err(Into::into) }); @@ -371,13 +426,15 @@ impl Server { + Sync + 'static, { + self.config.validate()?; + let bound = std::net::UdpSocket::bind(addr.into())?; bound.set_nonblocking(true)?; let socket = tokio::net::UdpSocket::from_std(bound)?; let (handle, signal) = make_handle(self.config.drain_timeout, Some(socket.local_addr()?)); - let drain_timeout = self.config.drain_timeout; + let config = self.config.clone(); spawn_done(&handle, async move { - crate::server_udp::run_socket(socket, handler, signal, drain_timeout) + crate::server_udp::run_socket(socket, handler, signal, config) .await .map_err(Into::into) }); diff --git a/tako-rs-server/src/builder/tokio_server/raw_unix.rs b/tako-rs-server/src/builder/tokio_server/raw_unix.rs new file mode 100644 index 00000000..c8ccb9a6 --- /dev/null +++ b/tako-rs-server/src/builder/tokio_server/raw_unix.rs @@ -0,0 +1,66 @@ +use std::future::Future; +use std::path::PathBuf; +use std::pin::Pin; + +use super::Server; +use crate::ServerHandle; +use crate::builder::spawn::failed_handle; +use crate::builder::spawn::make_handle; +use crate::builder::spawn::spawn_done; + +impl Server { + /// Startup errors are returned by [`ServerHandle::result`]. + pub fn spawn_unix_raw(&self, path: impl Into, handler: F) -> ServerHandle + where + F: Fn( + tokio::net::UnixStream, + tokio::net::unix::SocketAddr, + ) -> Pin> + Send>> + + Send + + Sync + + 'static, + { + if let Err(error) = self.config.validate() { + return failed_handle(error.into(), self.config.drain_timeout); + } + + let path = path.into(); + let config = self.config.clone(); + let (handle, signal) = make_handle(config.drain_timeout, None); + spawn_done(&handle, async move { + crate::server_unix::serve_unix_with_config(path, handler, signal, config) + .await + .map_err(Into::into) + }); + handle + } + + /// Returns configuration and bind errors before spawning. + pub async fn try_spawn_unix_raw( + &self, + path: impl Into, + handler: F, + ) -> Result + where + F: Fn( + tokio::net::UnixStream, + tokio::net::unix::SocketAddr, + ) -> Pin> + Send>> + + Send + + Sync + + 'static, + { + self.config.validate()?; + + let path = path.into(); + let listener = crate::server_unix::listener::bind_unix_listener(&path).await?; + let config = self.config.clone(); + let (handle, signal) = make_handle(config.drain_timeout, None); + spawn_done(&handle, async move { + crate::server_unix::run_listener(listener, &path, handler, signal, config) + .await + .map_err(Into::into) + }); + Ok(handle) + } +} diff --git a/tako-rs-server/src/config.rs b/tako-rs-server/src/config.rs index eb9180a2..25669a05 100644 --- a/tako-rs-server/src/config.rs +++ b/tako-rs-server/src/config.rs @@ -2,6 +2,9 @@ use std::time::Duration; +mod validation; +pub use validation::ServerConfigError; + /// QUIC congestion controller for HTTP/3, from `quinn::congestion` without a quinn dependency. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum H3Congestion { @@ -55,7 +58,9 @@ pub struct ServerConfig { /// `drain_timeout`. pub h3_goaway_grace: Duration, /// Cap on concurrent connections, `None` for none; an upgraded connection such as a WebSocket - /// counts until its socket closes. + /// counts until its socket closes. Raw TCP and Unix streams count until their handler returns. + /// On raw UDP, caps active datagram handlers; excess datagrams are dropped without queuing. + /// A configured limit must be positive and at most [`tokio::sync::Semaphore::MAX_PERMITS`]. pub max_connections: Option, /// Read deadline applied before the PROXY protocol header is parsed, 10 s by default. pub proxy_read_timeout: Duration, diff --git a/tako-rs-server/src/config/validation.rs b/tako-rs-server/src/config/validation.rs new file mode 100644 index 00000000..8da952f4 --- /dev/null +++ b/tako-rs-server/src/config/validation.rs @@ -0,0 +1,94 @@ +use std::fmt; +use std::time::Instant; + +use super::ServerConfig; + +/// Invalid server configuration. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ServerConfigError { + field: &'static str, + reason: &'static str, +} + +impl ServerConfigError { + const fn new(field: &'static str, reason: &'static str) -> Self { + Self { field, reason } + } + + pub const fn field(&self) -> &'static str { + self.field + } + + pub const fn reason(&self) -> &'static str { + self.reason + } +} + +impl fmt::Display for ServerConfigError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "{} {}", self.field, self.reason) + } +} + +impl std::error::Error for ServerConfigError {} + +impl From for std::io::Error { + fn from(error: ServerConfigError) -> Self { + Self::new(std::io::ErrorKind::InvalidInput, error) + } +} + +impl ServerConfig { + /// Validates limits and deadlines before startup. + /// + /// Zero shutdown and handshake deadlines expire immediately; zero disables HTTP/3 idle timeout. + /// Protocol stream, header, and send-buffer limits may also be zero. + pub fn validate(&self) -> Result<(), ServerConfigError> { + if let Some(limit) = self.max_connections + && (limit == 0 || limit > tokio::sync::Semaphore::MAX_PERMITS) + { + return Err(ServerConfigError::new( + "max_connections", + "must be between 1 and tokio::sync::Semaphore::MAX_PERMITS", + )); + } + + if self.h2_max_send_buf_size > u32::MAX as usize { + return Err(ServerConfigError::new( + "h2_max_send_buf_size", + "must be at most u32::MAX", + )); + } + + if let Some(timeout) = self.h3_max_idle_timeout + && timeout.as_millis() > (1_u128 << 62) - 1 + { + return Err(ServerConfigError::new( + "h3_max_idle_timeout", + "must fit in a QUIC variable-length integer of milliseconds", + )); + } + + let now = Instant::now(); + for (field, timeout) in [ + ("drain_timeout", Some(self.drain_timeout)), + ("header_read_timeout", self.header_read_timeout), + ("h2_keep_alive_interval", self.h2_keep_alive_interval), + ("h3_max_idle_timeout", self.h3_max_idle_timeout), + ("h3_goaway_grace", Some(self.h3_goaway_grace)), + ("proxy_read_timeout", Some(self.proxy_read_timeout)), + ("tls_handshake_timeout", Some(self.tls_handshake_timeout)), + ] { + if let Some(timeout) = timeout + && now.checked_add(timeout).is_none() + { + return Err(ServerConfigError::new( + field, + "exceeds the monotonic clock's range", + )); + } + } + + Ok(()) + } +} diff --git a/tako-rs-server/src/lib.rs b/tako-rs-server/src/lib.rs index 1c03dce1..98c479b0 100644 --- a/tako-rs-server/src/lib.rs +++ b/tako-rs-server/src/lib.rs @@ -9,6 +9,7 @@ mod config; pub use config::AcceptBackoff; pub use config::H3Congestion; pub use config::ServerConfig; +pub use config::ServerConfigError; #[cfg(not(feature = "compio"))] mod server; diff --git a/tako-rs-server/src/proxy_protocol/listener.rs b/tako-rs-server/src/proxy_protocol/listener.rs index 30552507..51b32cd6 100644 --- a/tako-rs-server/src/proxy_protocol/listener.rs +++ b/tako-rs-server/src/proxy_protocol/listener.rs @@ -10,6 +10,7 @@ use tako_rs_core::body::TakoBody; use tako_rs_core::router::Router; use tako_rs_core::server_support::ConnectionTimer; use tako_rs_core::server_support::Leased; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::connection_router; use tako_rs_core::server_support::drive_connection; use tako_rs_core::types::BoxError; @@ -24,7 +25,10 @@ pub(crate) async fn run_proxy_http( router: Router, signal: impl Future + Send + 'static, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { + config.validate()?; + let router = Arc::new(router); #[cfg(feature = "plugins")] @@ -90,6 +94,7 @@ pub(crate) async fn run_proxy_http( let router = connection_router(&router); let conn_cancel = cancel.clone(); + let registry = upgrades.registry(); join_set.spawn(async move { let handshake = tokio::select! { @@ -115,6 +120,7 @@ pub(crate) async fn run_proxy_http( let io = hyper_util::rt::TokioIo::new(Leased::new(stream, permit)); let svc = service_fn(move |mut req| { + registry.attach(&mut req); apply_to_request(&mut req, &proxy_header); let router = router.clone(); async move { @@ -146,8 +152,10 @@ pub(crate) async fn run_proxy_http( } } + upgrades.trigger(); let drain = tokio::time::timeout(drain_timeout, async { while join_set.join_next().await.is_some() {} + upgrades.join().await; }); if drain.await.is_err() { @@ -155,7 +163,9 @@ pub(crate) async fn run_proxy_http( "Drain timeout exceeded, aborting {} remaining connections", join_set.len() ); + upgrades.abort(); join_set.shutdown().await; + upgrades.join().await; } tracing::info!("PROXY protocol HTTP server shut down gracefully"); diff --git a/tako-rs-server/src/server.rs b/tako-rs-server/src/server.rs index 08441b37..3f6ff369 100644 --- a/tako-rs-server/src/server.rs +++ b/tako-rs-server/src/server.rs @@ -10,6 +10,7 @@ use tako_rs_core::server_support::ConnDriver; use tako_rs_core::server_support::Http1Io; use tako_rs_core::server_support::Leased; use tako_rs_core::server_support::ShutdownSignal; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::tcp_service; #[cfg(feature = "signals")] use tako_rs_core::signals::transport as signal_tx; @@ -27,7 +28,10 @@ pub(crate) async fn run( router: Router, signal: impl Future + Send + 'static, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { + config.validate()?; + #[cfg(feature = "tako-tracing")] tako_rs_core::tracing::init_tracing(); @@ -89,7 +93,7 @@ pub(crate) async fn run( }; let _ = stream.set_nodelay(true); - let ctx = Arc::new(ConnCtx::new(Arc::clone(&router), addr)); + let ctx = Arc::new(ConnCtx::new(Arc::clone(&router), addr).with_upgrades(upgrades.registry())); let io = Http1Io::new( hyper_util::rt::TokioIo::new(Leased::new(stream, permit)), Arc::clone(&ctx.activity), @@ -134,8 +138,10 @@ pub(crate) async fn run( } stopping.trigger(); + upgrades.trigger(); let drain = tokio::time::timeout(drain_timeout, async { while join_set.join_next().await.is_some() {} + upgrades.join().await; }); if drain.await.is_err() { @@ -144,7 +150,9 @@ pub(crate) async fn run( drain_timeout, join_set.len() ); + upgrades.abort(); join_set.shutdown().await; + upgrades.join().await; } tracing::info!("Server shut down gracefully"); diff --git a/tako-rs-server/src/server_compio.rs b/tako-rs-server/src/server_compio.rs index a37693d6..a6aebb8d 100644 --- a/tako-rs-server/src/server_compio.rs +++ b/tako-rs-server/src/server_compio.rs @@ -4,6 +4,7 @@ use std::future::Future; use compio::net::TcpListener; use tako_rs_core::router::Router; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::types::BoxError; use crate::ServerConfig; @@ -23,8 +24,9 @@ pub(crate) async fn run( router: Router, signal: impl Future, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { - accept_loop(listener, router, signal, config, Protocol::Http1).await + accept_loop(listener, router, signal, config, Protocol::Http1, upgrades).await } #[cfg(feature = "http2")] @@ -34,7 +36,15 @@ pub(crate) async fn run_h2c( signal: impl Future, config: ServerConfig, ) -> Result<(), BoxError> { - accept_loop(listener, router, signal, config, Protocol::H2c).await + accept_loop( + listener, + router, + signal, + config, + Protocol::H2c, + UpgradeSupervisor::default(), + ) + .await } #[cfg(feature = "proxy-protocol")] @@ -43,8 +53,17 @@ pub(crate) async fn run_proxy_protocol( router: Router, signal: impl Future, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { - accept_loop(listener, router, signal, config, Protocol::ProxyHttp1).await + accept_loop( + listener, + router, + signal, + config, + Protocol::ProxyHttp1, + upgrades, + ) + .await } #[cfg(unix)] @@ -53,6 +72,7 @@ pub(crate) async fn run_unix( router: Router, signal: impl Future, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { - accept_loop(listener, router, signal, config, Protocol::Http1).await + accept_loop(listener, router, signal, config, Protocol::Http1, upgrades).await } diff --git a/tako-rs-server/src/server_compio/accept.rs b/tako-rs-server/src/server_compio/accept.rs index 124b35fc..5c3f3371 100644 --- a/tako-rs-server/src/server_compio/accept.rs +++ b/tako-rs-server/src/server_compio/accept.rs @@ -13,6 +13,7 @@ use futures_util::StreamExt; use futures_util::future::Either; use futures_util::stream::FuturesUnordered; use tako_rs_core::router::Router; +use tako_rs_core::server_support::UpgradeSupervisor; #[cfg(feature = "signals")] use tako_rs_core::signals::transport as signal_tx; use tako_rs_core::types::BoxError; @@ -63,12 +64,15 @@ pub(crate) async fn accept_loop( signal: impl Future, config: ServerConfig, protocol: Protocol, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> where L: Listener, ::ReadHalf: AsyncRead + Unpin, ::WriteHalf: AsyncWrite + Unpin, { + config.validate()?; + #[cfg(feature = "tako-tracing")] tako_rs_core::tracing::init_tracing(); @@ -137,7 +141,7 @@ where peer, router.clone(), protocol, - settings, + settings.clone().with_upgrades(upgrades.registry()), cancel.clone(), permit, ))); @@ -151,16 +155,20 @@ where } cancel.cancel(); + upgrades.trigger(); if compio::time::timeout(drain_timeout, async { while connections.next().await.is_some() {} + upgrades.join().await; }) .await .is_err() { tracing::warn!("drain timeout exceeded; cancelling remaining connections"); + upgrades.abort(); for connection in connections { connection.cancel().await; } + upgrades.join().await; } listener.finish(); diff --git a/tako-rs-server/src/server_compio/connection.rs b/tako-rs-server/src/server_compio/connection.rs index a62c3772..68c0a6cd 100644 --- a/tako-rs-server/src/server_compio/connection.rs +++ b/tako-rs-server/src/server_compio/connection.rs @@ -21,6 +21,7 @@ use tako_rs_core::conn_info::ConnInfo; use tako_rs_core::conn_info::UnixPeerAddr; use tako_rs_core::router::Router; use tako_rs_core::server_support::Leased; +use tako_rs_core::server_support::UpgradeRegistry; use tako_rs_core::server_support::drive_connection; use tako_rs_core::server_support::sanitize_response; #[cfg(feature = "signals")] @@ -122,8 +123,9 @@ impl Peer { } /// Per-connection limits copied out of [`ServerConfig`] once per server. -#[derive(Clone, Copy)] +#[derive(Clone)] pub(crate) struct ConnectionSettings { + upgrades: Option, keep_alive: bool, header_read_timeout: Option, #[cfg(feature = "http2")] @@ -135,6 +137,7 @@ pub(crate) struct ConnectionSettings { impl From<&ServerConfig> for ConnectionSettings { fn from(config: &ServerConfig) -> Self { Self { + upgrades: None, keep_alive: config.keep_alive, header_read_timeout: config.header_read_timeout, #[cfg(feature = "http2")] @@ -145,6 +148,13 @@ impl From<&ServerConfig> for ConnectionSettings { } } +impl ConnectionSettings { + pub(crate) fn with_upgrades(mut self, upgrades: UpgradeRegistry) -> Self { + self.upgrades = Some(upgrades); + self + } +} + pub(crate) async fn serve_connection( #[cfg_attr(not(feature = "proxy-protocol"), allow(unused_mut))] mut stream: S, peer: Peer, @@ -178,6 +188,9 @@ pub(crate) async fn serve_connection( let wire = protocol.wire(); let io = Leased::new(HyperStream::new_plain(stream), permit); let svc = service_fn(move |mut req| { + if let Some(upgrades) = &settings.upgrades { + upgrades.attach(&mut req); + } peer.annotate(&mut req, wire); let router = router.clone(); async move { diff --git a/tako-rs-server/src/server_compio/h3.rs b/tako-rs-server/src/server_compio/h3.rs index 73d2ba03..1d3605e7 100644 --- a/tako-rs-server/src/server_compio/h3.rs +++ b/tako-rs-server/src/server_compio/h3.rs @@ -34,6 +34,8 @@ pub(crate) fn bind_endpoint( config: &ServerConfig, #[cfg(feature = "http3-qlog")] qlog_dir: Option<&std::path::Path>, ) -> Result, BoxError> { + config.validate()?; + // Early data requires replay protection, which this server does not provide. let mut tls = tls_config.clone(); tls.max_early_data_size = 0; @@ -69,6 +71,8 @@ pub(crate) async fn run_endpoint( signal: impl Future, config: ServerConfig, ) -> Result<(), BoxError> { + config.validate()?; + let BoundEndpoint { endpoint, connections: connection_config, diff --git a/tako-rs-server/src/server_h2c.rs b/tako-rs-server/src/server_h2c.rs index eb26bf6c..22dfb317 100644 --- a/tako-rs-server/src/server_h2c.rs +++ b/tako-rs-server/src/server_h2c.rs @@ -26,6 +26,8 @@ pub(crate) async fn run( signal: impl Future + Send + 'static, config: ServerConfig, ) -> Result<(), BoxError> { + config.validate()?; + #[cfg(feature = "tako-tracing")] tako_rs_core::tracing::init_tracing(); diff --git a/tako-rs-server/src/server_h3/run.rs b/tako-rs-server/src/server_h3/run.rs index 62b5ec0b..415c8014 100644 --- a/tako-rs-server/src/server_h3/run.rs +++ b/tako-rs-server/src/server_h3/run.rs @@ -37,6 +37,8 @@ pub(crate) fn bind_endpoint( config: &ServerConfig, #[cfg(feature = "http3-qlog")] qlog_dir: Option<&std::path::Path>, ) -> Result, BoxError> { + config.validate()?; + #[cfg(feature = "tako-tracing")] tako_rs_core::tracing::init_tracing(); @@ -73,6 +75,8 @@ pub(crate) async fn run_endpoint( signal: Option + Send + 'static>, config: ServerConfig, ) -> Result<(), BoxError> { + config.validate()?; + let BoundEndpoint { endpoint, connections, diff --git a/tako-rs-server/src/server_tcp.rs b/tako-rs-server/src/server_tcp.rs index 6d7f0b1a..2cbe3c19 100644 --- a/tako-rs-server/src/server_tcp.rs +++ b/tako-rs-server/src/server_tcp.rs @@ -45,6 +45,8 @@ use std::net::SocketAddr; use std::pin::Pin; use std::sync::Arc; +use crate::ServerConfig; + /// Runs `handler` for every accepted connection until an accept error, which it returns. /// /// # Examples @@ -77,30 +79,13 @@ where + Sync + 'static, { - let listener = tokio::net::TcpListener::bind(addr).await?; - tracing::info!("TCP server listening on {}", listener.local_addr()?); - - #[cfg(feature = "signals")] - tako_rs_core::signals::transport::emit_server_started( - &listener.local_addr()?.to_string(), - "tcp", - false, + serve_tcp_with_config( + addr, + handler, + std::future::pending(), + ServerConfig::default(), ) - .await; - - let handler = Arc::new(handler); - - loop { - let (stream, peer_addr) = listener.accept().await?; - let _ = stream.set_nodelay(true); - let handler = Arc::clone(&handler); - - tokio::spawn(async move { - if let Err(e) = handler(stream, peer_addr).await { - tracing::error!("TCP connection error from {peer_addr}: {e}"); - } - }); - } + .await } /// Stops accepting once `signal` resolves and drains connections for up to 30 seconds; use @@ -138,8 +123,40 @@ where + 'static, S: Future + Send + 'static, { + serve_tcp_with_config( + addr, + handler, + signal, + ServerConfig { + drain_timeout, + ..ServerConfig::default() + }, + ) + .await +} + +/// Enforces the configured connection limit and drain timeout. +#[cfg(not(feature = "compio"))] +pub async fn serve_tcp_with_config( + addr: &str, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn( + tokio::net::TcpStream, + SocketAddr, + ) -> Pin> + Send>> + + Send + + Sync + + 'static, + S: Future + Send + 'static, +{ + config.validate()?; + let listener = tokio::net::TcpListener::bind(addr).await?; - run_listener(listener, handler, signal, drain_timeout).await + run_listener(listener, handler, signal, config).await } #[cfg(not(feature = "compio"))] @@ -147,7 +164,7 @@ pub(crate) async fn run_listener( listener: tokio::net::TcpListener, handler: F, signal: S, - drain_timeout: std::time::Duration, + config: ServerConfig, ) -> std::io::Result<()> where F: Fn( @@ -159,6 +176,8 @@ where + 'static, S: Future + Send + 'static, { + config.validate()?; + tracing::info!("TCP server listening on {}", listener.local_addr()?); #[cfg(feature = "signals")] @@ -176,7 +195,13 @@ where loop { tokio::select! { - result = listener.accept() => { + biased; + () = &mut signal => { + tracing::info!("TCP server shutting down, draining {} connections", join_set.len()); + break; + } + _ = join_set.join_next(), if !join_set.is_empty() => {} + result = listener.accept(), if config.max_connections.is_none_or(|max| join_set.len() < max) => { let (stream, peer_addr) = result?; let _ = stream.set_nodelay(true); let handler = Arc::clone(&handler); @@ -186,16 +211,11 @@ where tracing::error!("TCP connection error from {peer_addr}: {e}"); } }); - while join_set.try_join_next().is_some() {} - } - () = &mut signal => { - tracing::info!("TCP server shutting down, draining {} connections", join_set.len()); - break; } } } - if tokio::time::timeout(drain_timeout, async { + if tokio::time::timeout(config.drain_timeout, async { while join_set.join_next().await.is_some() {} }) .await @@ -223,31 +243,13 @@ where + Sync + 'static, { - let listener = compio::net::TcpListener::bind(addr).await?; - tracing::info!("TCP server listening on {}", listener.local_addr()?); - - #[cfg(feature = "signals")] - tako_rs_core::signals::transport::emit_server_started( - &listener.local_addr()?.to_string(), - "tcp", - false, + serve_tcp_with_config( + addr, + handler, + std::future::pending(), + ServerConfig::default(), ) - .await; - - let handler = Arc::new(handler); - - loop { - let (stream, peer_addr) = listener.accept().await?; - let _ = stream.set_nodelay(true); - let handler = Arc::clone(&handler); - - compio::runtime::spawn(async move { - if let Err(e) = handler(stream, peer_addr).await { - tracing::error!("TCP connection error from {peer_addr}: {e}"); - } - }) - .detach(); - } + .await } /// Stops accepting once `signal` resolves and drains connections for up to 30 seconds; use @@ -272,6 +274,53 @@ pub async fn serve_tcp_with_shutdown_and_drain( signal: S, drain_timeout: std::time::Duration, ) -> std::io::Result<()> +where + F: Fn(compio::net::TcpStream, SocketAddr) -> Pin>>> + + Send + + Sync + + 'static, + S: Future + 'static, +{ + serve_tcp_with_config( + addr, + handler, + signal, + ServerConfig { + drain_timeout, + ..ServerConfig::default() + }, + ) + .await +} + +/// Enforces the configured connection limit and drain timeout. +#[cfg(feature = "compio")] +pub async fn serve_tcp_with_config( + addr: &str, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn(compio::net::TcpStream, SocketAddr) -> Pin>>> + + Send + + Sync + + 'static, + S: Future + 'static, +{ + config.validate()?; + + let listener = compio::net::TcpListener::bind(addr).await?; + run_listener(listener, handler, signal, config).await +} + +#[cfg(feature = "compio")] +pub(crate) async fn run_listener( + listener: compio::net::TcpListener, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> where F: Fn(compio::net::TcpStream, SocketAddr) -> Pin>>> + Send @@ -282,7 +331,8 @@ where use futures_util::FutureExt; use futures_util::StreamExt; - let listener = compio::net::TcpListener::bind(addr).await?; + config.validate()?; + tracing::info!("TCP server listening on {}", listener.local_addr()?); #[cfg(feature = "signals")] @@ -300,22 +350,35 @@ where let mut signal = signal; loop { + while connections.next().now_or_never().flatten().is_some() {} + + if config + .max_connections + .is_some_and(|max| connections.len() >= max) + { + let completed = std::pin::pin!(connections.next()); + match futures_util::future::select(&mut signal, completed).await { + futures_util::future::Either::Left(_) => break, + futures_util::future::Either::Right(_) => continue, + } + } + let accept_fut = listener.accept(); let accept_fut = std::pin::pin!(accept_fut); - match futures_util::future::select(accept_fut, &mut signal).await { - futures_util::future::Either::Left((result, _)) => { + match futures_util::future::select(&mut signal, accept_fut).await { + futures_util::future::Either::Right((result, _)) => { let (stream, peer_addr) = result?; let _ = stream.set_nodelay(true); let handler = Arc::clone(&handler); + connections.push(compio::runtime::spawn(async move { if let Err(e) = handler(stream, peer_addr).await { tracing::error!("TCP connection error from {peer_addr}: {e}"); } })); - while connections.next().now_or_never().flatten().is_some() {} } - futures_util::future::Either::Right(_) => { + futures_util::future::Either::Left(_) => { tracing::info!( "TCP server shutting down, draining {} connections", connections.len() @@ -325,7 +388,7 @@ where } } - if compio::time::timeout(drain_timeout, async { + if compio::time::timeout(config.drain_timeout, async { while connections.next().await.is_some() {} }) .await diff --git a/tako-rs-server/src/server_tls.rs b/tako-rs-server/src/server_tls.rs index d6b0f1f4..3f8e4faa 100644 --- a/tako-rs-server/src/server_tls.rs +++ b/tako-rs-server/src/server_tls.rs @@ -5,4 +5,5 @@ mod serve; pub use entry::serve_tls_with_rustls_config; pub use entry::serve_tls_with_rustls_config_and_shutdown; +pub(crate) use serve::run_supervised; pub use serve::run_with_config; diff --git a/tako-rs-server/src/server_tls/serve.rs b/tako-rs-server/src/server_tls/serve.rs index 7a7fbc23..a503079f 100644 --- a/tako-rs-server/src/server_tls/serve.rs +++ b/tako-rs-server/src/server_tls/serve.rs @@ -12,8 +12,9 @@ use tako_rs_core::conn_info::TlsInfo; use tako_rs_core::router::Router; use tako_rs_core::server_support::ConnectionTimer; use tako_rs_core::server_support::Leased; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::drive_connection; -use tako_rs_core::server_support::http_service; +use tako_rs_core::server_support::http_service_with_upgrades; #[cfg(feature = "signals")] use tako_rs_core::signals::transport as signal_tx; use tako_rs_core::types::BoxError; @@ -34,6 +35,27 @@ pub async fn run_with_config( signal: Option + Send + 'static>, config: ServerConfig, ) -> Result<(), BoxError> { + run_supervised( + listener, + router, + tls_config, + signal, + config, + UpgradeSupervisor::default(), + ) + .await +} + +pub(crate) async fn run_supervised( + listener: TcpListener, + router: Router, + tls_config: Arc, + signal: Option + Send + 'static>, + config: ServerConfig, + upgrades: UpgradeSupervisor, +) -> Result<(), BoxError> { + config.validate()?; + #[cfg(feature = "tako-tracing")] tako_rs_core::tracing::init_tracing(); @@ -101,6 +123,7 @@ pub async fn run_with_config( let _ = stream.set_nodelay(true); let acceptor = acceptor.clone(); let router = Arc::clone(&router); + let registry = upgrades.registry(); let conn_cancel = cancel.clone(); join_set.spawn(async move { @@ -162,7 +185,7 @@ pub async fn run_with_config( let io = TokioIo::new(Leased::new(tls_stream, permit)); - let svc = http_service(router, conn_info); + let svc = http_service_with_upgrades(router, conn_info, registry); #[cfg(feature = "http2")] if proto.as_deref() == Some(b"h2") { @@ -204,8 +227,10 @@ pub async fn run_with_config( } } + upgrades.trigger(); let drain = tokio::time::timeout(drain_timeout, async { while join_set.join_next().await.is_some() {} + upgrades.join().await; }); if drain.await.is_err() { @@ -214,7 +239,9 @@ pub async fn run_with_config( drain_timeout, join_set.len() ); + upgrades.abort(); join_set.shutdown().await; + upgrades.join().await; } tracing::info!("TLS server shut down gracefully"); diff --git a/tako-rs-server/src/server_tls_compio.rs b/tako-rs-server/src/server_tls_compio.rs index e2ea3e11..341be9eb 100644 --- a/tako-rs-server/src/server_tls_compio.rs +++ b/tako-rs-server/src/server_tls_compio.rs @@ -3,6 +3,7 @@ mod accept; mod serve; +pub(crate) use accept::run_supervised; pub use accept::run_with_config; pub use serve::serve_tls_with_rustls_config; pub use serve::serve_tls_with_rustls_config_and_shutdown; diff --git a/tako-rs-server/src/server_tls_compio/accept.rs b/tako-rs-server/src/server_tls_compio/accept.rs index 1988b2b2..45f21651 100644 --- a/tako-rs-server/src/server_tls_compio/accept.rs +++ b/tako-rs-server/src/server_tls_compio/accept.rs @@ -21,6 +21,7 @@ use tako_rs_core::conn_info::ConnInfo; use tako_rs_core::conn_info::TlsInfo; use tako_rs_core::router::Router; use tako_rs_core::server_support::Leased; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::drive_connection; use tako_rs_core::server_support::sanitize_response; #[cfg(feature = "signals")] @@ -42,6 +43,27 @@ pub async fn run_with_config( signal: Option>, config: ServerConfig, ) -> Result<(), BoxError> { + run_supervised( + listener, + router, + tls_config, + signal, + config, + UpgradeSupervisor::default(), + ) + .await +} + +pub(crate) async fn run_supervised( + listener: TcpListener, + router: Router, + tls_config: Arc, + signal: Option>, + config: ServerConfig, + upgrades: UpgradeSupervisor, +) -> Result<(), BoxError> { + config.validate()?; + #[cfg(feature = "tako-tracing")] tako_rs_core::tracing::init_tracing(); @@ -124,6 +146,7 @@ pub async fn run_with_config( let router = router.clone(); let conn_cancel = cancel.clone(); + let registry = upgrades.registry(); connections.push(compio::runtime::spawn(async move { let handshake_deadline = std::pin::pin!(compio::time::sleep(tls_handshake_timeout)); @@ -184,6 +207,7 @@ pub async fn run_with_config( let io = Leased::new(HyperStream::new_tls(tls_stream), permit); let svc = service_fn(move |mut req| { + registry.attach(&mut req); let r = router.clone(); let conn_info = conn_info.clone(); async move { @@ -252,16 +276,20 @@ pub async fn run_with_config( } cancel.cancel(); + upgrades.trigger(); if compio::time::timeout(drain_timeout, async { while connections.next().await.is_some() {} + upgrades.join().await; }) .await .is_err() { tracing::warn!("drain timeout exceeded; cancelling remaining connections"); + upgrades.abort(); for connection in connections { connection.cancel().await; } + upgrades.join().await; } tracing::info!("TLS server shut down gracefully"); diff --git a/tako-rs-server/src/server_udp.rs b/tako-rs-server/src/server_udp.rs index e0da6bb0..10df81e0 100644 --- a/tako-rs-server/src/server_udp.rs +++ b/tako-rs-server/src/server_udp.rs @@ -28,6 +28,8 @@ use futures_util::FutureExt; #[cfg(feature = "compio")] use futures_util::StreamExt; +use crate::ServerConfig; + /// Runs `handler` for every datagram until a receive error, which it returns. /// /// # Examples @@ -54,30 +56,13 @@ where + Sync + 'static, { - let socket = Arc::new(tokio::net::UdpSocket::bind(addr).await?); - tracing::info!("UDP server listening on {}", socket.local_addr()?); - - #[cfg(feature = "signals")] - tako_rs_core::signals::transport::emit_server_started( - &socket.local_addr()?.to_string(), - "udp", - false, + serve_udp_with_config( + addr, + handler, + std::future::pending(), + ServerConfig::default(), ) - .await; - - let handler = Arc::new(handler); - let mut buf = vec![0u8; 65535]; - - loop { - let (len, peer) = socket.recv_from(&mut buf).await?; - let data = buf[..len].to_vec(); - let socket = Arc::clone(&socket); - let handler = Arc::clone(&handler); - - tokio::spawn(async move { - handler(data, peer, socket).await; - }); - } + .await } /// Stops receiving once `signal` resolves and gives running handlers up to 30 seconds. @@ -90,8 +75,28 @@ where + 'static, S: Future + Send + 'static, { + serve_udp_with_config(addr, handler, signal, ServerConfig::default()).await +} + +/// Drops datagrams received at the configured handler cap. +#[cfg(not(feature = "compio"))] +pub async fn serve_udp_with_config( + addr: &str, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn(Vec, SocketAddr, Arc) -> Pin + Send>> + + Send + + Sync + + 'static, + S: Future + Send + 'static, +{ + config.validate()?; + let socket = tokio::net::UdpSocket::bind(addr).await?; - run_socket(socket, handler, signal, std::time::Duration::from_secs(30)).await + run_socket(socket, handler, signal, config).await } #[cfg(not(feature = "compio"))] @@ -99,7 +104,7 @@ pub(crate) async fn run_socket( socket: tokio::net::UdpSocket, handler: F, signal: S, - drain_timeout: std::time::Duration, + config: ServerConfig, ) -> std::io::Result<()> where F: Fn(Vec, SocketAddr, Arc) -> Pin + Send>> @@ -108,6 +113,8 @@ where + 'static, S: Future + Send + 'static, { + config.validate()?; + let socket = Arc::new(socket); let mut tasks = tokio::task::JoinSet::new(); tracing::info!("UDP server listening on {}", socket.local_addr()?); @@ -127,8 +134,18 @@ where loop { tokio::select! { + biased; + () = &mut signal => { + tracing::info!("UDP server shutting down"); + break; + } + _ = tasks.join_next(), if !tasks.is_empty() => {} result = socket.recv_from(&mut buf) => { let (len, peer) = result?; + if config.max_connections.is_some_and(|max| tasks.len() >= max) { + continue; + } + let data = buf[..len].to_vec(); let socket = Arc::clone(&socket); let handler = Arc::clone(&handler); @@ -136,16 +153,11 @@ where tasks.spawn(async move { handler(data, peer, socket).await; }); - while tasks.try_join_next().is_some() {} - } - () = &mut signal => { - tracing::info!("UDP server shutting down"); - break; } } } - if tokio::time::timeout(drain_timeout, async { + if tokio::time::timeout(config.drain_timeout, async { while tasks.join_next().await.is_some() {} }) .await @@ -153,6 +165,7 @@ where { tasks.shutdown().await; } + #[cfg(feature = "signals")] tako_rs_core::signals::transport::emit_server_stopped( &socket.local_addr()?.to_string(), @@ -172,34 +185,13 @@ where + Sync + 'static, { - // compio is thread-per-core; the Arc never crosses threads. - #[allow(clippy::arc_with_non_send_sync)] - let socket = Arc::new(compio::net::UdpSocket::bind(addr).await?); - tracing::info!("UDP server listening on {}", socket.local_addr()?); - - #[cfg(feature = "signals")] - tako_rs_core::signals::transport::emit_server_started( - &socket.local_addr()?.to_string(), - "udp", - false, + serve_udp_with_config( + addr, + handler, + std::future::pending(), + ServerConfig::default(), ) - .await; - - let handler = Arc::new(handler); - - loop { - let buf = vec![0u8; 65535]; - let compio::BufResult(result, mut buf) = socket.recv_from(buf).await; - let (len, peer) = result?; - buf.truncate(len); - let socket = Arc::clone(&socket); - let handler = Arc::clone(&handler); - - compio::runtime::spawn(async move { - handler(buf, peer, socket).await; - }) - .detach(); - } + .await } /// Stops receiving once `signal` resolves and gives running handlers up to 30 seconds. @@ -212,12 +204,53 @@ where + 'static, S: Future + 'static, { + serve_udp_with_config(addr, handler, signal, ServerConfig::default()).await +} + +/// Drops datagrams received at the configured handler cap. +#[cfg(feature = "compio")] +pub async fn serve_udp_with_config( + addr: &str, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn(Vec, SocketAddr, Arc) -> Pin>> + + Send + + Sync + + 'static, + S: Future + 'static, +{ + config.validate()?; + + let socket = compio::net::UdpSocket::bind(addr).await?; + run_socket(socket, handler, signal, config).await +} + +#[cfg(feature = "compio")] +pub(crate) async fn run_socket( + socket: compio::net::UdpSocket, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn(Vec, SocketAddr, Arc) -> Pin>> + + Send + + Sync + + 'static, + S: Future + 'static, +{ + config.validate()?; + // compio is thread-per-core; the Arc never crosses threads. #[allow(clippy::arc_with_non_send_sync)] - let socket = Arc::new(compio::net::UdpSocket::bind(addr).await?); + let socket = Arc::new(socket); tracing::info!("UDP server listening on {}", socket.local_addr()?); let mut tasks = futures_util::stream::FuturesUnordered::new(); + #[cfg(feature = "signals")] tako_rs_core::signals::transport::emit_server_started( &socket.local_addr()?.to_string(), @@ -228,17 +261,24 @@ where let handler = Arc::new(handler); let signal = std::pin::pin!(signal); - let mut signal = signal; loop { + while tasks.next().now_or_never().flatten().is_some() {} + let buf = vec![0u8; 65535]; let recv_fut = socket.recv_from(buf); - let recv_fut = std::pin::pin!(recv_fut); - match futures_util::future::select(recv_fut, &mut signal).await { - futures_util::future::Either::Left((compio::BufResult(result, buf_out), _)) => { + + match futures_util::future::select(&mut signal, recv_fut).await { + futures_util::future::Either::Right((compio::BufResult(result, buf_out), _)) => { let (len, peer) = result?; + while tasks.next().now_or_never().flatten().is_some() {} + + if config.max_connections.is_some_and(|max| tasks.len() >= max) { + continue; + } + let mut buf = buf_out; buf.truncate(len); let socket = Arc::clone(&socket); @@ -247,16 +287,15 @@ where tasks.push(compio::runtime::spawn(async move { handler(buf, peer, socket).await; })); - while tasks.next().now_or_never().flatten().is_some() {} } - futures_util::future::Either::Right(_) => { + futures_util::future::Either::Left(_) => { tracing::info!("UDP server shutting down"); break; } } } - if compio::time::timeout(std::time::Duration::from_secs(30), async { + if compio::time::timeout(config.drain_timeout, async { while tasks.next().await.is_some() {} }) .await @@ -266,6 +305,7 @@ where task.cancel().await; } } + #[cfg(feature = "signals")] tako_rs_core::signals::transport::emit_server_stopped( &socket.local_addr()?.to_string(), diff --git a/tako-rs-server/src/server_unix.rs b/tako-rs-server/src/server_unix.rs index d8ebc1dc..cabf4bd2 100644 --- a/tako-rs-server/src/server_unix.rs +++ b/tako-rs-server/src/server_unix.rs @@ -67,15 +67,23 @@ mod raw; #[cfg(feature = "compio")] mod raw_compio; +#[cfg(not(feature = "compio"))] +pub(crate) use raw::run_listener; #[cfg(not(feature = "compio"))] pub use raw::serve_unix; #[cfg(not(feature = "compio"))] +pub use raw::serve_unix_with_config; +#[cfg(not(feature = "compio"))] pub use raw::serve_unix_with_shutdown; #[cfg(not(feature = "compio"))] pub use raw::serve_unix_with_shutdown_and_drain; #[cfg(feature = "compio")] +pub(crate) use raw_compio::run_listener; +#[cfg(feature = "compio")] pub use raw_compio::serve_unix; #[cfg(feature = "compio")] +pub use raw_compio::serve_unix_with_config; +#[cfg(feature = "compio")] pub use raw_compio::serve_unix_with_shutdown; #[cfg(feature = "compio")] pub use raw_compio::serve_unix_with_shutdown_and_drain; diff --git a/tako-rs-server/src/server_unix/http.rs b/tako-rs-server/src/server_unix/http.rs index f01d5734..30686fb7 100644 --- a/tako-rs-server/src/server_unix/http.rs +++ b/tako-rs-server/src/server_unix/http.rs @@ -13,6 +13,7 @@ use tako_rs_core::conn_info::UnixPeerAddr; use tako_rs_core::router::Router; use tako_rs_core::server_support::ConnectionTimer; use tako_rs_core::server_support::Leased; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::connection_router; use tako_rs_core::server_support::drive_connection; #[cfg(feature = "signals")] @@ -29,9 +30,12 @@ pub(crate) async fn run_http( router: Router, signal: impl Future + Send + 'static, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { + config.validate()?; + let listener = bind_unix_listener(path).await?; - run_listener(listener, path, router, signal, config).await + run_listener(listener, path, router, signal, config, upgrades).await } pub(crate) async fn run_listener( @@ -40,7 +44,10 @@ pub(crate) async fn run_listener( router: Router, signal: impl Future + Send + 'static, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { + config.validate()?; + let router = Arc::new(router); #[cfg(feature = "plugins")] @@ -101,8 +108,10 @@ pub(crate) async fn run_listener( }; let conn_cancel = cancel.clone(); + let registry = upgrades.registry(); join_set.spawn(async move { let svc = service_fn(move |mut req| { + registry.attach(&mut req); let router = router.clone(); let peer_addr = peer_addr.clone(); async move { @@ -137,8 +146,10 @@ pub(crate) async fn run_listener( } } + upgrades.trigger(); let drain = tokio::time::timeout(drain_timeout, async { while join_set.join_next().await.is_some() {} + upgrades.join().await; }); if drain.await.is_err() { @@ -146,7 +157,9 @@ pub(crate) async fn run_listener( "Drain timeout exceeded, aborting {} remaining connections", join_set.len() ); + upgrades.abort(); join_set.shutdown().await; + upgrades.join().await; } if !is_abstract_path(path) { diff --git a/tako-rs-server/src/server_unix/raw.rs b/tako-rs-server/src/server_unix/raw.rs index 9540cfbd..ad1a3f46 100644 --- a/tako-rs-server/src/server_unix/raw.rs +++ b/tako-rs-server/src/server_unix/raw.rs @@ -9,6 +9,7 @@ use std::time::Duration; use tokio::task::JoinSet; use super::listener::bind_unix_listener; +use crate::ServerConfig; /// Runs `handler` for every accepted connection until an accept error, which it returns. pub async fn serve_unix(path: impl AsRef, handler: F) -> std::io::Result<()> @@ -21,26 +22,13 @@ where + Sync + 'static, { - let path = path.as_ref(); - let listener = bind_unix_listener(path).await?; - tracing::info!("Unix socket server listening on {}", path.display()); - - #[cfg(feature = "signals")] - tako_rs_core::signals::transport::emit_server_started(&path.to_string_lossy(), "unix", false) - .await; - - let handler = Arc::new(handler); - - loop { - let (stream, addr) = listener.accept().await?; - let handler = Arc::clone(&handler); - - tokio::spawn(async move { - if let Err(e) = handler(stream, addr).await { - tracing::error!("Unix socket connection error: {e}"); - } - }); - } + serve_unix_with_config( + path, + handler, + std::future::pending(), + ServerConfig::default(), + ) + .await } /// Stops accepting once `signal` resolves and drains connections for up to 30 seconds; use @@ -80,8 +68,61 @@ where + 'static, S: Future + Send + 'static, { + serve_unix_with_config( + path, + handler, + signal, + ServerConfig { + drain_timeout, + ..ServerConfig::default() + }, + ) + .await +} + +/// Enforces the configured connection limit and drain timeout. +pub async fn serve_unix_with_config( + path: impl AsRef, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn( + tokio::net::UnixStream, + tokio::net::unix::SocketAddr, + ) -> Pin> + Send>> + + Send + + Sync + + 'static, + S: Future + Send + 'static, +{ + config.validate()?; + let path = path.as_ref(); let listener = bind_unix_listener(path).await?; + run_listener(listener, path, handler, signal, config).await +} + +pub(crate) async fn run_listener( + listener: tokio::net::UnixListener, + path: &Path, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn( + tokio::net::UnixStream, + tokio::net::unix::SocketAddr, + ) -> Pin> + Send>> + + Send + + Sync + + 'static, + S: Future + Send + 'static, +{ + config.validate()?; + tracing::info!("Unix socket server listening on {}", path.display()); #[cfg(feature = "signals")] @@ -95,7 +136,13 @@ where loop { tokio::select! { - result = listener.accept() => { + biased; + () = &mut signal => { + tracing::info!("Unix socket server shutting down, draining {} connections", join_set.len()); + break; + } + _ = join_set.join_next(), if !join_set.is_empty() => {} + result = listener.accept(), if config.max_connections.is_none_or(|max| join_set.len() < max) => { let (stream, addr) = result?; let handler = Arc::clone(&handler); @@ -104,16 +151,11 @@ where tracing::error!("Unix socket connection error: {e}"); } }); - while join_set.try_join_next().is_some() {} - } - () = &mut signal => { - tracing::info!("Unix socket server shutting down, draining {} connections", join_set.len()); - break; } } } - if tokio::time::timeout(drain_timeout, async { + if tokio::time::timeout(config.drain_timeout, async { while join_set.join_next().await.is_some() {} }) .await diff --git a/tako-rs-server/src/server_unix/raw_compio.rs b/tako-rs-server/src/server_unix/raw_compio.rs index 12fccd7e..057555fb 100644 --- a/tako-rs-server/src/server_unix/raw_compio.rs +++ b/tako-rs-server/src/server_unix/raw_compio.rs @@ -12,6 +12,7 @@ use futures_util::future::Either; use futures_util::stream::FuturesUnordered; use tako_rs_core::conn_info::UnixPeerAddr; +use crate::ServerConfig; use crate::server_compio::unix::bind_listener; use crate::server_compio::unix::peer_addr; @@ -23,28 +24,13 @@ where + Sync + 'static, { - let path = path.as_ref(); - let listener = bind_listener(path).await?; - tracing::info!("Unix socket server listening on {}", path.display()); - - #[cfg(feature = "signals")] - tako_rs_core::signals::transport::emit_server_started(&path.to_string_lossy(), "unix", false) - .await; - - let handler = Arc::new(handler); - - loop { - let (stream, addr) = listener.accept().await?; - let peer = peer_addr(addr.as_pathname()); - let handler = Arc::clone(&handler); - - compio::runtime::spawn(async move { - if let Err(e) = handler(stream, peer).await { - tracing::error!("Unix socket connection error: {e}"); - } - }) - .detach(); - } + serve_unix_with_config( + path, + handler, + std::future::pending(), + ServerConfig::default(), + ) + .await } /// Stops accepting once `signal` resolves and drains connections for up to 30 seconds; use @@ -78,8 +64,55 @@ where + 'static, S: Future + 'static, { + serve_unix_with_config( + path, + handler, + signal, + ServerConfig { + drain_timeout, + ..ServerConfig::default() + }, + ) + .await +} + +/// Enforces the configured connection limit and drain timeout. +pub async fn serve_unix_with_config( + path: impl AsRef, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn(compio::net::UnixStream, UnixPeerAddr) -> Pin>>> + + Send + + Sync + + 'static, + S: Future + 'static, +{ + config.validate()?; + let path = path.as_ref(); let listener = bind_listener(path).await?; + run_listener(listener, path, handler, signal, config).await +} + +pub(crate) async fn run_listener( + listener: compio::net::UnixListener, + path: &Path, + handler: F, + signal: S, + config: ServerConfig, +) -> std::io::Result<()> +where + F: Fn(compio::net::UnixStream, UnixPeerAddr) -> Pin>>> + + Send + + Sync + + 'static, + S: Future + 'static, +{ + config.validate()?; + tracing::info!("Unix socket server listening on {}", path.display()); #[cfg(feature = "signals")] @@ -91,20 +124,34 @@ where let mut signal = std::pin::pin!(signal); loop { + while connections.next().now_or_never().flatten().is_some() {} + + if config + .max_connections + .is_some_and(|max| connections.len() >= max) + { + let completed = std::pin::pin!(connections.next()); + match futures_util::future::select(&mut signal, completed).await { + Either::Left(_) => break, + Either::Right(_) => continue, + } + } + let accept = std::pin::pin!(listener.accept()); - match futures_util::future::select(accept, &mut signal).await { - Either::Left((result, _)) => { + + match futures_util::future::select(&mut signal, accept).await { + Either::Right((result, _)) => { let (stream, addr) = result?; let peer = peer_addr(addr.as_pathname()); let handler = Arc::clone(&handler); + connections.push(compio::runtime::spawn(async move { if let Err(e) = handler(stream, peer).await { tracing::error!("Unix socket connection error: {e}"); } })); - while connections.next().now_or_never().flatten().is_some() {} } - Either::Right(_) => { + Either::Left(_) => { tracing::info!( "Unix socket server shutting down, draining {} connections", connections.len() @@ -114,7 +161,7 @@ where } } - if compio::time::timeout(drain_timeout, async { + if compio::time::timeout(config.drain_timeout, async { while connections.next().await.is_some() {} }) .await diff --git a/tako-rs-server/src/server_vsock.rs b/tako-rs-server/src/server_vsock.rs index 80687101..20fbe200 100644 --- a/tako-rs-server/src/server_vsock.rs +++ b/tako-rs-server/src/server_vsock.rs @@ -13,6 +13,7 @@ use tako_rs_core::conn_info::Transport; use tako_rs_core::router::Router; use tako_rs_core::server_support::ConnectionTimer; use tako_rs_core::server_support::Leased; +use tako_rs_core::server_support::UpgradeSupervisor; use tako_rs_core::server_support::connection_router; use tako_rs_core::server_support::drive_connection; use tako_rs_core::types::BoxError; @@ -29,7 +30,10 @@ pub(crate) async fn run_listener( router: Router, signal: impl Future + Send + 'static, config: ServerConfig, + upgrades: UpgradeSupervisor, ) -> Result<(), BoxError> { + config.validate()?; + let router = Arc::new(router); #[cfg(feature = "plugins")] @@ -85,9 +89,11 @@ pub(crate) async fn run_listener( let router = connection_router(&router); let conn_cancel = cancel.clone(); + let registry = upgrades.registry(); join_set.spawn(async move { let peer_label = format!("vsock:{}:{}", peer.cid(), peer.port()); let svc = service_fn(move |mut req| { + registry.attach(&mut req); let router = router.clone(); let peer_label = peer_label.clone(); async move { @@ -124,8 +130,10 @@ pub(crate) async fn run_listener( } } + upgrades.trigger(); let drain = tokio::time::timeout(drain_timeout, async { while join_set.join_next().await.is_some() {} + upgrades.join().await; }); if drain.await.is_err() { tracing::warn!( @@ -133,7 +141,9 @@ pub(crate) async fn run_listener( drain_timeout, join_set.len() ); + upgrades.abort(); join_set.shutdown().await; + upgrades.join().await; } tracing::info!("vsock HTTP server shut down gracefully"); diff --git a/tako-rs-server/tests/compio_raw_limits.rs b/tako-rs-server/tests/compio_raw_limits.rs new file mode 100644 index 00000000..5e2b82ab --- /dev/null +++ b/tako-rs-server/tests/compio_raw_limits.rs @@ -0,0 +1,165 @@ +#![cfg(feature = "compio")] + +use std::sync::Arc; +use std::time::Duration; + +use compio::io::AsyncRead; +use tako_rs_server::CompioServer; +use tako_rs_server::ServerConfig; + +#[test] +fn compio_raw_tcp_waits_for_capacity_and_shutdown_cancels_the_active_handler() { + compio::runtime::Runtime::new().unwrap().block_on(async { + let (started, mut received) = tokio::sync::mpsc::unbounded_channel(); + let release = Arc::new(tokio::sync::Semaphore::new(0)); + let handler_release = release.clone(); + let handle = limited_server() + .try_spawn_tcp_raw("127.0.0.1:0", move |stream, _| { + let started = started.clone(); + let release = handler_release.clone(); + Box::pin(async move { + started.send(()).unwrap(); + release.acquire().await.unwrap().forget(); + drop(stream); + Ok(()) + }) + }) + .unwrap(); + let address = handle.local_addr().unwrap(); + + let _first = std::net::TcpStream::connect(address).unwrap(); + receive(&mut received).await; + + let mut second = compio::net::TcpStream::connect(address).await.unwrap(); + assert!( + compio::time::timeout(Duration::from_millis(75), received.recv()) + .await + .is_err() + ); + + release.add_permits(1); + receive(&mut received).await; + + handle.trigger(); + compio::time::timeout(Duration::from_secs(1), handle.result()) + .await + .unwrap() + .unwrap(); + let compio::BufResult(result, _) = + compio::time::timeout(Duration::from_secs(1), second.read(vec![0])) + .await + .unwrap(); + assert_eq!(result.unwrap(), 0); + }); +} + +#[cfg(unix)] +#[test] +fn compio_raw_unix_waits_for_capacity_and_shutdown_cancels_the_active_handler() { + compio::runtime::Runtime::new().unwrap().block_on(async { + let path = std::env::temp_dir().join(format!("tako-compio-limit-{}.sock", std::process::id())); + let (started, mut received) = tokio::sync::mpsc::unbounded_channel(); + let release = Arc::new(tokio::sync::Semaphore::new(0)); + let handler_release = release.clone(); + let handle = limited_server() + .try_spawn_unix_raw(&path, move |stream, _| { + let started = started.clone(); + let release = handler_release.clone(); + Box::pin(async move { + started.send(()).unwrap(); + release.acquire().await.unwrap().forget(); + drop(stream); + Ok(()) + }) + }) + .await + .unwrap(); + + let _first = std::os::unix::net::UnixStream::connect(&path).unwrap(); + receive(&mut received).await; + + let mut second = compio::net::UnixStream::connect(&path).await.unwrap(); + assert!( + compio::time::timeout(Duration::from_millis(75), received.recv()) + .await + .is_err() + ); + + release.add_permits(1); + receive(&mut received).await; + + handle.trigger(); + compio::time::timeout(Duration::from_secs(1), handle.result()) + .await + .unwrap() + .unwrap(); + let compio::BufResult(result, _) = + compio::time::timeout(Duration::from_secs(1), second.read(vec![0])) + .await + .unwrap(); + assert_eq!(result.unwrap(), 0); + std::fs::remove_file(path).unwrap(); + }); +} + +#[cfg(feature = "udp")] +#[test] +fn compio_raw_udp_drops_excess_datagrams_and_resumes_after_capacity_returns() { + compio::runtime::Runtime::new().unwrap().block_on(async { + let (started, mut received) = tokio::sync::mpsc::unbounded_channel(); + let release = Arc::new(tokio::sync::Semaphore::new(0)); + let handler_release = release.clone(); + let handle = limited_server() + .try_spawn_udp_raw("127.0.0.1:0", move |data, _, _| { + let started = started.clone(); + let release = handler_release.clone(); + Box::pin(async move { + started.send(data).unwrap(); + release.acquire().await.unwrap().forget(); + }) + }) + .unwrap(); + let address = handle.local_addr().unwrap(); + let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap(); + + socket.send_to(b"first", address).unwrap(); + assert_eq!(receive(&mut received).await, b"first"); + + for _ in 0..32 { + socket.send_to(b"excess", address).unwrap(); + } + assert!( + compio::time::timeout(Duration::from_millis(75), received.recv()) + .await + .is_err() + ); + + release.add_permits(1); + compio::time::sleep(Duration::from_millis(20)).await; + socket.send_to(b"after", address).unwrap(); + assert_eq!(receive(&mut received).await, b"after"); + + handle.trigger(); + compio::time::timeout(Duration::from_secs(1), handle.result()) + .await + .unwrap() + .unwrap(); + }); +} + +fn limited_server() -> CompioServer { + CompioServer::builder() + .config(ServerConfig { + max_connections: Some(1), + drain_timeout: Duration::from_millis(20), + ..ServerConfig::default() + }) + .build() +} + +async fn receive(receiver: &mut tokio::sync::mpsc::UnboundedReceiver) -> T { + compio::time::timeout(Duration::from_secs(1), receiver.recv()) + .await + .unwrap() + .unwrap() +} diff --git a/tako-rs-server/tests/config_api.rs b/tako-rs-server/tests/config_api.rs new file mode 100644 index 00000000..309e33d0 --- /dev/null +++ b/tako-rs-server/tests/config_api.rs @@ -0,0 +1,145 @@ +use std::time::Duration; + +#[cfg(feature = "compio")] +use tako_rs_server::CompioServer as Server; +#[cfg(not(feature = "compio"))] +use tako_rs_server::Server; +use tako_rs_server::ServerConfig; +use tako_rs_server::ServerConfigError; + +#[test] +fn fallible_builder_reports_the_invalid_field_without_a_runtime() { + for limit in [0, tokio::sync::Semaphore::MAX_PERMITS + 1] { + let error = Server::builder() + .config(ServerConfig { + max_connections: Some(limit), + ..ServerConfig::default() + }) + .try_build() + .unwrap_err(); + + assert_eq!(error.field(), "max_connections"); + } + + Server::builder().try_build().unwrap(); +} + +#[test] +fn raw_startup_returns_typed_validation_errors_before_binding() { + let error = Server::builder() + .config(ServerConfig { + max_connections: Some(0), + ..ServerConfig::default() + }) + .build() + .try_spawn_tcp_raw("invalid socket address", |_, _| Box::pin(async { Ok(()) })) + .err() + .unwrap(); + + assert_eq!( + error.downcast_ref::().unwrap().field(), + "max_connections" + ); +} + +#[test] +fn supported_limit_boundaries_and_immediate_timeout_policies_remain_valid() { + let config = ServerConfig { + drain_timeout: Duration::ZERO, + header_read_timeout: Some(Duration::ZERO), + h2_max_concurrent_streams: 1, + h2_max_send_buf_size: u32::MAX as usize, + h2_max_pending_accept_reset_streams: 0, + h2_keep_alive_interval: Some(Duration::ZERO), + h3_max_concurrent_bidi_streams: 1, + h3_max_concurrent_uni_streams: 3, + h3_max_idle_timeout: Some(Duration::ZERO), + h3_goaway_grace: Duration::ZERO, + max_connections: Some(tokio::sync::Semaphore::MAX_PERMITS), + proxy_read_timeout: Duration::ZERO, + tls_handshake_timeout: Duration::ZERO, + ..ServerConfig::default() + }; + + config.validate().unwrap(); + Server::builder().config(config).try_build().unwrap(); +} + +#[test] +fn explicit_protocol_throttling_policies_remain_valid() { + for uni_streams in [0, 1, 2] { + let config = ServerConfig { + h2_max_concurrent_streams: 0, + h2_max_header_list_size: 0, + h2_max_send_buf_size: 0, + h3_max_concurrent_bidi_streams: 0, + h3_max_concurrent_uni_streams: uni_streams, + ..ServerConfig::default() + }; + + config.validate().unwrap(); + Server::builder().config(config).try_build().unwrap(); + } +} + +#[cfg(target_pointer_width = "64")] +#[test] +fn oversized_http2_send_buffer_is_rejected_before_hyper_can_panic() { + let error = ServerConfig { + h2_max_send_buf_size: u32::MAX as usize + 1, + ..ServerConfig::default() + } + .validate() + .unwrap_err(); + + assert_eq!(error.field(), "h2_max_send_buf_size"); +} + +#[test] +fn overflowing_deadlines_are_rejected_with_the_field_name() { + let cases = [ + ( + "header_read_timeout", + ServerConfig { + header_read_timeout: Some(Duration::MAX), + ..ServerConfig::default() + }, + ), + ( + "h2_keep_alive_interval", + ServerConfig { + h2_keep_alive_interval: Some(Duration::MAX), + ..ServerConfig::default() + }, + ), + ( + "h3_goaway_grace", + ServerConfig { + h3_goaway_grace: Duration::MAX, + ..ServerConfig::default() + }, + ), + ( + "proxy_read_timeout", + ServerConfig { + proxy_read_timeout: Duration::MAX, + ..ServerConfig::default() + }, + ), + ( + "tls_handshake_timeout", + ServerConfig { + tls_handshake_timeout: Duration::MAX, + ..ServerConfig::default() + }, + ), + ]; + + for (field, config) in cases { + let error = config.validate().unwrap_err(); + assert_eq!(error.field(), field); + + let io_error = std::io::Error::from(error); + assert_eq!(io_error.kind(), std::io::ErrorKind::InvalidInput); + } +} diff --git a/tako-rs-server/tests/config_startup.rs b/tako-rs-server/tests/config_startup.rs new file mode 100644 index 00000000..451d814c --- /dev/null +++ b/tako-rs-server/tests/config_startup.rs @@ -0,0 +1,69 @@ +#![cfg(feature = "plugins")] + +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; + +#[cfg(feature = "compio")] +use compio::net::TcpListener; +use tako_rs_core::plugins::TakoPlugin; +use tako_rs_core::router::Router; +#[cfg(feature = "compio")] +use tako_rs_server::CompioServer as Server; +#[cfg(not(feature = "compio"))] +use tako_rs_server::Server; +use tako_rs_server::ServerConfig; +use tako_rs_server::ServerConfigError; +#[cfg(not(feature = "compio"))] +use tokio::net::TcpListener; + +#[derive(Clone)] +struct StartupPlugin(Arc); + +impl TakoPlugin for StartupPlugin { + fn name(&self) -> &'static str { + "startup-state" + } + + fn setup(&self, router: &Router) -> anyhow::Result<()> { + self.0.store(true, Ordering::SeqCst); + router.middleware(|request, next| async move { next.run(request).await }); + Ok(()) + } +} + +async fn check_startup() { + let initialized = Arc::new(AtomicBool::new(false)); + let mut router = Router::new(); + router.plugin(StartupPlugin(initialized.clone())); + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let error = Server::builder() + .config(ServerConfig { + max_connections: Some(0), + ..ServerConfig::default() + }) + .build() + .try_spawn_http(listener, router) + .err() + .unwrap(); + + assert_eq!( + error.downcast_ref::().unwrap().field(), + "max_connections" + ); + assert!(!initialized.load(Ordering::SeqCst)); +} + +#[cfg(not(feature = "compio"))] +#[tokio::test] +async fn invalid_configuration_does_not_initialize_plugins() { + check_startup().await; +} + +#[cfg(feature = "compio")] +#[test] +fn invalid_configuration_does_not_initialize_plugins() { + compio::runtime::Runtime::new() + .unwrap() + .block_on(check_startup()); +} diff --git a/tako-rs-server/tests/config_validation.rs b/tako-rs-server/tests/config_validation.rs new file mode 100644 index 00000000..e52d7fad --- /dev/null +++ b/tako-rs-server/tests/config_validation.rs @@ -0,0 +1,71 @@ +#![cfg(not(feature = "compio"))] + +use tako_rs_core::router::Router; +use tako_rs_server::Server; +use tako_rs_server::ServerConfig; + +#[tokio::test] +async fn unrepresentable_timeouts_are_rejected_before_http_startup() { + let cases = [ + ( + "h3_max_idle_timeout", + ServerConfig { + h3_max_idle_timeout: Some(std::time::Duration::MAX), + ..ServerConfig::default() + }, + ), + ( + "drain_timeout", + ServerConfig { + drain_timeout: std::time::Duration::MAX, + ..ServerConfig::default() + }, + ), + ]; + + for (field, config) in cases { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let result = Server::builder() + .config(config) + .build() + .try_spawn_http(listener, Router::new()); + + assert!(result.is_err(), "invalid {field} started HTTP"); + } +} + +#[tokio::test] +async fn invalid_connection_limit_is_rejected_before_http_startup() { + for limit in [0, tokio::sync::Semaphore::MAX_PERMITS + 1] { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let result = Server::builder() + .config(ServerConfig { + max_connections: Some(limit), + ..ServerConfig::default() + }) + .build() + .try_spawn_http(listener, Router::new()); + + assert!( + result.is_err(), + "invalid max_connections {limit} started HTTP" + ); + } +} + +#[test] +fn invalid_configuration_is_rejected_before_raw_tcp_binding() { + let result = Server::builder() + .config(ServerConfig { + max_connections: Some(0), + ..ServerConfig::default() + }) + .build() + .try_spawn_tcp_raw("invalid socket address", |_, _| Box::pin(async { Ok(()) })); + let error = result.expect_err("invalid configuration must fail"); + + assert!( + error.to_string().contains("max_connections"), + "configuration must be checked before attempting to bind: {error}" + ); +} diff --git a/tako-rs-server/tests/raw_limits.rs b/tako-rs-server/tests/raw_limits.rs new file mode 100644 index 00000000..75ea1575 --- /dev/null +++ b/tako-rs-server/tests/raw_limits.rs @@ -0,0 +1,125 @@ +#![cfg(not(feature = "compio"))] + +use std::sync::Arc; +use std::time::Duration; + +use tako_rs_server::Server; +use tako_rs_server::ServerConfig; +use tokio::io::AsyncReadExt; +use tokio::net::TcpStream; + +#[tokio::test] +async fn raw_tcp_waits_for_capacity_and_shutdown_cancels_the_active_handler() { + let (started, mut received) = tokio::sync::mpsc::unbounded_channel(); + let release = Arc::new(tokio::sync::Semaphore::new(0)); + let handler_release = release.clone(); + let handle = Server::builder() + .config(ServerConfig { + max_connections: Some(1), + drain_timeout: Duration::from_millis(20), + ..ServerConfig::default() + }) + .build() + .try_spawn_tcp_raw("127.0.0.1:0", move |stream, _| { + let started = started.clone(); + let release = handler_release.clone(); + Box::pin(async move { + started.send(()).unwrap(); + release.acquire().await.unwrap().forget(); + drop(stream); + Ok(()) + }) + }) + .unwrap(); + let address = handle.local_addr().unwrap(); + + let _first = TcpStream::connect(address).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), received.recv()) + .await + .unwrap() + .unwrap(); + + let mut second = TcpStream::connect(address).await.unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(75), received.recv()) + .await + .is_err(), + "a second raw TCP handler started while max_connections was exhausted" + ); + + release.add_permits(1); + tokio::time::timeout(Duration::from_secs(1), received.recv()) + .await + .unwrap() + .unwrap(); + + handle.trigger(); + tokio::time::timeout(Duration::from_secs(1), handle.result()) + .await + .unwrap() + .unwrap(); + assert_eq!(second.read(&mut [0]).await.unwrap(), 0); +} + +#[cfg(feature = "udp")] +#[tokio::test] +async fn raw_udp_drops_excess_datagrams_and_resumes_after_capacity_returns() { + let (started, mut received) = tokio::sync::mpsc::unbounded_channel(); + let release = Arc::new(tokio::sync::Semaphore::new(0)); + let handler_release = release.clone(); + let handle = Server::builder() + .config(ServerConfig { + max_connections: Some(1), + drain_timeout: Duration::from_millis(20), + ..ServerConfig::default() + }) + .build() + .try_spawn_udp_raw("127.0.0.1:0", move |data, _, _| { + let started = started.clone(); + let release = handler_release.clone(); + Box::pin(async move { + started.send(data).unwrap(); + release.acquire().await.unwrap().forget(); + }) + }) + .unwrap(); + let socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap(); + let address = handle.local_addr().unwrap(); + + socket.send_to(b"first", address).await.unwrap(); + assert_eq!( + tokio::time::timeout(Duration::from_secs(1), received.recv()) + .await + .unwrap() + .unwrap(), + b"first" + ); + + for _ in 0..32 { + socket.send_to(b"excess", address).await.unwrap(); + } + assert!( + tokio::time::timeout(Duration::from_millis(75), received.recv()) + .await + .is_err(), + "UDP started excess handlers instead of dropping datagrams" + ); + + release.add_permits(1); + tokio::time::sleep(Duration::from_millis(20)).await; + socket.send_to(b"after", address).await.unwrap(); + assert_eq!( + tokio::time::timeout(Duration::from_secs(1), received.recv()) + .await + .unwrap() + .unwrap(), + b"after", + "datagrams received at capacity must not be queued for later handlers" + ); + + handle.trigger(); + tokio::time::timeout(Duration::from_secs(1), handle.result()) + .await + .unwrap() + .unwrap(); +} diff --git a/tako-rs-server/tests/raw_unix_limits.rs b/tako-rs-server/tests/raw_unix_limits.rs new file mode 100644 index 00000000..f9a21512 --- /dev/null +++ b/tako-rs-server/tests/raw_unix_limits.rs @@ -0,0 +1,63 @@ +#![cfg(all(unix, not(feature = "compio")))] + +use std::sync::Arc; +use std::time::Duration; + +use tako_rs_server::Server; +use tako_rs_server::ServerConfig; +use tokio::io::AsyncReadExt; + +#[tokio::test] +async fn raw_unix_waits_for_capacity_and_shutdown_cancels_the_active_handler() { + let path = std::env::temp_dir().join(format!("tako-raw-limit-{}.sock", std::process::id())); + let (started, mut received) = tokio::sync::mpsc::unbounded_channel(); + let release = Arc::new(tokio::sync::Semaphore::new(0)); + let handler_release = release.clone(); + let handle = Server::builder() + .config(ServerConfig { + max_connections: Some(1), + drain_timeout: Duration::from_millis(20), + ..ServerConfig::default() + }) + .build() + .try_spawn_unix_raw(&path, move |stream, _| { + let started = started.clone(); + let release = handler_release.clone(); + Box::pin(async move { + started.send(()).unwrap(); + release.acquire().await.unwrap().forget(); + drop(stream); + Ok(()) + }) + }) + .await + .unwrap(); + + let _first = tokio::net::UnixStream::connect(&path).await.unwrap(); + tokio::time::timeout(Duration::from_secs(1), received.recv()) + .await + .unwrap() + .unwrap(); + + let mut second = tokio::net::UnixStream::connect(&path).await.unwrap(); + assert!( + tokio::time::timeout(Duration::from_millis(75), received.recv()) + .await + .is_err(), + "a second raw Unix handler started while max_connections was exhausted" + ); + + release.add_permits(1); + tokio::time::timeout(Duration::from_secs(1), received.recv()) + .await + .unwrap() + .unwrap(); + + handle.trigger(); + tokio::time::timeout(Duration::from_secs(1), handle.result()) + .await + .unwrap() + .unwrap(); + assert_eq!(second.read(&mut [0]).await.unwrap(), 0); + std::fs::remove_file(path).unwrap(); +} diff --git a/tako-rs/src/lib.rs b/tako-rs/src/lib.rs index 42873be1..de4743fc 100644 --- a/tako-rs/src/lib.rs +++ b/tako-rs/src/lib.rs @@ -76,6 +76,7 @@ pub use tako_rs_server::Server; #[cfg(not(feature = "compio"))] pub use tako_rs_server::ServerBuilder; pub use tako_rs_server::ServerConfig; +pub use tako_rs_server::ServerConfigError; pub use tako_rs_server::ServerError; pub use tako_rs_server::ServerHandle; pub use tako_rs_server::TlsCert; @@ -235,13 +236,31 @@ pub mod plugins { pub use tako_rs_server_pt::PerThreadConfig; #[cfg(feature = "per-thread")] #[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] +pub use tako_rs_server_pt::PerThreadError; +#[cfg(feature = "per-thread")] +#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] +pub use tako_rs_server_pt::PerThreadErrorKind; +#[cfg(feature = "per-thread")] +#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] +pub use tako_rs_server_pt::PerThreadHandle; +#[cfg(feature = "per-thread")] +#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] +pub use tako_rs_server_pt::PerThreadReadiness; +#[cfg(feature = "per-thread")] +#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] pub use tako_rs_server_pt::PerThreadShutdown; #[cfg(feature = "per-thread")] #[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] +pub use tako_rs_server_pt::PerThreadWorkerState; +#[cfg(feature = "per-thread")] +#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] pub use tako_rs_server_pt::serve_per_thread; #[cfg(feature = "per-thread")] #[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] pub use tako_rs_server_pt::spawn_per_thread; +#[cfg(feature = "per-thread")] +#[cfg_attr(docsrs, doc(cfg(feature = "per-thread")))] +pub use tako_rs_server_pt::spawn_per_thread_supervised; #[cfg(feature = "jemalloc")] #[cfg_attr(docsrs, doc(cfg(feature = "jemalloc")))] pub use tikv_jemallocator::Jemalloc; diff --git a/tako-rs/tests/fixtures/websocket-shutdown-cert.der b/tako-rs/tests/fixtures/websocket-shutdown-cert.der new file mode 100644 index 00000000..4994dab4 Binary files /dev/null and b/tako-rs/tests/fixtures/websocket-shutdown-cert.der differ diff --git a/tako-rs/tests/fixtures/websocket-shutdown-key.der b/tako-rs/tests/fixtures/websocket-shutdown-key.der new file mode 100644 index 00000000..7549756b Binary files /dev/null and b/tako-rs/tests/fixtures/websocket-shutdown-key.der differ diff --git a/tako-rs/tests/per_thread_shutdown.rs b/tako-rs/tests/per_thread_shutdown.rs new file mode 100644 index 00000000..1674d1fe --- /dev/null +++ b/tako-rs/tests/per_thread_shutdown.rs @@ -0,0 +1,52 @@ +#![cfg(all(feature = "per-thread", feature = "ws"))] + +use std::time::Duration; + +use tako::PerThreadConfig; +use tako::router::Router; +use tako::spawn_per_thread; +use tako::types::Request; +use tako::ws::WebSocketUpgrade; +use tokio::io::AsyncReadExt; +use tokio::io::AsyncWriteExt; + +#[tokio::test] +async fn worker_shutdown_sends_websocket_going_away_before_closing() { + let mut router = Router::new(); + router.get("/ws", |request: Request| async move { + WebSocketUpgrade::new(request, |mut websocket| async move { + while let Some(Ok(_)) = websocket.recv().await {} + }) + }); + let reservation = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let address = reservation.local_addr().unwrap(); + drop(reservation); + let (workers, shutdown) = spawn_per_thread( + &address.to_string(), + router, + PerThreadConfig { + workers: 1, + drain_timeout: Duration::from_millis(100), + ..PerThreadConfig::default() + }, + ) + .unwrap(); + shutdown.wait_for_bind_outcome(1).await.unwrap(); + + let mut client = tokio::net::TcpStream::connect(address).await.unwrap(); + client.write_all(b"GET /ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n").await.unwrap(); + let mut head = Vec::new(); + while !head.ends_with(b"\r\n\r\n") { + head.push(client.read_u8().await.unwrap()); + } + assert!(head.starts_with(b"HTTP/1.1 101")); + + shutdown.trigger(); + let mut frames = Vec::new(); + let closed = tokio::time::timeout(Duration::from_secs(2), client.read_to_end(&mut frames)).await; + for worker in workers { + worker.join().unwrap(); + } + closed.unwrap().unwrap(); + assert_eq!(frames, [0x88, 2, 0x03, 0xE9]); +} diff --git a/tako-rs/tests/per_thread_startup.rs b/tako-rs/tests/per_thread_startup.rs new file mode 100644 index 00000000..c577d010 --- /dev/null +++ b/tako-rs/tests/per_thread_startup.rs @@ -0,0 +1,106 @@ +#![cfg(all(feature = "per-thread", feature = "plugins", not(feature = "compio")))] + +use std::sync::Arc; +use std::sync::Condvar; +use std::sync::Mutex; +use std::time::Duration; + +use tako::PerThreadConfig; +use tako::PerThreadErrorKind; +use tako::plugins::TakoPlugin; +use tako::router::Router; +use tako::spawn_per_thread_supervised; + +#[derive(Clone, Default)] +struct BlockingCleanup(Arc<(Mutex, Condvar)>); + +impl BlockingCleanup { + fn wait(&self) { + let (released, wake) = self.0.as_ref(); + let _guard = wake + .wait_while(released.lock().unwrap(), |released| !*released) + .unwrap(); + } + + fn release(&self) { + let (released, wake) = self.0.as_ref(); + *released.lock().unwrap() = true; + wake.notify_all(); + } +} + +#[derive(Clone)] +struct FailingPlugin { + cleanup: BlockingCleanup, + panic: bool, +} + +impl TakoPlugin for FailingPlugin { + fn name(&self) -> &'static str { + "blocking-startup-failure" + } + + fn setup(&self, _: &Router) -> anyhow::Result<()> { + let cleanup = self.cleanup.clone(); + let (started, ready) = std::sync::mpsc::channel(); + let blocking = move || { + started.send(()).unwrap(); + cleanup.wait(); + }; + tokio::task::spawn_blocking(blocking); + ready.recv().unwrap(); + assert!(!self.panic, "plugin startup panic"); + Err(std::io::Error::other("plugin startup failed").into()) + } +} + +async fn assert_failure_precedes_cleanup(panic: bool, kind: PerThreadErrorKind) { + let cleanup = BlockingCleanup::default(); + let mut router = Router::new(); + router.plugin(FailingPlugin { + cleanup: cleanup.clone(), + panic, + }); + let server = spawn_per_thread_supervised( + "127.0.0.1:0", + router, + PerThreadConfig { + workers: 1, + ..PerThreadConfig::default() + }, + ) + .unwrap(); + let shutdown = server.shutdown_handle(); + let (ready, result, stopped) = tokio::join!( + tokio::time::timeout(Duration::from_secs(1), server.wait_ready()), + tokio::time::timeout(Duration::from_secs(1), server.result()), + tokio::time::timeout(Duration::from_secs(1), shutdown.notified()), + ); + cleanup.release(); + let joined = server.join(); + + assert!( + ready + .expect("readiness waited for runtime cleanup") + .is_err() + ); + assert_eq!( + result + .expect("failure waited for runtime cleanup") + .unwrap_err() + .kind(), + kind + ); + stopped.expect("peer cancellation waited for runtime cleanup"); + assert_eq!(joined.unwrap_err().kind(), kind); +} + +#[tokio::test] +async fn startup_error_is_reported_before_runtime_waits_for_blocking_tasks() { + assert_failure_precedes_cleanup(false, PerThreadErrorKind::Startup).await; +} + +#[tokio::test] +async fn startup_panic_is_reported_before_runtime_waits_for_blocking_tasks() { + assert_failure_precedes_cleanup(true, PerThreadErrorKind::Panic).await; +} diff --git a/tako-rs/tests/websocket_echo_compio.rs b/tako-rs/tests/websocket_echo_compio.rs index 7b594f82..b4b5b6b8 100644 --- a/tako-rs/tests/websocket_echo_compio.rs +++ b/tako-rs/tests/websocket_echo_compio.rs @@ -15,6 +15,7 @@ const UPGRADE: &[u8] = b"GET /ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websock Connection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\ Sec-WebSocket-Version: 13\r\n\r\n"; const MASK: [u8; 4] = [9, 8, 7, 6]; +const CLIENT_TIMEOUT: Duration = Duration::from_secs(5); fn router() -> Router { let mut router = Router::new(); @@ -77,9 +78,7 @@ fn read_exact(stream: &mut TcpStream, len: usize) -> Vec { fn connect(address: SocketAddr, path: &str) -> TcpStream { let mut stream = TcpStream::connect(address).unwrap(); - stream - .set_read_timeout(Some(Duration::from_secs(2))) - .unwrap(); + stream.set_read_timeout(Some(CLIENT_TIMEOUT)).unwrap(); let upgrade = String::from_utf8(UPGRADE.to_vec()) .unwrap() .replace("/ws", path); @@ -132,7 +131,11 @@ fn exercise_lifecycle(address: SocketAddr) { assert_eq!(read_exact(&mut stream, 7), b"\x81\x05hello"); } let [high, low] = code.to_be_bytes(); - assert_eq!(read_exact(&mut stream, 4), [0x88, 2, high, low], "{path}"); + let mut close = [0; 4]; + stream + .read_exact(&mut close) + .unwrap_or_else(|error| panic!("{path}: failed to read close frame: {error}")); + assert_eq!(close, [0x88, 2, high, low], "{path}"); let mut rest = Vec::new(); stream.read_to_end(&mut rest).unwrap(); assert!(rest.is_empty(), "{path}: {rest:?}"); diff --git a/tako-rs/tests/websocket_shutdown.rs b/tako-rs/tests/websocket_shutdown.rs new file mode 100644 index 00000000..9ffbf705 --- /dev/null +++ b/tako-rs/tests/websocket_shutdown.rs @@ -0,0 +1,190 @@ +#![cfg(feature = "ws")] + +use std::io::Read; +use std::io::Write; +use std::net::SocketAddr; +use std::net::TcpStream; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::time::Duration; + +use tako::ServerHandle; +use tako::router::Router; +use tako::types::Request; +use tako::ws::WebSocketUpgrade; +use tokio::sync::oneshot; + +const UPGRADE: &[u8] = b"GET /ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\n\ +Connection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\ +Sec-WebSocket-Version: 13\r\n\r\n"; +const CLIENT_TIMEOUT: Duration = Duration::from_secs(5); + +async fn serve(router: Router) -> ServerHandle { + #[cfg(not(feature = "compio"))] + { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + tako::Server::builder() + .build() + .try_spawn_http(listener, router) + .unwrap() + } + #[cfg(feature = "compio")] + { + let listener = compio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + tako::CompioServer::builder() + .build() + .try_spawn_http(listener, router) + .unwrap() + } +} + +fn client( + address: SocketAddr, +) -> ( + std::thread::JoinHandle<()>, + oneshot::Receiver<()>, + oneshot::Receiver>>, +) { + let (ready, ready_rx) = oneshot::channel(); + let (done, done_rx) = oneshot::channel(); + let thread = std::thread::spawn(move || { + let result = (|| { + let mut stream = TcpStream::connect(address)?; + stream.set_read_timeout(Some(CLIENT_TIMEOUT))?; + stream.write_all(UPGRADE)?; + let mut head = Vec::new(); + while !head.ends_with(b"\r\n\r\n") { + let mut byte = [0]; + stream.read_exact(&mut byte)?; + head.push(byte[0]); + assert!(head.len() < 8192); + } + assert!(head.starts_with(b"HTTP/1.1 101")); + + let _ = ready.send(()); + let mut frames = Vec::new(); + stream.read_to_end(&mut frames)?; + Ok(frames) + })(); + let _ = done.send(result); + }); + (thread, ready_rx, done_rx) +} + +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn shutdown_sends_going_away_and_closes_the_session() { + let mut router = Router::new(); + router.get("/ws", |req: Request| async move { + WebSocketUpgrade::new(req, |mut ws| async move { + while let Some(Ok(_)) = ws.recv().await {} + }) + }); + let server = serve(router).await; + let (thread, ready, done) = client(server.local_addr().unwrap()); + ready.await.unwrap(); + + server.shutdown(Duration::from_secs(1)).await; + let frames = done.await.unwrap(); + thread.join().unwrap(); + assert_eq!(frames.unwrap(), [0x88, 2, 0x03, 0xE9]); +} + +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn forced_shutdown_closes_a_socket_retained_by_another_task() { + let (started, mut started_rx) = tokio::sync::mpsc::channel(1); + let mut router = Router::new(); + router.get("/ws", move |req: Request| { + let started = started.clone(); + async move { + WebSocketUpgrade::new(req, |ws| async move { + let hold = async move { + let _socket = ws; + std::future::pending::<()>().await; + }; + #[cfg(not(feature = "compio"))] + tokio::spawn(hold); + #[cfg(feature = "compio")] + compio::runtime::spawn(hold).detach(); + started.send(()).await.unwrap(); + }) + } + }); + let server = serve(router).await; + let (thread, ready, done) = client(server.local_addr().unwrap()); + ready.await.unwrap(); + started_rx.recv().await.unwrap(); + + server.shutdown(Duration::ZERO).await; + let frames = done.await.unwrap(); + thread.join().unwrap(); + let frames = frames.expect("a retained WebSocket handle must not retain a stopped socket"); + assert!(frames.is_empty() || frames == [0x88, 2, 0x03, 0xE9]); +} + +struct Dropped(Arc); + +impl Drop for Dropped { + fn drop(&mut self) { + self.0.store(true, Ordering::SeqCst); + } +} + +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn shutdown_returns_only_after_the_session_handler_is_dropped() { + let dropped = Arc::new(AtomicBool::new(false)); + let (started, mut ready) = tokio::sync::mpsc::channel(1); + let mut router = Router::new(); + let handler_dropped = dropped.clone(); + router.get("/ws", move |req: Request| { + let guard = Dropped(handler_dropped.clone()); + let started = started.clone(); + async move { + WebSocketUpgrade::new(req, |socket| async move { + let _guard = guard; + let _socket = socket; + started.send(()).await.unwrap(); + std::future::pending::<()>().await; + }) + } + }); + let server = serve(router).await; + let (thread, upgraded, done) = client(server.local_addr().unwrap()); + upgraded.await.unwrap(); + ready.recv().await.unwrap(); + + server.shutdown(Duration::ZERO).await; + assert!(dropped.load(Ordering::SeqCst)); + done.await.unwrap().unwrap(); + thread.join().unwrap(); +} + +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn a_handler_constructor_panic_closes_a_retained_socket() { + let mut router = Router::new(); + router.get("/ws", |req: Request| async move { + WebSocketUpgrade::new(req, |socket| -> std::future::Ready<()> { + let hold = async move { + let _socket = socket; + std::future::pending::<()>().await; + }; + #[cfg(not(feature = "compio"))] + tokio::spawn(hold); + #[cfg(feature = "compio")] + compio::runtime::spawn(hold).detach(); + panic!("handler construction failed"); + }) + }); + let server = serve(router).await; + let (thread, ready, done) = client(server.local_addr().unwrap()); + ready.await.unwrap(); + let frames = done.await.unwrap().unwrap(); + thread.join().unwrap(); + + assert_eq!(frames, [0x88, 2, 0x03, 0xF3]); + server.shutdown(Duration::from_secs(1)).await; +} diff --git a/tako-rs/tests/websocket_shutdown_transports.rs b/tako-rs/tests/websocket_shutdown_transports.rs new file mode 100644 index 00000000..880936bf --- /dev/null +++ b/tako-rs/tests/websocket_shutdown_transports.rs @@ -0,0 +1,210 @@ +#![cfg(all( + feature = "ws", + any( + unix, + feature = "proxy-protocol", + all(feature = "tls", not(feature = "compio")), + feature = "compio-tls" + ) +))] + +use std::future::Future; +use std::io::Read; +use std::io::Write; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; +use std::time::Duration; + +#[cfg(feature = "compio")] +use tako::CompioServer as Server; +#[cfg(not(feature = "compio"))] +use tako::Server; +use tako::ServerHandle; +use tako::router::Router; +use tako::types::Request; +use tako::ws::WebSocketUpgrade; +use tokio::sync::mpsc; +use tokio::sync::oneshot; + +const UPGRADE: &[u8] = b"GET /ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\n\ +Connection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\ +Sec-WebSocket-Version: 13\r\n\r\n"; +const DEADLINE: Duration = Duration::from_secs(3); + +struct SessionDrop(Arc); + +impl Drop for SessionDrop { + fn drop(&mut self) { + self.0.store(true, Ordering::Release); + } +} + +fn endpoint() -> (Router, mpsc::Receiver<()>, Arc) { + let (started, received) = mpsc::channel(1); + let dropped = Arc::new(AtomicBool::new(false)); + let probe = dropped.clone(); + let mut router = Router::new(); + router.get("/ws", move |request: Request| { + let started = started.clone(); + let dropped = probe.clone(); + async move { + WebSocketUpgrade::new(request, move |mut socket| async move { + let _probe = SessionDrop(dropped); + started.send(()).await.unwrap(); + while let Some(Ok(_)) = socket.recv().await {} + }) + } + }); + + (router, received, dropped) +} + +async fn within(future: F) -> F::Output { + #[cfg(not(feature = "compio"))] + let result = tokio::time::timeout(DEADLINE, future).await; + #[cfg(feature = "compio")] + let result = compio::time::timeout(DEADLINE, future).await; + result.expect("WebSocket shutdown did not finish within the deadline") +} + +async fn check_shutdown( + server: ServerHandle, + mut started: mpsc::Receiver<()>, + dropped: Arc, + connect: C, +) where + C: FnOnce() -> std::io::Result + Send + 'static, + S: Read + Write, +{ + let (sent, received) = oneshot::channel(); + let client = std::thread::spawn(move || { + let result = (|| { + let mut stream = connect()?; + stream.write_all(UPGRADE)?; + stream.flush()?; + let mut head = Vec::new(); + while !head.ends_with(b"\r\n\r\n") { + let mut byte = [0]; + stream.read_exact(&mut byte)?; + head.push(byte[0]); + assert!(head.len() < 8192); + } + assert!(head.starts_with(b"HTTP/1.1 101"), "{head:?}"); + + let mut frames = Vec::new(); + stream.read_to_end(&mut frames)?; + Ok::<_, std::io::Error>(frames) + })(); + let _ = sent.send(result); + }); + within(started.recv()).await.unwrap(); + + within(server.shutdown(Duration::from_secs(1))).await; + assert!( + dropped.load(Ordering::Acquire), + "shutdown returned before the WebSocket handler was dropped" + ); + let result = within(received).await; + client.join().unwrap(); + assert_eq!(result.unwrap().unwrap(), [0x88, 2, 0x03, 0xE9]); +} + +#[cfg(any( + feature = "proxy-protocol", + all(feature = "tls", not(feature = "compio")), + feature = "compio-tls" +))] +fn connect_tcp(address: std::net::SocketAddr) -> std::io::Result { + let stream = std::net::TcpStream::connect_timeout(&address, DEADLINE)?; + stream.set_read_timeout(Some(DEADLINE))?; + stream.set_write_timeout(Some(DEADLINE))?; + Ok(stream) +} + +#[cfg(unix)] +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn unix_websocket_shutdown_sends_going_away_and_drops_the_handler() { + let path = std::env::temp_dir().join(format!("tako-ws-stop-{}.sock", std::process::id())); + let (router, started, dropped) = endpoint(); + let server = Server::builder() + .build() + .try_spawn_unix_http(path.clone(), router) + .await + .unwrap(); + + check_shutdown(server, started, dropped, move || { + let stream = std::os::unix::net::UnixStream::connect(path)?; + stream.set_read_timeout(Some(DEADLINE))?; + stream.set_write_timeout(Some(DEADLINE))?; + Ok(stream) + }) + .await; +} + +#[cfg(feature = "proxy-protocol")] +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn proxy_websocket_shutdown_sends_going_away_and_drops_the_handler() { + #[cfg(not(feature = "compio"))] + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + #[cfg(feature = "compio")] + let listener = compio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let (router, started, dropped) = endpoint(); + let server = Server::builder() + .build() + .try_spawn_proxy_protocol(listener, router) + .unwrap(); + + check_shutdown(server, started, dropped, move || { + let mut stream = connect_tcp(address)?; + stream.write_all(b"PROXY TCP4 192.0.2.1 192.0.2.2 40000 80\r\n")?; + Ok(stream) + }) + .await; +} + +#[cfg(any(all(feature = "tls", not(feature = "compio")), feature = "compio-tls"))] +#[cfg_attr(not(feature = "compio"), tokio::test)] +#[cfg_attr(feature = "compio", compio::test)] +async fn tls_websocket_shutdown_sends_going_away_and_drops_the_handler() { + use compio::tls::rustls; + + let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); + let certificate = rustls::pki_types::CertificateDer::from( + include_bytes!("fixtures/websocket-shutdown-cert.der").to_vec(), + ); + let key = rustls::pki_types::PrivatePkcs8KeyDer::from( + include_bytes!("fixtures/websocket-shutdown-key.der").to_vec(), + ); + #[cfg(not(feature = "compio"))] + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + #[cfg(feature = "compio")] + let listener = compio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let (router, started, dropped) = endpoint(); + let server = Server::builder() + .tls(tako::TlsCert::der(vec![certificate.clone()], key.into())) + .build() + .try_spawn_tls(listener, router) + .unwrap(); + + let mut roots = rustls::RootCertStore::empty(); + roots.add(certificate).unwrap(); + let mut config = rustls::ClientConfig::builder() + .with_root_certificates(roots) + .with_no_client_auth(); + config.alpn_protocols = vec![b"http/1.1".to_vec()]; + + check_shutdown(server, started, dropped, move || { + let connection = rustls::ClientConnection::new( + Arc::new(config), + rustls::pki_types::ServerName::try_from("localhost").unwrap(), + ) + .map_err(std::io::Error::other)?; + Ok(rustls::StreamOwned::new(connection, connect_tcp(address)?)) + }) + .await; +} diff --git a/website/content/docs/concepts/runtimes.mdx b/website/content/docs/concepts/runtimes.mdx index df520785..08ec6da2 100644 --- a/website/content/docs/concepts/runtimes.mdx +++ b/website/content/docs/concepts/runtimes.mdx @@ -101,9 +101,12 @@ to a multi-threaded Tokio executor. The `per-thread` and `per-thread-compio` features host the same thread-safe `Router` on N current-thread workers, fanned out with `SO_REUSEPORT`; Tokio workers also hand new connections to the least busy worker. -`serve_per_thread(addr, router, config)` blocks until Ctrl+C or SIGTERM and then -drains the workers. `spawn_per_thread(addr, router, config)` returns the worker -thread handles and a `PerThreadShutdown` for explicit control. Enabling +`serve_per_thread(addr, router, config)` waits for Ctrl+C, SIGTERM, or a terminal +worker failure, drains the workers, and returns failures. For observable +readiness and errors, `spawn_per_thread_supervised(addr, router, config)` returns +a `PerThreadHandle` with `wait_ready()`, `readiness()`, and async `result()`. +`spawn_per_thread(addr, router, config)` still returns the worker thread handles +and a `PerThreadShutdown` for explicit control. Enabling `per-thread-compio` also selects Compio for all shared runtime-dependent code. These entry points live in `tako-rs-server-pt`. diff --git a/website/content/docs/deployment.mdx b/website/content/docs/deployment.mdx index f0f70490..bf296e0d 100644 --- a/website/content/docs/deployment.mdx +++ b/website/content/docs/deployment.mdx @@ -88,12 +88,71 @@ fn main() -> std::io::Result<()> { } ``` -`serve_per_thread` waits for Ctrl+C or SIGTERM. For explicit control, use -`spawn_per_thread(addr, router, config)`, await -`shutdown.wait_for_bind_outcome(worker_count)`, then call `shutdown.trigger()` -and join the returned OS thread handles during teardown. Bind and plugin setup -errors are reported through the shutdown handle. These workers currently serve -HTTP/1.1. TLS, h2c, and PROXY listeners use the regular server builder. +`serve_per_thread` waits for Ctrl+C, SIGTERM, or a terminal worker failure. A +failure clears readiness, stops peers, and is returned to the caller. + +For explicit control, use `spawn_per_thread_supervised(addr, router, config)`. +Its `PerThreadHandle` provides `wait_ready()`, `readiness()`, and async `result()`. +The result reports the first terminal failure with its worker ID and error +kind; `readiness().workers()` exposes each worker's state. Use +`readiness().is_ready()` in a Healthcheck readiness probe. + +Call `handle.shutdown()` to begin draining, then `handle.join()` to wait for +the OS threads and return their result. `join()` blocks, so call it outside +an async executor or through `tokio::task::spawn_blocking`. Dropping the +supervised handle requests shutdown without blocking. The tuple-returning +`spawn_per_thread` remains available for callers that own their thread joins. + +A worker that cannot bind may still receive handoffs on Tokio. Readiness can +remain true while another worker listens; runtime, plugin, panic, and unexpected +exit failures stop the whole group. These workers serve HTTP/1.1. TLS, h2c, and +PROXY listeners use the regular server builder. + +To install a Healthcheck probe before spawning, capture a shared cell and fill +it with the readiness handle afterward. An empty cell is unready: + +```rust +use std::sync::{Arc, OnceLock}; +use tako::middleware::{IntoMiddleware, healthcheck::{Healthcheck, Probe}}; +use tako::router::Router; +use tako::{PerThreadConfig, PerThreadReadiness, spawn_per_thread_supervised}; + +async fn run() -> std::io::Result<()> { + let readiness = Arc::new(OnceLock::::new()); + let probe_readiness = Arc::clone(&readiness); + let probe = Probe::new("workers", move || { + let ready = probe_readiness.get().is_some_and(PerThreadReadiness::is_ready); + async move { + if ready { + Ok(()) + } else { + Err("workers are not ready".into()) + } + } + }); + + let mut router = Router::new(); + router.middleware(Healthcheck::new().probe(probe).into_middleware()); + let server = spawn_per_thread_supervised("127.0.0.1:8080", router, PerThreadConfig::default())?; + assert!(readiness.set(server.readiness()).is_ok()); + server.wait_ready().await?; + + tokio::select! { + result = server.result() => { + if let Err(error) = result { + eprintln!("{error}"); + } + } + signal = tokio::signal::ctrl_c() => { signal?; } + } + + server.shutdown(); + tokio::task::spawn_blocking(move || server.join()) + .await + .map_err(std::io::Error::other)? + .map_err(std::io::Error::from) +} +``` Workers are not pinned to CPU cores by default, so the OS can move a worker off a core that another process keeps busy. On a machine the server has to itself, diff --git a/website/content/docs/transports/http.mdx b/website/content/docs/transports/http.mdx index 259e57ae..6e3f2b2e 100644 --- a/website/content/docs/transports/http.mdx +++ b/website/content/docs/transports/http.mdx @@ -108,7 +108,7 @@ async fn main() -> anyhow::Result<()> { max_connections: Some(10_000), ..ServerConfig::default() }) - .build(); + .try_build()?; let handle = server.spawn_http(listener, router); tokio::signal::ctrl_c().await?; @@ -123,6 +123,14 @@ read timeout, keep-alive and keep-alive timeout, HTTP/2 caps, HTTP/3 caps, `Default` mirrors the historical hardcoded values (30 s drain, 30 s header read, 100 H2 streams), so anything you do not set keeps a safe default. +`ServerConfig::validate()` and the builder's `try_build()` return a typed +`ServerConfigError` with `field()` and `reason()` for invalid settings, such as +`max_connections: Some(0)`. The existing `build()` remains available and defers +validation to startup. Every `try_spawn_*` checks the configuration before +plugin setup or task creation; methods that bind their own sockets validate +before binding. The corresponding `spawn_*` reports startup failures through +`ServerHandle::result()`. + ## HTTP/2 cleartext (h2c) Enable the `http2` feature. Prior-knowledge h2c is typically used behind an L7 diff --git a/website/content/docs/transports/tcp-udp-unix.mdx b/website/content/docs/transports/tcp-udp-unix.mdx index 56a717d2..2b419516 100644 --- a/website/content/docs/transports/tcp-udp-unix.mdx +++ b/website/content/docs/transports/tcp-udp-unix.mdx @@ -215,11 +215,61 @@ and writable by the server user. Abstract-namespace sockets use no lockfile. ## Graceful shutdown -Each function has `…_with_shutdown` and `…_with_shutdown_and_drain` variants that -take a shutdown-signal future. On signal the listener stops accepting and in-flight -work is drained, with a default 30-second bound you can override on the -`_and_drain` form. For HTTP-over-unix, the builder's `spawn_unix_http` takes its -[`ServerConfig`](/docs/concepts/runtimes) for connection caps and timeouts. +Each function has a `…_with_shutdown` variant that takes a shutdown-signal future. +On signal the listener stops accepting and drains active work for up to 30 seconds. +TCP and Unix also have `…_with_shutdown_and_drain` variants for a different deadline. + +For connection limits and a drain deadline together, use +`serve_tcp_with_config(address, handler, signal, config)`, +`serve_udp_with_config(address, handler, signal, config)`, or +`serve_unix_with_config(path, handler, signal, config)`. Each accepts a +[`ServerConfig`](/docs/concepts/runtimes) and validates it before binding. + +`max_connections: Some(n)` limits active TCP and Unix handlers. A slot becomes +available when its handler returns, and the listener waits for a slot before +accepting another connection. For UDP, the same setting limits active datagram +handlers: excess datagrams are dropped immediately, without a queue of waiting +tasks. `None` leaves admission unlimited. Shutdown still works when every slot is +occupied and cancels handlers that exceed `drain_timeout`. + +Both `Server` and `CompioServer` provide `spawn_tcp_raw`, `spawn_udp_raw`, and +`spawn_unix_raw`, plus fallible `try_spawn_*` variants that bind before returning. +The Unix `try_spawn_unix_raw` method is async. Use the builder's `try_build()` to +check settings before startup; it returns a `ServerConfigError` whose `field()` +identifies the invalid setting. Existing `build()` calls remain usable, and all +startup methods still validate the configuration before starting work. + +```rust +use std::time::Duration; +use tako::{Server, ServerConfig}; + +#[tokio::main] +async fn main() -> Result<(), tako::types::BoxError> { + let server = Server::builder() + .config(ServerConfig { + max_connections: Some(100), + drain_timeout: Duration::from_secs(10), + ..ServerConfig::default() + }) + .try_build()?; + let handle = server.try_spawn_tcp_raw("127.0.0.1:9001", |mut stream, _peer| { + Box::pin(async move { + let (mut reader, mut writer) = stream.split(); + tokio::io::copy(&mut reader, &mut writer).await?; + Ok(()) + }) + })?; + tako::shutdown_signal().await; + handle.shutdown(Duration::from_secs(10)).await; + Ok(()) +} +``` + +Connection caps must be positive and within the runtime's supported range. +Validation also rejects values that exceed buffer, QUIC, or clock ranges. +Protocol stream, header, and send-buffer limits may be zero to preserve explicit +throttling policies. Zero shutdown and handshake deadlines remain valid +immediate-expiry policies; a zero HTTP/3 idle timeout disables that timeout. ## HTTP alternatives diff --git a/website/content/docs/transports/websocket.mdx b/website/content/docs/transports/websocket.mdx index 62467097..e573593c 100644 --- a/website/content/docs/transports/websocket.mdx +++ b/website/content/docs/transports/websocket.mdx @@ -72,6 +72,19 @@ other combinators work. Through `Sink`, `poll_flush` always writes. When the handler returns and no `WebSocket` handle is left, tako closes the connection with code 1000. A handler that panics closes it with 1011. +## Server shutdown + +WebSocket sessions participate in the listener's shutdown on both runtimes, +including per-thread workers and GraphQL subscriptions. Once draining starts, +new upgrades return HTTP 503 and existing sessions send close code 1001. +HTTP connections and WebSocket sessions share the configured drain deadline. + +After the deadline, remaining sessions are cancelled and their sockets are +closed. `ServerHandle::shutdown()` waits for that cleanup before returning, +including sockets whose WebSocket handles were moved into application tasks. +Those application tasks remain responsible for their own lifecycle; retained +handles report a closed connection. + ## Compio With the `compio` and `ws` features the same code runs on Compio. `WebSocket` is