Don't attempt to call getsockname before binding an async socket
authorChristopher Dykes <cdykes@fb.com>
Wed, 10 Aug 2016 20:15:18 +0000 (13:15 -0700)
committerFacebook Github Bot 0 <facebook-github-bot-0-bot@fb.com>
Wed, 10 Aug 2016 20:23:31 +0000 (13:23 -0700)
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

folly/io/async/AsyncServerSocket.cpp

index ee1239d52d0679bb57fdb1a9081b695585bab295..cbabc6f429ea2881b311003086e281a8f0136997 100644 (file)
@@ -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, "<unknown>"));
     }
+
+    SocketAddress address;
+    address.setFromLocalAddress(s);
+
+    sockets_.emplace_back(eventBase_, s, this, address.getFamily());
   };
 
   const int kNumTries = 25;