A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix a panic in wasi-http with a zero timeout (#14483)
Alex Crichton9d5eb1ee2683
diff --git a/crates/test-programs/src/bin/p2_http_outbound_request_timeout.rs b/crates/test-programs/src/bin/p2_http_outbound_request_timeout.rsindex ae0dac5120..8328290ef6 100644--- a/crates/test-programs/src/bin/p2_http_outbound_request_timeout.rs+++ b/crates/test-programs/src/bin/p2_http_outbound_request_timeout.rs@@ -30,4 +30,42 @@ fn main() {),"expected connection timeout: {err:?}");++ let addr = std::env::var("HTTP_SERVER").unwrap();++ // zero timeouts are accepted and shouldn't panic anything on the host, but+ // they're a bit finnicky so don't assert the result+ let _ = test_programs::http::request(+ Method::Post,+ Scheme::Http,+ &addr,+ "/post",+ Some(b"{\"foo\": \"bar\"}"),+ None,+ Some(0),+ None,+ None,+ );+ let _ = test_programs::http::request(+ Method::Post,+ Scheme::Http,+ &addr,+ "/post",+ Some(b"{\"foo\": \"bar\"}"),+ None,+ None,+ Some(0),+ None,+ );+ let _ = test_programs::http::request(+ Method::Post,+ Scheme::Http,+ &addr,+ "/post",+ Some(b"{\"foo\": \"bar\"}"),+ None,+ None,+ None,+ Some(0),+ );}diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_timeout.rs b/crates/test-programs/src/bin/p3_http_outbound_request_timeout.rsindex 64f100fded..c91073eaa8 100644--- a/crates/test-programs/src/bin/p3_http_outbound_request_timeout.rs+++ b/crates/test-programs/src/bin/p3_http_outbound_request_timeout.rs@@ -34,6 +34,49 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {),"expected connection timeout: {err:?}");++ const BODY: &[u8] = b"{\"foo\": \"bar\"}";+ let addr = test_programs::p3::wasi::cli::environment::get_environment()+ .into_iter()+ .find_map(|(k, v)| k.eq("HTTP_SERVER").then_some(v))+ .unwrap();+ let _ = test_programs::p3::http::request(+ Method::Post,+ Scheme::Http,+ &addr,+ "/post",+ Some(BODY),+ None,+ Some(0),+ None,+ None,+ )+ .await;+ let _ = test_programs::p3::http::request(+ Method::Post,+ Scheme::Http,+ &addr,+ "/post",+ Some(BODY),+ None,+ None,+ Some(0),+ None,+ )+ .await;+ let _ = test_programs::p3::http::request(+ Method::Post,+ Scheme::Http,+ &addr,+ "/post",+ Some(BODY),+ None,+ None,+ None,+ Some(0),+ )+ .await;+Ok(())}}diff --git a/crates/wasi-http/src/default_send_request.rs b/crates/wasi-http/src/default_send_request.rsindex afed96215e..99ccb13e46 100644--- a/crates/wasi-http/src/default_send_request.rs+++ b/crates/wasi-http/src/default_send_request.rs@@ -155,6 +155,13 @@ pub async fn default_send_request(.await.map_err(|_| Error::ConnectionReadTimeout)?.map_err(Error::from)?;+ // Tokio panics on an `interval` with a zero duration, so clamp it to a+ // 1ns timeout which is sort of effectively zero anyway.+ let between_bytes_timeout = if between_bytes_timeout == Duration::ZERO {+ Duration::new(0, 1)+ } else {+ between_bytes_timeout+ };let mut timeout = tokio::time::interval(between_bytes_timeout);timeout.reset();Ok(res.map(|incoming| IncomingResponseBody { incoming, timeout }))diff --git a/crates/wasi-http/tests/all/p2/async_.rs b/crates/wasi-http/tests/all/p2/async_.rsindex 20234c4469..56a4fbcabd 100644--- a/crates/wasi-http/tests/all/p2/async_.rs+++ b/crates/wasi-http/tests/all/p2/async_.rs@@ -26,7 +26,7 @@ async fn p2_http_outbound_request_get() -> Result<()> {#[test_log::test(tokio::test(flavor = "multi_thread"))]async fn p2_http_outbound_request_timeout() -> Result<()> {- let server = Server::http1(1)?;+ let server = Server::http1(3)?;run(P2_HTTP_OUTBOUND_REQUEST_TIMEOUT_COMPONENT, &server).await}diff --git a/crates/wasi-http/tests/all/p2/sync.rs b/crates/wasi-http/tests/all/p2/sync.rsindex c55faf3383..22f8f523dd 100644--- a/crates/wasi-http/tests/all/p2/sync.rs+++ b/crates/wasi-http/tests/all/p2/sync.rs@@ -26,7 +26,7 @@ fn p2_http_outbound_request_get() -> Result<()> {#[test_log::test]fn p2_http_outbound_request_timeout() -> Result<()> {- let server = Server::http1(1)?;+ let server = Server::http1(3)?;run(P2_HTTP_OUTBOUND_REQUEST_TIMEOUT_COMPONENT, &server)}diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rsindex b24e31bc46..97fc1c6d98 100644--- a/crates/wasi-http/tests/all/p3/mod.rs+++ b/crates/wasi-http/tests/all/p3/mod.rs@@ -196,7 +196,7 @@ async fn p3_http_outbound_request_get() -> wasmtime::Result<()> {#[test_log::test(tokio::test(flavor = "multi_thread"))]async fn p3_http_outbound_request_timeout() -> wasmtime::Result<()> {- let server = Server::http1(1)?;+ let server = Server::http1(3)?;run_cli(P3_HTTP_OUTBOUND_REQUEST_TIMEOUT_COMPONENT, &server).await}