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

No error is thrown when port is taken #1443

Closed
Electroid opened this issue Nov 1, 2022 · 7 comments
Closed

No error is thrown when port is taken #1443

Electroid opened this issue Nov 1, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@Electroid
Copy link
Contributor

import { serve } from "bun";

serve({
  port: 4040,
  fetch(request) {
    return new Response("Hi");
  }
});

serve({
  port: 4040,
  fetch(request) {
    return new Response("Hi (again)");
  }
});

I would expect an error to be thrown on the 2nd serve. There is also no error when the port is taken by a non-Bun process. Tested on MacOS Silicon, while trying to debug #247

@Electroid Electroid added the bug Something isn't working label Nov 1, 2022
@Jarred-Sumner
Copy link
Collaborator

This is more complicated than it seems

SO_REUSEPORT is a builtin way to do load balancing, but it only really works properly on Linux

We should probably disable SO_REUSEPORT when development mode is enabled

@Electroid
Copy link
Contributor Author

I think it's a potential footgun to not have this throw, even though SO_REUSEPORT is sometimes useful in certain deployments. Maybe that's a flag you have to opt-in to?

export default {
  port: 3000,
  portReuse: true,
  // ...
}

@Electroid
Copy link
Contributor Author

This was fixed.

@calebeaires
Copy link

@Electroid, when you mentioned that "we will modify the default behavior to indicate an error in the event of a port conflict," are you implying that the reusePort feature is being considered for deprecation?

I have found the reusePort feature to be highly beneficial, as it serves as a viable alternative to PM2 cluster mode!

@khaled-0
Copy link

This serves as a built in load balancer for me. Should it?
Seems like an unintentional feature to me.

@Jarred-Sumner
Copy link
Collaborator

@calebeaires @khaled-0 we are not deprecating reusePort: true, do not worry.

Originally, reusePort: true did not exist, it was implicitly the default behavior (i.e. no option), which means we did not throw an error when the port was already in use. This is very confusing in development, but in production this feature is important. So we added reusePort: true to make this behavior explicit - to allow the production usecase without causing confusion in development.

@KostyaTretyak
Copy link

@Jarred-Sumner, how to achieve port reuse when using node:http package (like in cluster mode)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants