From cf08c0359f2ca4225b25236e57a32bd2e3285f6f Mon Sep 17 00:00:00 2001 From: water <672684719@qq.com> Date: Wed, 29 Jul 2026 22:13:55 +0800 Subject: [PATCH] fix: close frameReader before socket in stopIfNecessary to prevent TSAN data race (#10294) --- .../java/io/grpc/okhttp/OkHttpClientTransport.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java index 4764a6a1387..a4d44361c86 100644 --- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java +++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java @@ -1207,8 +1207,15 @@ private void stopIfNecessary() { frameWriter.goAway(0, ErrorCode.NO_ERROR, new byte[0]); } - // We will close the underlying socket in the writing thread to break out the reader - // thread, which will close the frameReader and notify the listener. + // Close the frameReader first to stop the ClientFrameHandler thread, then + // close the frameWriter (which closes the underlying socket through the + // writing thread). This prevents a TSAN data race between the socket close + // and the ClientFrameHandler's read from the same socket. + try { + clientFrameHandler.frameReader.close(); + } catch (IOException e) { + log.log(java.util.logging.Level.FINE, "Exception closing frame reader", e); + } frameWriter.close(); }