Fix AsyncServletOutputStreamWriterConcurrencyTest flakiness (#9948)

The commit 792946132c (diff-cc7b2eb82d208e027f432435bcd324a46713c31096352f437417b770752f92abR197) makes it possible that the sleep can naturally wake up while `writeState` gets changes at the same time, causing a data race in the value of `parkingThread` between

792946132c/servlet/src/main/java/io/grpc/servlet/AsyncServletOutputStreamWriter.java (L199)

and 

792946132c/servlet/src/main/java/io/grpc/servlet/AsyncServletOutputStreamWriter.java (L218)

, in extreme scenario such as the CPU is stressed.

Fixes #9917
This commit is contained in:
ZHANG Dapeng 2023-03-24 11:20:38 -07:00 committed by GitHub
parent 687340bbbe
commit 85e656c0dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -194,7 +194,9 @@ final class AsyncServletOutputStreamWriter {
// being set to false by runOrBuffer() concurrently.
while (writeState.get().readyAndDrained) {
parkingThread = Thread.currentThread();
LockSupport.parkNanos(Duration.ofMinutes(1).toNanos()); // should return immediately
// Try to sleep for an extremely long time to avoid writeState being changed at exactly
// the time when sleep time expires (in extreme scenario, such as #9917).
LockSupport.parkNanos(Duration.ofHours(1).toNanos()); // should return immediately
}
parkingThread = null;
}