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
This commit is contained in:
Eric Anderson 2024-06-05 17:01:38 -07:00
parent 0a4df9f93c
commit a28357e197
1 changed files with 8 additions and 0 deletions

View File

@ -50,6 +50,7 @@ import io.grpc.okhttp.internal.framed.Settings;
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;
@ -170,6 +171,13 @@ final class OkHttpServerTransport implements ServerTransport,
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;