Recent versions of the Linux kernel (after commit
5b0af621c3f6ef9261cf6067812f2fd9943acb4b) return -1 with errno set to
EOPNOTSUPP when a process attempts to set SO_REUSEPORT on a UNIX
domain socket. We started setting this flag in commit
c423f13cc2f3fbdbf48390535cb1629b99b0eb27 to work around a test suite
issue on macOS. Restrict setting SO_REUSEPORT to macOS to avoid
unnecessary interactions with its behavior on other platforms.
abort();
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0)
abort();
-#ifdef SO_REUSEPORT
+#if defined(SO_REUSEPORT) && defined(__APPLE__)
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) != 0)
abort();
#endif
st = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
if (st)
return st;
-#ifdef SO_REUSEPORT
+#if defined(SO_REUSEPORT) && defined(__APPLE__)
+ /* macOS experimentally needs this flag as well to avoid conflicts between
+ * recently exited server processes and new ones. */
st = setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &value, sizeof(value));
if (st)
return st;