From: Christopher Dykes Date: Wed, 10 Aug 2016 20:15:18 +0000 (-0700) Subject: Don't attempt to call getsockname before binding an async socket X-Git-Tag: v2016.08.15.00~22 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=76a4de976ad66df26aedd949a2c62e19d88ceb30;p=folly.git Don't attempt to call getsockname before binding an async socket Summary: With WinSock, calling `getsockname` on a socket before it's been bound will always result in an error, so make sure to bind the socket first. Reviewed By: yfeldblum Differential Revision: D3698112 fbshipit-source-id: e9efe05323b242add3808ee1a6fec2593beb04ac --- diff --git a/folly/io/async/AsyncServerSocket.cpp b/folly/io/async/AsyncServerSocket.cpp index ee1239d5..cbabc6f4 100644 --- a/folly/io/async/AsyncServerSocket.cpp +++ b/folly/io/async/AsyncServerSocket.cpp @@ -392,11 +392,6 @@ void AsyncServerSocket::bind(uint16_t port) { &v6only, sizeof(v6only))); } - SocketAddress address; - address.setFromLocalAddress(s); - - sockets_.emplace_back(eventBase_, s, this, address.getFamily()); - // Bind to the socket if (fsp::bind(s, res->ai_addr, res->ai_addrlen) != 0) { folly::throwSystemError( @@ -406,6 +401,11 @@ void AsyncServerSocket::bind(uint16_t port) { " family ", SocketAddress::getFamilyNameFrom(res->ai_addr, "")); } + + SocketAddress address; + address.setFromLocalAddress(s); + + sockets_.emplace_back(eventBase_, s, this, address.getFamily()); }; const int kNumTries = 25;