Skip to content

Commit 2673a40

Browse files
authored
Disable SO_REUSEADDR in wasmtime serve (#7863)
Closes #7852
1 parent 825494f commit 2673a40

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/commands/serve.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::common::{Profile, RunCommon, RunTarget};
22
use anyhow::{anyhow, bail, Result};
33
use clap::Parser;
4+
use std::net::SocketAddr;
45
use std::{
56
path::PathBuf,
67
sync::{
@@ -64,7 +65,7 @@ pub struct ServeCommand {
6465

6566
/// Socket address for the web server to bind to.
6667
#[arg(long = "addr", value_name = "SOCKADDR", default_value_t = DEFAULT_ADDR )]
67-
addr: std::net::SocketAddr,
68+
addr: SocketAddr,
6869

6970
/// The WebAssembly component to run.
7071
#[arg(value_name = "WASM", required = true)]
@@ -262,7 +263,17 @@ impl ServeCommand {
262263

263264
let instance = linker.instantiate_pre(&component)?;
264265

265-
let listener = tokio::net::TcpListener::bind(self.addr).await?;
266+
// Tokio by default sets `SO_REUSEADDR` for listeners but that makes it
267+
// a bit confusing if you run Wasmtime but forget to close a previous
268+
// `serve` session. To avoid that we explicitly disable `SO_REUSEADDR`
269+
// here.
270+
let socket = match &self.addr {
271+
SocketAddr::V4(_) => tokio::net::TcpSocket::new_v4()?,
272+
SocketAddr::V6(_) => tokio::net::TcpSocket::new_v6()?,
273+
};
274+
socket.set_reuseaddr(false)?;
275+
socket.bind(self.addr)?;
276+
let listener = socket.listen(100)?;
266277

267278
eprintln!("Serving HTTP on http://{}/", listener.local_addr()?);
268279

0 commit comments

Comments
 (0)