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

Why is SO_REUSEADDR disabled for Windows? #65

Closed
MacGritsch opened this issue Feb 22, 2014 · 6 comments
Closed

Why is SO_REUSEADDR disabled for Windows? #65

MacGritsch opened this issue Feb 22, 2014 · 6 comments

Comments

@MacGritsch
Copy link
Contributor

Hi,

also under Windows SO_REUSEADDR is needed, otherwise you can not bind if you are restarting a crashed service.

http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t

2341c2341
< #ifndef WIN32

---
> //#ifndef WIN32 //needed for windows too
2351c2351
< #endif

---
> //#endif

filemerge

@warmcat
Copy link
Collaborator

warmcat commented Feb 22, 2014

On 22/02/14 17:43, the mail apparently from MacGritsch included:

Hi,

also under Windows SO_REUSEADDR is needed, otherwise you can not bind if you are restarting a crashed service.

http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t

2341c2341

< #ifndef WIN32

//#ifndef WIN32 //needed for windows too
2351c2351

< #endif

//#endif

I'm reliant on Windows users to send patches, I don't use it.

From the comment it looks like I or somebody else found it made trouble
before by not following normal semantics with that flag.

If it works properly now (or always did...) then fine -->

http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=86267d94ad5c552fddc9965e9d2f86f6f77989e2

-Andy


Reply to this email directly or view it on GitHub:
#65

@flx5
Copy link

flx5 commented Apr 19, 2014

Hi,
setting this option on windows may lead to strange behaviour:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621%28v=vs.85%29.aspx

"Once the second socket has successfully bound, the behavior for all sockets bound to that port is indeterminate. For example, if all of the sockets on the same port provide TCP service, any incoming TCP connection requests over the port cannot be guaranteed to be handled by the correct socket — the behavior is non-deterministic"

On linux although this flag is enabled, the server will not be able to start twice and fail with "ERROR on binding to port ...", which in my opinion is the intended behaviour.

But on windows the server will start twice and as stated on the website of microsoft,
this will lead to undefined behaviour. In my test, the server started first kept accepting connections, while the second server did nothing. After killing the first server, the second accepted the connections.

@MacGritsch
Copy link
Contributor Author

ok, but how to solve then the problem that you can not listen to the same port if the old service crashed?

@flx5
Copy link

flx5 commented Apr 22, 2014

I've removed the REUSE for windows and tried it, but I wasn't able to reproduce this problem.

Could you please give me some information on how I can reproduce such a crash?
Because when I tried to just kill the process with the Task Manager I could immediately restart the server.

--- EDIT

I just had a look at the Apache HTTPD project and how they handle this:

#ifndef WIN32
    stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
    if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
        ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00067)
                      "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
                      server->bind_addr);
        apr_socket_close(s);
        return stat;
    }
#endif

(Source: http://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x/server/listen.c)

It looks like they only enable the REUSE on linux, but not on windows.

@cameronelliott
Copy link

Hi, been researching this intently, for another reason.
I understand REUSE is popular under *nix because it prevents re-binding process restart errors for processes stuck in TIME_WAIT.
Supposedly Windows does not have this issue of preventing socket binding for socket addr/port combinations in TIME_WAIT.
Apparently there is little value to using REUSE under Windows except in cases where you are using multicast.

@warmcat
Copy link
Collaborator

warmcat commented Feb 19, 2015

Well if it makes trouble and is happy without it, provide a patch to disable ifdef WIN32... all the windows stuff is contributed. The thing is if another windows guy says that patch makes trouble I can't judge it since I don't use it.

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

No branches or pull requests

3 participants