Skip to content

Commit

Permalink
Allow the dev server socket to be reused immediately (#4709)
Browse files Browse the repository at this point in the history
### 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 <tobias.koppers@googlemail.com>
  • Loading branch information
alexkirsz and sokra authored Apr 26, 2023
1 parent b7e03af commit 2b0654e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/turbopack-dev-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2b0654e

Please sign in to comment.