2626import io .grpc .InternalLogId ;
2727import io .grpc .servlet .ServletServerStream .ServletTransportState ;
2828import java .io .IOException ;
29- import java .time .Duration ;
3029import java .util .Queue ;
3130import java .util .concurrent .ConcurrentLinkedQueue ;
31+ import java .util .concurrent .TimeUnit ;
3232import java .util .concurrent .atomic .AtomicReference ;
3333import java .util .concurrent .locks .LockSupport ;
3434import java .util .function .BiFunction ;
@@ -128,7 +128,7 @@ public void finest(String str, Object... params) {
128128 log .fine ("call completed" );
129129 });
130130 };
131- this .isReady = () -> outputStream . isReady () ;
131+ this .isReady = outputStream :: isReady ;
132132 }
133133
134134 /**
@@ -173,7 +173,9 @@ void complete() {
173173 /** Called from the container thread {@link javax.servlet.WriteListener#onWritePossible()}. */
174174 void onWritePossible () throws IOException {
175175 log .finest ("onWritePossible: ENTRY. The servlet output stream becomes ready" );
176- assureReadyAndDrainedTurnsFalse ();
176+ if (writeState .get ().readyAndDrained ) {
177+ assureReadyAndDrainedTurnsFalse ();
178+ }
177179 while (isReady .getAsBoolean ()) {
178180 WriteState curState = writeState .get ();
179181
@@ -200,11 +202,9 @@ private void assureReadyAndDrainedTurnsFalse() {
200202 // readyAndDrained should have been set to false already.
201203 // Just in case due to a race condition readyAndDrained is still true at this moment and is
202204 // being set to false by runOrBuffer() concurrently.
205+ parkingThread = Thread .currentThread ();
203206 while (writeState .get ().readyAndDrained ) {
204- parkingThread = Thread .currentThread ();
205- // Try to sleep for an extremely long time to avoid writeState being changed at exactly
206- // the time when sleep time expires (in extreme scenario, such as #9917).
207- LockSupport .parkNanos (Duration .ofHours (1 ).toNanos ()); // should return immediately
207+ LockSupport .parkNanos (TimeUnit .MINUTES .toNanos (1 )); // should return immediately
208208 }
209209 parkingThread = null ;
210210 }
@@ -254,7 +254,7 @@ interface ActionItem {
254254 @ VisibleForTesting // Lincheck test can not run with java.util.logging dependency.
255255 interface Log {
256256 default boolean isLoggable (Level level ) {
257- return false ;
257+ return false ;
258258 }
259259
260260 default void fine (String str , Object ...params ) {}
0 commit comments