Skip to content

Commit a28357e

Browse files
committedJun 6, 2024·
okhttp: Workaround SSLSocket not noticing socket is closed
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
1 parent 0a4df9f commit a28357e

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed
 

‎okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import io.grpc.okhttp.internal.framed.Variant;
5151
import java.io.IOException;
5252
import java.net.Socket;
53+
import java.net.SocketException;
5354
import java.util.List;
5455
import java.util.Locale;
5556
import java.util.Map;
@@ -170,6 +171,13 @@ private void startIo(SerializingExecutor serializingExecutor) {
170171
HandshakerSocketFactory.HandshakeResult result =
171172
config.handshakerSocketFactory.handshake(socket, Attributes.EMPTY);
172173
synchronized (lock) {
174+
if (socket.isClosed()) {
175+
// The wrapped socket may not handle the underlying socket being closed by shutdown(). In
176+
// particular, SSLSocket hangs future reads if the underlying socket is already closed at
177+
// this point, even if you call sslSocket.close() later.
178+
result.socket.close();
179+
throw new SocketException("Socket close raced with handshake");
180+
}
173181
this.socket = result.socket;
174182
}
175183
this.attributes = result.attributes;

0 commit comments

Comments
 (0)
Please sign in to comment.