Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

serve: reject zero for the `--max-concurrent-*` options (#14661)

Xia Chaod57f93893c23
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index 8f04c697f0..c41d778f6d 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -7,6 +7,7 @@ use pin_project_lite::pin_project;
use std::convert::Infallible;
use std::ffi::OsString;
use std::net::SocketAddr;
+use std::num::NonZeroUsize;
use std::pin::Pin;
use std::task::{Context, Poll};
@@ -179,11 +180,11 @@ pub struct ServeCommand {
/// Maximum number of concurrent requests that can be processed at any one
/// point in time.
#[arg(long)]
- max_concurrent_requests: Option<usize>,
+ max_concurrent_requests: Option<NonZeroUsize>,
/// Maximum number of concurrent connections that can be held at any one
/// point in time.
#[arg(long)]
- max_concurrent_connections: Option<usize>,
+ max_concurrent_connections: Option<NonZeroUsize>,
#[arg(skip)]
inherited_sockets: InheritedSockets,
@@ -717,9 +718,11 @@ impl ServeCommand {
let max_concurrent_connections = self
.max_concurrent_connections
+ .map(|n| n.get())
.unwrap_or(if debuggee_store.is_some() { 1 } else { 1000 });
let max_concurrent_requests = self
.max_concurrent_requests
+ .map(|n| n.get())
.unwrap_or(if debuggee_store.is_some() { 1 } else { 1000 });
if debuggee_store.is_some() && max_concurrent_connections != 1 {
bail!("cannot have more than 1 max concurrent connections with a debugger");
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 5dfe8b049b..cfadc273c3 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -1825,6 +1825,41 @@ mod test_programs {
Ok(())
}
+ #[test]
+ fn p2_cli_serve_reject_zero_concurrency() -> Result<()> {
+ // Both limits are handed to a semaphore, so a zero leaves the server
+ // listening but never serving anything.
+ let component = P2_CLI_SERVE_HELLO_WORLD_COMPONENT;
+ for option in [
+ "--max-concurrent-requests=0",
+ "--max-concurrent-connections=0",
+ ] {
+ let mut cmd = super::get_wasmtime_command()?;
+ cmd.args(["serve", "-Scli", option, component])
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped());
+ let mut child = cmd.spawn()?;
+ let deadline = std::time::Instant::now() + Duration::from_secs(10);
+ while child.try_wait()?.is_none() {
+ assert!(
+ std::time::Instant::now() < deadline,
+ "`{option}` was accepted and the server kept running"
+ );
+ thread::sleep(Duration::from_millis(100));
+ }
+ let output = child.wait_with_output()?;
+ assert!(!output.status.success(), "`{option}` was accepted");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains("invalid value"),
+ "expected `{option}` to be rejected, got: {stderr}"
+ );
+ }
+
+ Ok(())
+ }
+
#[test]
fn p2_cli_run_outgoing_body_chunk_size() -> Result<()> {
// Without the option an outgoing body keeps its 1 MiB default budget.