From 0a6cc3bfa8b8c0738198f67ffe5a3841401c1499 Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Wed, 26 Apr 2023 23:12:25 +0200 Subject: [PATCH] Allow the dev server socket to be reused immediately (vercel/turbo#4709) ### Description This avoids running into "Address already in use (os error 48)" when restarting the dev server in quick succession. --------- Co-authored-by: Tobias Koppers --- crates/turbopack-dev-server/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/turbopack-dev-server/src/lib.rs b/crates/turbopack-dev-server/src/lib.rs index f12ef6a0c2be6..d14d78a40bf86 100644 --- a/crates/turbopack-dev-server/src/lib.rs +++ b/crates/turbopack-dev-server/src/lib.rs @@ -107,6 +107,12 @@ impl DevServer { // real TCP listener, see if it bound, and get its bound address. let socket = Socket::new(Domain::for_address(addr), Type::STREAM, Some(Protocol::TCP)) .context("unable to create socket")?; + // Allow the socket to be reused immediately after closing. This ensures that + // the dev server can be restarted on the same address without a buffer time for + // the OS to release the socket. + // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse + #[cfg(not(windows))] + let _ = socket.set_reuse_address(true); if matches!(addr, SocketAddr::V6(_)) { // When possible bind to v4 and v6, otherwise ignore the error let _ = socket.set_only_v6(false);