From 8d2d3744f3486806675182fced75551fc3a59e9f Mon Sep 17 00:00:00 2001 From: Kamil Krzywannski Date: Sun, 12 Jul 2026 11:00:03 +0200 Subject: [PATCH] Fix flaky preferCallConnectionOverDeferredConnection test Stall IPv6 by destination address in Socket.connect() instead of the first createSocket() call, which raced under concurrent connects. Fixes #9369 --- .../kotlin/okhttp3/FastFallbackTest.kt | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/okhttp/src/jvmTest/kotlin/okhttp3/FastFallbackTest.kt b/okhttp/src/jvmTest/kotlin/okhttp3/FastFallbackTest.kt index 1307ecaaf8c3..07a21c559ab9 100644 --- a/okhttp/src/jvmTest/kotlin/okhttp3/FastFallbackTest.kt +++ b/okhttp/src/jvmTest/kotlin/okhttp3/FastFallbackTest.kt @@ -23,7 +23,9 @@ import java.io.IOException import java.net.Inet4Address import java.net.Inet6Address import java.net.InetAddress +import java.net.InetSocketAddress import java.net.Socket +import java.net.SocketAddress import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import javax.net.SocketFactory @@ -280,18 +282,23 @@ class FastFallbackTest { serverIpv4.protocols = listOf(Protocol.H2_PRIOR_KNOWLEDGE) serverIpv6.protocols = listOf(Protocol.H2_PRIOR_KNOWLEDGE) - // Yield the first IP address so the second IP address completes first. - val firstConnectLatch = CountDownLatch(1) + // Stall IPv6 so the IPv4 address completes first. + val ipv6ConnectLatch = CountDownLatch(1) val socketFactory = object : DelegatingSocketFactory(SocketFactory.getDefault()) { - var first = true - override fun createSocket(): Socket { - if (first) { - first = false - firstConnectLatch.await() + return object : Socket() { + override fun connect( + endpoint: SocketAddress, + timeout: Int, + ) { + val address = (endpoint as? InetSocketAddress)?.address + if (address == localhostIpv6) { + ipv6ConnectLatch.await() + } + super.connect(endpoint, timeout) + } } - return super.createSocket() } } @@ -305,7 +312,7 @@ class FastFallbackTest { try { chain.proceed(chain.request()) } finally { - firstConnectLatch.countDown() + ipv6ConnectLatch.countDown() } }, ).build()