Skip to content

Commit

Permalink
skip setting SO_REUSEADDR when socket is AF_UNIX
Browse files Browse the repository at this point in the history
Summary:
After D38076789 (4a18fd1), we started to actually set `SO_REUSEADDR` on Windows. This has broken our build because we are using `folly::AsyncServerSocket` with a UNIX Domain Socket address on Windows. Setting `SO_REUSEADDR` on the socket confuses Windows and making it failed to bind.

It looks like we can safely skip setting `SO_REUSEADDR` when the address family is `AF_UNIX`. Neither Linux nor BSD supports it.

https://gavv.net/articles/unix-socket-reuse/
https://stackoverflow.com/a/15717204

Reviewed By: kvtsoy

Differential Revision: D38515166

fbshipit-source-id: 3dfc61ae2f5173e4750226f88fa6c32c50f76ee3
  • Loading branch information
fanzeyi authored and facebook-github-bot committed Aug 9, 2022
1 parent a8834cc commit d41d59b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions folly/io/async/AsyncServerSocket.cpp
Expand Up @@ -829,8 +829,10 @@ void AsyncServerSocket::setupSocket(NetworkSocket fd, int family) {

// Set reuseaddr to avoid 2MSL delay on server restart
int one = 1;
if (netops::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) !=
0) {
// AF_UNIX does not support SO_REUSEADDR, setting this would confuse Windows
if (family != AF_UNIX &&
netops::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) !=
0) {
auto errnoCopy = errno;
// This isn't a fatal error; just log an error message and continue
LOG(ERROR) << "failed to set SO_REUSEADDR on async server socket "
Expand Down

0 comments on commit d41d59b

Please sign in to comment.