Skip to content

Commit

Permalink
Allow the dev server socket to be reused immediately (vercel/turborep…
Browse files Browse the repository at this point in the history
…o#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 <[email protected]>
alexkirsz and sokra authored Apr 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 149a144 commit 624cbc9
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
@@ -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);

0 comments on commit 624cbc9

Please sign in to comment.