Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the dev server socket to be reused immediately #4709

Merged
merged 2 commits into from
Apr 26, 2023
Merged

Conversation

alexkirsz
Copy link
Contributor

Description

This avoids running into "Address already in use (os error 48)" when restarting the dev server in quick succession.

@alexkirsz alexkirsz requested a review from a team as a code owner April 26, 2023 12:08
@vercel
Copy link

vercel bot commented Apr 26, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-native-web 🔄 Building (Inspect) Apr 26, 2023 6:50pm
examples-nonmonorepo 🔄 Building (Inspect) Apr 26, 2023 6:50pm
9 Ignored Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-cra-web ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-designsystem-docs ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-gatsby-web ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-kitchensink-blog ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-svelte-web ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-tailwind-web ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
examples-vite-web ⬜️ Ignored (Inspect) Apr 26, 2023 6:50pm
turbo-site ⬜️ Ignored (Inspect) Visit Preview Apr 26, 2023 6:50pm

// 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.
let _ = socket.set_reuse_address(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also allow binding to the same port twice, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's SO_REUSEPORT.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indicates that futher calls to bind may allow reuse of local addresses. For IPv4 sockets this means that a socket may bind even when there's a socket already listening on this port

from the rustdoc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it also says linux now matches this behavior

I'm fine with merging this for macOS/Linux only, but we might have to update the port finding code, we've already had a bunch of bug reports because of it not reporting ports in use correctly / not finding a new port as expected in the past and this might make it worse

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may very well break find_port on macos and linux

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll test find_port on macOS with and without this option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_port still works properly with this option on macOS. However, as you said, binding to different local addresses no longer produces an error.

One way to fix this would be to replace find_port to run lsof/other OS-specific commands to check if a port is in use instead of trying to bind to that port.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 26, 2023

✅ This change can build next-swc

@github-actions
Copy link
Contributor

github-actions bot commented Apr 26, 2023

🟢 CI successful 🟢

Thanks

@sokra sokra merged commit 2b0654e into main Apr 26, 2023
@sokra sokra deleted the alexkirsz/reuseaddr branch April 26, 2023 21:12
sokra added a commit to vercel/next.js that referenced this pull request Apr 26, 2023
### What?

* vercel/turborepo#4700 <!-- Tobias Koppers - update
deps -->
* vercel/turborepo#4706 <!-- Tobias Koppers - make
library code less verbose in stack traces -->
* vercel/turborepo#4705 <!-- Tobias Koppers -
improve error handling in update stream -->
* vercel/turborepo#4667 <!-- Caleb Webber - remove
box_syntax -->
* vercel/turborepo#4714 <!-- Tobias Koppers - chunk
hash need to include availability root -->
* vercel/turborepo#4709 <!-- Alex Kirszenberg -
Allow the dev server socket to be reused immediately -->
* vercel/turborepo#4716 <!-- Tobias Koppers - errors
lead to consistent exit code in issue detail -->
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
…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 <tobias.koppers@googlemail.com>
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
…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 <tobias.koppers@googlemail.com>
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
…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 <tobias.koppers@googlemail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants