shinseongsu commented on PR #936: URL: https://github.com/apache/httpcomponents-client/pull/936#issuecomment-6997973944
@arturobernalg I tried the racing path against a host where one address accepts TCP but fails the TLS handshake, and wanted to check whether this is intended. With two addresses, where the first completes the TCP handshake but the TLS handshake fails and the second is healthy: - Happy Eyeballs off: the TLS failure on the first address is caught in the sequential loop and the second address is used, same as on master. - Happy Eyeballs on: the first address wins the race as soon as TCP connects, the other attempts are cancelled, and the TLS failure goes straight to the caller. The second address is never tried. The winner is picked on TCP connect in `HappyEyeballsSocketConnector` ([L187](https://github.com/arturobernalg/httpcomponents-client/blob/38e8db54f348bbc87c037bc0a460be5b737a0633/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/HappyEyeballsSocketConnector.java#L187)). `upgradeToTls` then runs once outside the race ([L272](https://github.com/arturobernalg/httpcomponents-client/blob/38e8db54f348bbc87c037bc0a460be5b737a0633/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java#L272)), and its catch closes the socket and rethrows. I measured it end to end with the classic client, a new connection per request. DNS returns `[127.0.0.1, ::1]` on the same port. 127.0.0.1 accepts TCP and then resets or stalls, and ::1 is a working TLS server. | | master | this PR, HE off | this PR, HE on | |---|---|---|---| | 127.0.0.1 resets after accept (100 requests) | 100 OK | 100 OK | 0 OK, 100 `SocketException` | | 127.0.0.1 stalls the handshake (10 requests) | 10 OK | 10 OK | 0 OK, 10 `SocketTimeoutException` | A packet capture of the HE-on runs shows no SYN to ::1 at all. With HE off, every request goes on to ::1 and completes the handshake there. Here is a test that runs the same case against the operator directly. It passes with `happyEyeballs=false` and fails with `happyEyeballs=true` (`IOException: handshake failed on localhost/127.0.0.1:...`): <details> <summary>HappyEyeballsTlsFallbackProbeTest</summary> ```java @ParameterizedTest(name = "happyEyeballs={0}") @ValueSource(booleans = {false, true}) void tlsFailureOnFirstAddressFallsBackToNextAddress(final boolean happyEyeballs) throws Exception { try (ServerSocket broken = new ServerSocket(0, 50, InetAddress.getLoopbackAddress()); ServerSocket healthy = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) { final List<InetSocketAddress> remoteAddresses = Arrays.asList( new InetSocketAddress(InetAddress.getLoopbackAddress(), broken.getLocalPort()), new InetSocketAddress(InetAddress.getLoopbackAddress(), healthy.getLocalPort())); final DnsResolver dnsResolver = Mockito.mock(DnsResolver.class); Mockito.when(dnsResolver.resolve(Mockito.anyString(), Mockito.anyInt())).thenReturn(remoteAddresses); final AtomicInteger handshakes = new AtomicInteger(); final AtomicInteger upgradedPort = new AtomicInteger(); final TlsSocketStrategy tls = (socket, target, port, attachment, context) -> { handshakes.incrementAndGet(); if (socket.getPort() == broken.getLocalPort()) { throw new IOException("handshake failed on " + socket.getRemoteSocketAddress()); } upgradedPort.set(socket.getPort()); return Mockito.mock(SSLSocket.class); }; final Lookup<TlsSocketStrategy> tlsLookup = scheme -> tls; final DefaultHttpClientConnectionOperator operator = new DefaultHttpClientConnectionOperator( proxy -> new Socket(), DefaultSchemePortResolver.INSTANCE, dnsResolver, tlsLookup, happyEyeballs, TimeValue.ofMilliseconds(250)); final DefaultManagedHttpClientConnection conn = new DefaultManagedHttpClientConnection( "test", null, null, null, null, null, null, null, null); operator.connect(conn, new HttpHost("https", "somehost", 443), null, TimeValue.ofSeconds(5), SocketConfig.DEFAULT, HttpClientContext.create()); assertEquals(healthy.getLocalPort(), upgradedPort.get()); assertEquals(2, handshakes.get()); } } ``` </details> I can see arguments for this being intended. RFC 8305 section 5 counts an attempt as successful once the TCP handshake completes, and section 9 leaves TLS failures out of scope. `happyEyeballsRacing_tlsHandshakeFailure_closesWinningSocket` also expects the IOException. The async client on master doesn't move to the next address after a TLS failure either. On the other hand, with the classic client enabling Happy Eyeballs makes this case fail where the default path succeeds. OkHttp's `FastFallbackExchangeFinder` races on TCP the same way, but it defers the losing attempts and goes on to them when `connectTlsEtc()` fails on the winner. Is the HE-on behavior what you want here? If so, maybe it's worth mentioning in the javadoc for `setHappyEyeballsEnabled` that a TLS failure on the winning address isn't retried on the other addresses. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
