diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java index 8fb74d3f1b5..84cec601ccb 100644 --- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java +++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java @@ -106,6 +106,7 @@ final class OkHttpServerTransport implements ServerTransport, private final KeepAliveEnforcer keepAliveEnforcer; private final Object lock = new Object(); + private final Object handShakeLock = new Object(); @GuardedBy("lock") private boolean abruptShutdown; @GuardedBy("lock") @@ -164,11 +165,13 @@ private void startIo(SerializingExecutor serializingExecutor) { // The socket implementation is lazily initialized, but had broken thread-safety // for that laziness https://bugs.openjdk.org/browse/JDK-8278326. // As a workaround, we lock to synchronize initialization with shutdown(). + HandshakerSocketFactory.HandshakeResult result; synchronized (lock) { socket.setTcpNoDelay(true); } - HandshakerSocketFactory.HandshakeResult result = - config.handshakerSocketFactory.handshake(socket, Attributes.EMPTY); + synchronized (handShakeLock) { + result = config.handshakerSocketFactory.handshake(socket, Attributes.EMPTY); + } synchronized (lock) { this.socket = result.socket; } diff --git a/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java b/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java index 7347399bfe5..89ef8c6e8ce 100644 --- a/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java +++ b/okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java @@ -18,6 +18,7 @@ import static com.google.common.base.Charsets.UTF_8; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static io.grpc.internal.ClientStreamListener.RpcProgress.MISCARRIED; import static io.grpc.internal.ClientStreamListener.RpcProgress.PROCESSED; import static io.grpc.internal.ClientStreamListener.RpcProgress.REFUSED; @@ -114,6 +115,7 @@ import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import java.util.stream.Collectors; import javax.annotation.Nullable; import javax.net.SocketFactory; import okio.Buffer; @@ -293,7 +295,10 @@ public void close() throws SecurityException { Buffer buffer = createMessageFrame(message); frameHandler().data(false, 3, buffer, (int) buffer.size(), (int) buffer.size()); - assertThat(logs).hasSize(1); + + assertWithMessage("log messages: " + + logs.stream().map(LogRecord::getMessage).collect(Collectors.toList())) + .that(logs).hasSize(1); log = logs.remove(0); assertThat(log.getMessage()).startsWith(Direction.INBOUND + " DATA: streamId=" + 3); assertThat(log.getLevel()).isEqualTo(Level.FINE); diff --git a/okhttp/src/test/java/io/grpc/okhttp/TlsTest.java b/okhttp/src/test/java/io/grpc/okhttp/TlsTest.java index a21360a89ba..ffc2486871a 100644 --- a/okhttp/src/test/java/io/grpc/okhttp/TlsTest.java +++ b/okhttp/src/test/java/io/grpc/okhttp/TlsTest.java @@ -117,6 +117,7 @@ public void mtls_succeeds() throws Exception { ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds)); SimpleServiceGrpc.newBlockingStub(channel).unaryRpc(SimpleRequest.getDefaultInstance()); + server.shutdown(); } @Test @@ -144,6 +145,7 @@ public void untrustedClient_fails() throws Exception { ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds)); assertRpcFails(channel); + server.shutdown(); } @Test @@ -168,6 +170,7 @@ public void missingOptionalClientCert_succeeds() throws Exception { ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds)); SimpleServiceGrpc.newBlockingStub(channel).unaryRpc(SimpleRequest.getDefaultInstance()); + server.shutdown(); } @Test @@ -192,6 +195,7 @@ public void missingRequiredClientCert_fails() throws Exception { ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds)); assertRpcFails(channel); + server.shutdown(); } @Test @@ -208,6 +212,7 @@ public void untrustedServer_fails() throws Exception { ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds)); assertRpcFails(channel); + server.shutdown(); } @Test @@ -231,6 +236,7 @@ public void unmatchedServerSubjectAlternativeNames_fails() throws Exception { .build()); assertRpcFails(channel); + server.shutdown(); } @Test @@ -255,6 +261,7 @@ public void hostnameVerifierTrusts_succeeds() .build()); SimpleServiceGrpc.newBlockingStub(channel).unaryRpc(SimpleRequest.getDefaultInstance()); + server.shutdown(); } @Test @@ -280,6 +287,7 @@ public void hostnameVerifierFails_fails() Status status = assertRpcFails(channel); assertThat(status.getCause()).isInstanceOf(SSLPeerUnverifiedException.class); + server.shutdown(); } private static Server server(ServerCredentials creds) throws IOException {