Skip to content

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

serve: reject --addr with --systemd-listenfd (#14902)

Xia Chao0973fbe39508
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index af09cee643..d5d0ec4571 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -109,7 +109,9 @@ pub struct ServeCommand {
run: RunCommon,
/// Socket addresses for the web server to bind to.
- #[arg(long , value_name = "SOCKADDR", default_values_t = DEFAULT_ADDRS)]
+ ///
+ /// Defaults to `0.0.0.0:8080`.
+ #[arg(long, value_name = "SOCKADDR", conflicts_with = "systemd_listenfd")]
addr: Vec<SocketAddr>,
/// Socket address where, when connected to, will initiate a graceful
@@ -664,8 +666,13 @@ impl ServeCommand {
listeners
}
None => {
+ let addrs = if self.addr.is_empty() {
+ DEFAULT_ADDRS.to_vec()
+ } else {
+ self.addr.clone()
+ };
let mut servers = vec![];
- for addr in &self.addr {
+ for addr in &addrs {
let socket = match addr {
SocketAddr::V4(_) => tokio::net::TcpSocket::new_v4()?,
SocketAddr::V6(_) => tokio::net::TcpSocket::new_v6()?,
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 7ec323b7b4..f0fd50fd75 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -2905,6 +2905,47 @@ start a print 1234
.await
}
+ #[cfg(unix)]
+ #[test]
+ fn serve_reject_addr_with_systemd_listenfd() -> Result<()> {
+ // `--systemd-listenfd` takes the sockets from the environment; an
+ // explicit `--addr` used to be accepted and silently ignored.
+ let mut cmd = super::get_wasmtime_command()?;
+ cmd.args([
+ "serve",
+ "-Scli",
+ "--systemd-listenfd",
+ "--addr",
+ "127.0.0.1:0",
+ P2_CLI_SERVE_HELLO_WORLD_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,
+ "--addr with --systemd-listenfd was accepted and the server kept running"
+ );
+ thread::sleep(Duration::from_millis(100));
+ }
+ let output = child.wait_with_output()?;
+ assert!(
+ !output.status.success(),
+ "--addr with --systemd-listenfd was accepted"
+ );
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains("cannot be used with")
+ && stderr.contains("--addr")
+ && stderr.contains("--systemd-listenfd"),
+ "unexpected stderr: {stderr}"
+ );
+ Ok(())
+ }
+
#[cfg(unix)]
#[tokio::test]
async fn serve_inherit() -> Result<()> {