Skip to content

Commit

Permalink
okhttp: Workaround SSLSocket not noticing socket is closed
Browse files Browse the repository at this point in the history
Using --runs_per_test=1000, this changes the flake rate of TlsTest from
2% to 0%.

While I believe it is possible to write a reliable test for this
(including noticing the SSLSocket behavior), it was becoming too
invasive so I gave up.

Fixes #11012
  • Loading branch information
ejona86 committed Jun 6, 2024
1 parent 0a4df9f commit a28357e
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.grpc.okhttp.internal.framed.Variant;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -170,6 +171,13 @@ private void startIo(SerializingExecutor serializingExecutor) {
HandshakerSocketFactory.HandshakeResult result =
config.handshakerSocketFactory.handshake(socket, Attributes.EMPTY);
synchronized (lock) {
if (socket.isClosed()) {
// The wrapped socket may not handle the underlying socket being closed by shutdown(). In
// particular, SSLSocket hangs future reads if the underlying socket is already closed at
// this point, even if you call sslSocket.close() later.
result.socket.close();
throw new SocketException("Socket close raced with handshake");
}
this.socket = result.socket;
}
this.attributes = result.attributes;
Expand Down

0 comments on commit a28357e

Please sign in to comment.