[Lldb-commits] [PATCH] D79757: Try IPv4 before IPv6 when creating TCP connection

Emre Kultursay via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon May 11 17:50:59 PDT 2020

emrekultursay created this revision.
Herald added subscribers: lldb-commits, mgrang.
Herald added a project: LLDB.

When connecting to Android, LLDB calls adb#Shell 5 times. At each call,
a TCP connection to "localhost:port" needs to be established. On hosts
that support IPv4 and IPv6, the "localhost" host can map to two

It seems like IPv6 typically becomes the addresses[0], and then trying
to connect to it gets rejected (WSAECONNREFUSED), and then the IPv4 one
at addresses[1] succeeds.

Trying to connect to IPv6 first wastes up to 2 seconds per call on
Windows. The adb CLI client also uses an IPv4-first approach.

On Windows, this CL brings total of ~10 second improvement in LLDB
attach latency to Android devices.

  rG LLVM Github Monorepo



Index: lldb/source/Host/common/TCPSocket.cpp
--- lldb/source/Host/common/TCPSocket.cpp
+++ lldb/source/Host/common/TCPSocket.cpp
@@ -162,6 +162,12 @@
   std::vector<SocketAddress> addresses = SocketAddress::GetAddressInfo(
       host_str.c_str(), nullptr, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
+  // IPv4 is more likely to succeed than IPv6. To avoid costly retries,
+  // we sort based on family to prioritize IPv4 over IPv6.
+  std::sort(addresses.begin(), addresses.end(),
+            [](SocketAddress a, SocketAddress b) {
+              return a.GetFamily() < b.GetFamily();
+            });
   for (SocketAddress &address : addresses) {
     error = CreateSocket(address.GetFamily());
     if (error.Fail())

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79757.263323.patch
Type: text/x-patch
Size: 786 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200512/107d5aca/attachment-0001.bin>

More information about the lldb-commits mailing list