diff --git a/Cargo.lock b/Cargo.lock index 6c123ac17..ff16cf478 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,7 @@ dependencies = [ "tls-parser", "tokio", "tokio-tungstenite", + "tokio-util", "toml", "tonic", "tonic-prost", diff --git a/Cargo.toml b/Cargo.toml index 703429f28..d47f70384 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ firecracker_client = { version = "1.15.1", path = "thirdparty/firecracker-client envd = { version = "0.1.0", path = "thirdparty/envd" } custom_extension_client = { version = "0.1.0", path = "src/custom_extension_api/generated", default-features = false } tokio = { version = "1", features = ["full"] } +tokio-util = "0.7" reqsign = { version = "0.16.5", default-features = false, features = ["services-aws"] } reqwest = { version = "0.13", features = ["json", "stream"] } opendal = { version = "0.59.5", features = ["services-oss", "services-s3"] } diff --git a/docs/src/internals/proxy-design.md b/docs/src/internals/proxy-design.md index 822f91897..1c0ba9533 100644 --- a/docs/src/internals/proxy-design.md +++ b/docs/src/internals/proxy-design.md @@ -176,6 +176,22 @@ Forwarded headers are injected: HTTP bodies are proxied as streams (request and response), including SSE and large uploads/downloads. +### Runtime Shutdown + +When the runtime shuts down, it pauses its sandboxes. A proxied HTTP request +that is still open then waits for an answer from a sandbox that sends none. +Once that sandbox shutdown has finished or failed, the runtime ends those +requests. A request still waiting for response headers gets `502 Bad Gateway`. +A response body first forwards any data Hyper, the HTTP library, can return +immediately. A complete response already available this way can finish normally. +If Hyper would need to wait, the body ends with an error. Bytes still waiting +in the socket or being processed by Hyper may be lost. + +The server still waits for other HTTP connections to drain. A download whose +client has stopped reading can remain open because Hyper stops polling its +body when the socket fills. Proxy cancellation does not end WebSockets or +their pending handshakes; a pending handshake keeps its existing 30 s limit. + ## WebSocket Semantics WebSocket upgrade is supported through the same `/proxy` endpoints. @@ -205,6 +221,7 @@ Handshake failure behavior: - Upstream transport/connect failure - Sandbox is `Running` but runtime route is missing (`RouteMissing`) - Paused sandbox auto-resume failed + - Runtime shutdown ended the request before the upstream answered - `504 Gateway Timeout` - Paused sandbox auto-resume timed out - Upstream response header timeout diff --git a/src/api/impls/mod.rs b/src/api/impls/mod.rs index 089ed70e8..dbc265ec6 100644 --- a/src/api/impls/mod.rs +++ b/src/api/impls/mod.rs @@ -9,10 +9,12 @@ mod template; mod template_helpers; mod volumes; +use std::future::Future; use std::sync::Arc; use anyhow::Error as AnyhowError; use async_trait::async_trait; +use tokio_util::sync::CancellationToken; use super::proxy::{build_proxy_client, ProxyClient}; use crate::api_key::ApiKey; @@ -59,6 +61,7 @@ pub struct ApiImpl { volume_manager: Arc, observability: Option>, proxy_client: ProxyClient, + proxied_requests_end: CancellationToken, sandbox_proxy_domains: Vec, api_key: ApiKey, build_sessions: Arc, @@ -85,6 +88,7 @@ impl ApiImpl { volume_manager, observability, proxy_client: build_proxy_client(), + proxied_requests_end: CancellationToken::new(), sandbox_proxy_domains, api_key, build_sessions: Arc::new(image_build::BuildSessions::default()), @@ -100,6 +104,20 @@ impl ApiImpl { &self.proxy_client } + /// Signals the runtime HTTP proxy to end requests that need more sandbox data. + /// Response bodies still forward data Hyper can return immediately. + /// + /// Call this after sandbox shutdown. Paused or stopped sandboxes cannot + /// answer these requests, so waiting for them would hold up shutdown. + pub fn end_proxied_requests(&self) { + self.proxied_requests_end.cancel(); + } + + /// Resolves once [`Self::end_proxied_requests`] has run. + pub(crate) fn proxied_requests_ended(&self) -> impl Future + Send + 'static { + self.proxied_requests_end.clone().cancelled_owned() + } + pub(crate) fn sandbox_proxy_domains(&self) -> &[String] { &self.sandbox_proxy_domains } diff --git a/src/api/proxy.rs b/src/api/proxy.rs index efc1a7bda..9355c7178 100644 --- a/src/api/proxy.rs +++ b/src/api/proxy.rs @@ -1,4 +1,10 @@ -use std::{error::Error as StdError, future::Future, time::Duration}; +use std::{ + error::Error as StdError, + future::Future, + pin::Pin, + task::{Context, Poll}, + time::Duration, +}; use axum::{ body::Body, @@ -17,7 +23,7 @@ use axum::{ }; use futures::{SinkExt, StreamExt}; use http_body_util::BodyExt; -use hyper::body::Incoming; +use hyper::body::{Body as HttpBody, Bytes, Frame, Incoming, SizeHint}; use hyper_util::{ client::legacy::{connect::HttpConnector, Client}, rt::TokioExecutor, @@ -468,12 +474,25 @@ async fn proxy_http_request( let (body, activity_rx) = track_request_body_activity(body); let upstream_request = Request::from_parts(parts, body); - let upstream_response_result = match wait_for_upstream_response_headers_with_activity_timeout( - api_impl.proxy_client().request(upstream_request), - activity_rx, - ) - .await - { + let waited = tokio::select! { + waited = wait_for_upstream_response_headers_with_activity_timeout( + api_impl.proxy_client().request(upstream_request), + activity_rx, + ) => waited, + () = api_impl.proxied_requests_ended() => { + // Shutdown can leave the sandbox unable to answer. Return 502 because + // the request may already have reached it. The proxy uses 404 or 410 + // only when it refuses a request before forwarding it. + warn!( + sandbox_id = %sandbox_id, + method = %upstream_method, + upstream = %upstream_uri_for_log, + "shutdown ended the upstream proxy request" + ); + return StatusCode::BAD_GATEWAY.into_response(); + } + }; + let upstream_response_result = match waited { Ok(result) => result, Err(_) => { warn!( @@ -517,7 +536,7 @@ async fn proxy_http_request( } }; - map_upstream_response(upstream_response) + map_upstream_response(upstream_response, api_impl.proxied_requests_ended()) } fn track_request_body_activity(body: Body) -> (Body, watch::Receiver<()>) { @@ -1090,12 +1109,55 @@ fn remove_hop_by_hop_headers(headers: &mut HeaderMap) { headers.remove(HeaderName::from_static("keep-alive")); } -fn map_upstream_response(response: Response) -> Response { +fn map_upstream_response( + response: Response, + requests_ended: impl Future + Send + 'static, +) -> Response { let (mut parts, body) = response.into_parts(); // Mirror the request-side filtering on the way back so connection-scoped // headers from the upstream do not leak through this proxy hop. remove_hop_by_hop_headers(&mut parts.headers); - Response::from_parts(parts, Body::new(body.map_err(axum::Error::new))) + let body = UpstreamBody { + body, + requests_ended: Box::pin(requests_ended), + }; + Response::from_parts(parts, Body::new(body)) +} + +/// Forwards response data Hyper can return immediately, including during shutdown. +/// After cancellation, returns an error if Hyper would need to wait for more data. +/// Bytes still in the socket or being processed by Hyper may be lost. +struct UpstreamBody { + body: Incoming, + requests_ended: Pin + Send>>, +} + +impl HttpBody for UpstreamBody { + type Data = Bytes; + type Error = axum::Error; + + fn poll_frame( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll, axum::Error>>> { + if let Poll::Ready(frame) = Pin::new(&mut self.body).poll_frame(cx) { + return Poll::Ready(frame.map(|result| result.map_err(axum::Error::new))); + } + if self.requests_ended.as_mut().poll(cx).is_ready() { + return Poll::Ready(Some(Err(axum::Error::new( + "shutdown ended the upstream response", + )))); + } + Poll::Pending + } + + fn is_end_stream(&self) -> bool { + self.body.is_end_stream() + } + + fn size_hint(&self) -> SizeHint { + self.body.size_hint() + } } async fn bridge_websocket_streams( @@ -3107,6 +3169,136 @@ mod tests { assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT); } + async fn request_running_sandbox( + upstream_addr: SocketAddr, + ) -> (Arc, tokio::task::JoinHandle>) { + let sandbox_id = SandboxId::new(); + let api = build_api().await; + api.orchestrator() + .set_proxy_target_for_test( + sandbox_id, + ProxyTarget::new(Ipv4Addr::LOCALHOST), + crate::orchestrator::SandboxState::Running, + ) + .await; + let app = server::new(api.clone()); + let request = Request::builder() + .uri("/proxy/download") + .header(SANDBOX_ID_HEADER, sandbox_id.to_string()) + .header(TARGET_PORT_HEADER, upstream_addr.port().to_string()) + .body(Body::empty()) + .unwrap(); + let response = tokio::spawn(async move { app.oneshot(request).await.unwrap() }); + (api, response) + } + + #[tokio::test] + async fn proxy_answers_bad_gateway_when_proxied_requests_end_before_upstream_headers() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let upstream_addr = listener.local_addr().unwrap(); + let (received_tx, received_rx) = tokio::sync::oneshot::channel(); + tokio::spawn(async move { + let (mut upstream, _) = listener.accept().await.unwrap(); + read_http_request_head(&mut upstream).await; + received_tx.send(()).unwrap(); + // Like a paused sandbox, keep the connection open and never answer. + std::future::pending::<()>().await; + }); + let (api, pending_response) = request_running_sandbox(upstream_addr).await; + received_rx.await.unwrap(); + + api.end_proxied_requests(); + + let response = pending_response.await.unwrap(); + assert_eq!(response.status(), StatusCode::BAD_GATEWAY); + } + + #[tokio::test] + async fn proxy_breaks_streaming_response_body_when_proxied_requests_end() { + async fn endless_download() -> Response { + let first = stream::once(async { Ok::<_, Infallible>("first") }); + // Like a paused sandbox, send nothing more and never end the body. + Response::new(Body::from_stream(first.chain(stream::pending()))) + } + let upstream_addr = + spawn_upstream(axum::Router::new().route("/download", get(endless_download))).await; + let (api, pending_response) = request_running_sandbox(upstream_addr).await; + let response = pending_response.await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let mut body = response.into_body().into_data_stream(); + assert_eq!(body.next().await.unwrap().unwrap(), "first"); + let mut next_frame = tokio::spawn(async move { body.next().await }); + // Wait until the reader is blocked on more data. Canceling the request + // must wake it up; the sandbox will not send another frame. + assert!( + timeout(Duration::from_millis(100), &mut next_frame) + .await + .is_err(), + "the response body should wait for the upstream's next frame" + ); + + api.end_proxied_requests(); + + let next = timeout(Duration::from_secs(1), next_frame) + .await + .expect("the response body should end once proxied requests end") + .unwrap(); + assert!( + matches!(next, Some(Err(_))), + "the response body should fail, not end cleanly: {next:?}" + ); + } + + #[tokio::test] + async fn proxy_preserves_buffered_command_acknowledgment_after_cancellation() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let upstream = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + read_http_request_head(&mut stream).await; + let acknowledgment = r#"{"commandID":"command-123"}"#; + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + acknowledgment.len(), + acknowledgment + ); + stream.write_all(response.as_bytes()).await.unwrap(); + }); + let stream = tokio::net::TcpStream::connect(address).await.unwrap(); + let (mut sender, connection) = + hyper::client::conn::http1::handshake(hyper_util::rt::TokioIo::new(stream)) + .await + .unwrap(); + let connection = tokio::spawn(connection); + let response = sender + .send_request( + Request::builder() + .method(Method::POST) + .uri("/commands") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + upstream.await.unwrap(); + // Wait until Hyper has read the complete command response before + // testing cancellation. The proxy must still deliver its command ID. + timeout(Duration::from_secs(1), connection) + .await + .unwrap() + .unwrap() + .unwrap(); + let response = map_upstream_response(response, std::future::ready(())); + let body = response + .into_body() + .collect() + .await + .expect("a buffered acknowledgment must finish despite cancellation") + .to_bytes(); + let acknowledgment: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(acknowledgment["commandID"], "command-123"); + } + #[tokio::test] async fn proxy_keeps_envd_stream_input_alive_with_body_activity() { let upstream_addr = start_client_stream_response_server().await; diff --git a/src/bin/server.rs b/src/bin/server.rs index 0328a4ae2..48226868e 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -209,6 +209,9 @@ async fn main() -> anyhow::Result<()> { if let Err(err) = shutdown_orchestrator.shutdown().await { warn!(target: "agentenv", error = %err, "error occurred while shutting down orchestrator"); } + // Sandbox shutdown has returned, and the process will exit even if it failed. + // End requests still waiting for a sandbox response. + api_impl.end_proxied_requests(); if let Some(pool) = FirecrackerPool::global() { info!(target: "agentenv", "shutting down firecracker pool"); if let Err(err) = pool.shutdown().await {