Skip to content

Commit cf08c03

Browse files
committed
fix: close frameReader before socket in stopIfNecessary to prevent TSAN data race (#10294)
1 parent 6ccd065 commit cf08c03

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,15 @@ private void stopIfNecessary() {
12071207
frameWriter.goAway(0, ErrorCode.NO_ERROR, new byte[0]);
12081208
}
12091209

1210-
// We will close the underlying socket in the writing thread to break out the reader
1211-
// thread, which will close the frameReader and notify the listener.
1210+
// Close the frameReader first to stop the ClientFrameHandler thread, then
1211+
// close the frameWriter (which closes the underlying socket through the
1212+
// writing thread). This prevents a TSAN data race between the socket close
1213+
// and the ClientFrameHandler's read from the same socket.
1214+
try {
1215+
clientFrameHandler.frameReader.close();
1216+
} catch (IOException e) {
1217+
log.log(java.util.logging.Level.FINE, "Exception closing frame reader", e);
1218+
}
12121219
frameWriter.close();
12131220
}
12141221

0 commit comments

Comments
 (0)