Skip to content

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

serve: reject concurrency limits above the semaphore maximum (#14936)

Xia Chao9427dafef0e2
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index a1efcc0d46..5dc91f0609 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -182,17 +182,28 @@ pub struct ServeCommand {
/// Maximum number of concurrent requests that can be processed at any one
/// point in time.
- #[arg(long)]
+ #[arg(long, value_parser = parse_concurrency_limit)]
max_concurrent_requests: Option<NonZeroUsize>,
/// Maximum number of concurrent connections that can be held at any one
/// point in time.
- #[arg(long)]
+ #[arg(long, value_parser = parse_concurrency_limit)]
max_concurrent_connections: Option<NonZeroUsize>,
#[arg(skip)]
inherited_sockets: InheritedSockets,
}
+/// Both limits are handed to a semaphore, which panics above its permit limit.
+fn parse_concurrency_limit(s: &str) -> Result<NonZeroUsize, String> {
+ let n: NonZeroUsize = s
+ .parse()
+ .map_err(|e: std::num::ParseIntError| e.to_string())?;
+ if n.get() > Semaphore::MAX_PERMITS {
+ return Err(format!("maximum is {}", Semaphore::MAX_PERMITS));
+ }
+ Ok(n)
+}
+
impl ServeCommand {
/// Start a server to run the given wasi-http proxy component
pub fn execute(mut self) -> Result<()> {
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index a55d01268c..27fd6fbda0 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -1903,12 +1903,15 @@ mod test_programs {
// 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;
+ let too_large = format!("={}", usize::MAX);
for option in [
- "--max-concurrent-requests=0",
- "--max-concurrent-connections=0",
+ "--max-concurrent-requests=0".to_string(),
+ "--max-concurrent-connections=0".to_string(),
+ format!("--max-concurrent-requests{too_large}"),
+ format!("--max-concurrent-connections{too_large}"),
] {
let mut cmd = super::get_wasmtime_command()?;
- cmd.args(["serve", "-Scli", option, component])
+ cmd.args(["serve", "-Scli", option.as_str(), component])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped());