Open Bug 489488 Opened 17 years ago Updated 4 years ago

PR_SockOpt_reuseaddr functions differently on Windows from on other OSes

Categories

(NSPR :: NSPR, defect)

x86
Windows XP
defect

Tracking

(Not tracked)

People

(Reporter: Waldo, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: autotest-issue)

On non-Windows, as near as I can tell from the research I've done, PR_SockOpt_reuseaddr = PR_TRUE is equivalent to SO_reuseaddr, which allows nearly-closed sockets to be rebound rather than waiting for a remote connection to fully terminate. A socket may *not* be bound multiple times. On Windows, hoever, the nearly-closed restriction doesn't seem to apply: http://msdn.microsoft.com/en-us/library/ms740621%28VS.85%29.aspx http://blogs.msdn.com/wndp/archive/2005/08/03/Anthony-Jones.aspx Therefore, sockets with this option may be bound multiple times on Windows, and observable behavior differs from the point of view of the code using NSPR. This currently bites Mozilla's nsServerSocket component, and as a consequence one test for httpd.js, an HTTP server that uses nsServerSocket, fails (or rather would fail, except that that test has been disabled on Windows): http://hg.mozilla.org/mozilla-central/annotate/4cb8996cd19e/netwerk/base/src/nsServerSocket.cpp#l307 (line 307) http://hg.mozilla.org/mozilla-central/annotate/639af68a74e3/netwerk/test/httpserver/test/test_start_stop.js#l114 (lines 114 and 119) The above articles suggest that on sufficiently recent versions of Windows (XPSP2 or later according to the blog post) PR_SockOpt_reuseaddr should perhaps be mapped to SO_EXCLUSIVEADDRUSE rather than SO_REUSEADDR; it might be necessary to insert a shutdown+read-until-nothing-read-loop to ensure immediate socket reusability as well, but I didn't look at NSPR source closely enough to determine where that might need to occur. I have no prior experience with winsock programming or with socket APIs on Unixy systems, so this could all be subtly wrong. Am I on track with this suggested change, or no?
> A socket may *not* be bound multiple times. First let's define socket. If you define socket as "the thing for which you get a handle back from a unix socket() call" then the statement quoted above is true. On the other hand, if you define socket as a unique set (ordered quadruplet) of values for the following variables: { IP protocol version, IP address, transport protocol, port number } e.g., {IPv4, 192.1.2.3, TCP, 1025} or {IPv6, fe80::21b:fcff:fe01, UDP, 4973} that may be bound to one of those things you get back from socket(), then the statement quoted above is not true on some (maybe all) Unix platforms. On some UNIX platforms, it is possible to - call socket() - call bind( NNNN) to bind to port NNNN for listening - call setsockopt(... SO_REUSEADDR) to allow other processes to bind to the same set of values, without restriction on the number of transport endpoints using that ordered quadruplet. (Others must set SO_REUSEADDR *before* calling bind if they wish to bind to the same ordered quadruplet.) IINM, there are popular web servers that rely on this very property to permit the server's load to be shared by multiple processes on the same box. Some OSes (e.g. IRIX, BSD) have an additional socket option, SO_REUSEPORT or similar, which also affects the ability of a second process to bind to an ordered quadruplet that is already bound. It controls this "almost closed" limitation of which you wrote. See http://www.unixguide.net/network/socketfaq/4.11.shtml Before the invention of SO_REUSEPORT in the early 1990s, all Unixes behaved just as Windows now behaves in response to SO_REUSEADDR, and I believe that on some Unixes (and maybe Linux) that is still the case. IMO the intended behavior of NSPR's PR_SockOpt_reuseaddr is to be like Windows' behavior, that is, like the old original Unix behavior for SO_REUSEADDR. I will confer with my fellow NSPR module owners on this point. Assuming that is correct, then this is actually a bug in NSPR on those flavors of Unix/Linux that implement SO_REUSEPORT. What flavor of Unix are you using, Jeff?
Jeff, the two MSDN articles you cited say SO_REUSEADDR behaves on Windows as we expect, and the Windows NT family provides a SO_EXCLUSIVEADDRUSE option, which prevents a bind call on a second socket with SO_REUSEADDR set from succeeding. So your statement that The above articles suggest that on sufficiently recent versions of Windows (...) PR_SockOpt_reuseaddr should perhaps be mapped to SO_EXCLUSIVEADDRUSE rather than SO_REUSEADDR; ... is incorrect. PR_SockOpt_reuseaddr is mapped to SO_REUSEADDR on all platforms. If your test shows that PR_SockOpt_reuseaddr doesn't behave as we expect on Windows, there are three possibilities: - PR_SockOpt_reuseaddr is not mapped to SO_REUSEADDR on Windows (an NSPR bug). The relevant NSPR code is at http://mxr.mozilla.org/nspr/ident?i=_PR_SocketSetSocketOption - SO_REUSEADDR doesn't behave as documented on Windows (a Windows bug). - Your test has a bug.
Wan-Teh, I suspect that Jeff has been testing on a BSD platform that has SO_REUSEPORT and defaults it to false. I believe that all of Jeff's claims of different behavior between his unnamed platform and Windows can be explained by that. You wrote: > PR_SockOpt_reuseaddr is mapped to SO_REUSEADDR on all platforms. That's true, but as explained, the behavior of SO_REUSEADDR is not the same on all platforms, due to the addition of SO_REUSEPORT on some platforms. That, in turn, means that PR_SockOpt_reuseaddr causes different socket behaviors on different platforms. So the questions I have for NSPR module owners are: 1) Do we want the definition of PR_SockOpt_reuseaddr to remain platform dependent? Or do we want it to try to cause socket behavior on all platforms that is as similar as possible? And if the latter, 2) Do we want the behavior of PR_SockOpt_reuseaddr to match SO_REUSEPORT being true, or do we want it to match SO_REUSEPORT being false? Perhaps all modern Unixes now support SO_REUSEPORT, and I am merely unaware of that fact. But if that is not the case, then, given that, on Unix platforms without SO_REUSEPORT, SO_REUSEADDR behaves as when SO_REUSEPORT is true on platforms that have it, and that there is no way to simulate the behavior of SO_REUSEPORT being false on systems that do not have it, I'd suggest that we define the PR_SockOpt_reuseaddr to make the socket behave as on systems that have no SO_REUSEPORT. Alternatively, we could add PR_SockOpt_reuseport, and map it to SO_EXCLUSIVEADDRUSE on Windows.
Nelson, it seems that you confused SO_REUSEADDR with SO_REUSEPORT. The reference you cited http://www.unixguide.net/network/socketfaq/4.11.shtml seems to describe the reverse (SO_REUSEADDR <-> SO_REUSEPORT) of what you described. That page says: SO_REUSEADDR allows your server to bind to an address which is in a TIME_WAIT state. It does not allow more than one server to bind to the same address. ... SO_REUSEPORT flag allows multiple processes to bind to the same address provided all of them use the SO_REUSEPORT option. You said: On some UNIX platforms, it is possible to ... - call setsockopt(... SO_REUSEADDR) to allow other processes to bind to the same set of values, without restriction on the number of transport endpoints using that ordered quadruplet. (Others must set SO_REUSEADDR *before* calling bind if they wish to bind to the same ordered quadruplet.) ... Some OSes (e.g. IRIX, BSD) have an additional socket option, SO_REUSEPORT or similar, which also affects the ability of a second process to bind to an ordered quadruplet that is already bound. It controls this "almost closed" limitation of which you wrote. If by "almost closed" you meant "in a TIME_WAIT state", then the reverse of the meanings of SO_REUSEADDR and SO_REUSEPORT between that page and your description should be obvious.
I wish to correct something I wrote in comment #1. Prior to the creation of SO_REUSEPORT, there were two ways that a quadruple could be made to be bound by multiple processes at once. They were: 1) The first process that bound to a quadruple could set the SO_REUSEADDR option on the socket it had bound, and this would make that quadruple available to be bound by any other process. The other processes that subsequently bound to that quadruple did not have to do anything special, such as set special socket options, to bind to a quadruple that had REUSEADDR set. 2) Alternatively, if the first process that bound to a quadruple did NOT set the SO_REUSEADDR option, other processes could STILL bind to it nonetheless, simply by setting the SO_REUSEADDR option on their sockets before binding to the already-in-use port. There was nothing that a program which had bound to a quadruple could do to stop another process from "sharing" that quadruple, if that other process knew how to do so. That was the reason that SO_REUSEPORT was created. The SO_REUSEPORT attribute, when false, absolutely stopped other processes cold from "sharing" a quadruple. When your process bound to a quadruple, and SO_REUSEPORT was false, your process owned that quadruple exclusively for as long as it had that socket open. Early implementations of SO_REUSEPORT differed in their default values for that attribute. For maximum backward compatibility, it defaulted to false. For maximum security, it defaulted to true.
I know about this because I was an IRIX kernel programmer in 1990-1994, and I implemented SO_REUSEPORT in the IRIX kernel. I believe IRIX's implementation of SO_REUSEPORT was the first. I may have invented it. It was necessary to have this ability to stop other processes from binding to an in-use port to get IRIX to pass a "B1" security evaluation, something like an EAL4 evaluation today. (Think FIPS 140 for Operating systems.)
In reply to comment 4. - Yes, by "almost closed" I mean "in a TIME_WAIT state". - What you describe as a reversal is merely an artifact of whether SO_REUSEPORT defaults to true or false. If it defaults to true, then the behavior is just as I described in comments above. I do not know that the default is on modern systems.
Hmm. Here's an interesting excerpt from one of my colleagues at SGI. http://bbs.actapricot.org/files/area39/net100.txt > getsockopt -- swapped functionality > > The functionality of the SO_REUSEADDR and SO_REUSEPORT options to > getsockopt(SSC) were swapped. For example, if SO_REUSEADDR was used to > bind a socket to a wildcard address and a fixed port, attempting to > repeat this while the first socket was still open would succeed. It > should fail and set EADDRINUSE (address already in use) in errno. This > supplement implements the correct behavior in libsocket.
The Unix I'm using personally is OS X 10.4, but I've seen the throw-on-bind-again behavior on every Mozilla tinderbox except for the Windows ones, and I assume everyone not on Windows is seeing throw-on-bind-again as well, else I'd have gotten a complaint about netwerk xpcshell tests not working by now. It might be because SO_REUSEPORT defaults to false on all the Unixes and only true on Windows, I guess -- I could test that on the systems to which I have access -- but it seems more than a little poor that performing the exact same sequence of API calls doesn't have the same results/side effects on different OSes, but maybe I'm expecting something of NSPR that it just didn't intend to support. I refer people to the last paragraph of comment 0 in response to the "you're wrong/mistaken" responses. :-)
Jeff: I understand the SO_REUSEADDR problem you described now. Sorry about the confusion. I'm afraid that we may not be able to come up with a perfect solution in NSPR to this problem. We may be able to do only the following in NSPR: - Document that PR_SockOpt_reuseaddr is mapped to SO_REUSEADDR, and warn that SO_REUSEADDR behaves differently on Windows. - Point to the documentation of SO_EXCLUSIVEADDRUSE and SO_REUSEPORT and explain how an NSPR-based application can call PR_FileDesc2NativeHandle and set these socket options on the native file descriptors. - Investigate whether it's possible to emulate SO_EXCLUSIVEADDRUSE with SO_REUSEPORT, or vice versa. If so, we can add a new NSPR socket option for the option that exists or can be emulated on all platforms. This may be a case where the platforms' offerings are so different that NSPR can't possibly provide a uniform interface, but most applications can tolerate some minor differences between platforms and can meet their requirements with platform-specific code.
I would add a fourth possible option to Wan-Teh's list of 3, one that is not mutually exclusive with the 3rd. - Change PR_SockOpt_reuseaddr to ALSO invoke SO_REUSEPORT on unix, and/or also invoke SO_EXCLUSIVEADDRUSE, to make the behaviors of NSPR sockets on the two platforms match each other as closely as possible. This may also necessitate setting those options on new NSPR sockets on one or both platforms, so that socket behavior is as similar as possible even when PR_SockOpt_reuseaddr has not been explicitly used by the application.

The bug assignee is inactive on Bugzilla, so the assignee is being reset.

Assignee: wtc → nobody
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.