Skip to content

Commit d552136

Browse files
committed
address comments from @dapengzhang0 and @carl-mastrangelo
including interruption handling fix
1 parent c4149a2 commit d552136

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

stub/src/main/java/io/grpc/stub/ClientCalls.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,12 +642,15 @@ private static final class ThreadlessExecutor extends ConcurrentLinkedQueue<Runn
642642
* Waits until there is a Runnable, then executes it and all queued Runnables after it.
643643
*/
644644
public void waitAndDrain() throws InterruptedException {
645+
final Thread currentThread = Thread.currentThread();
646+
throwIfInterrupted(currentThread);
645647
Runnable runnable = poll();
646648
if (runnable == null) {
647-
waiter = Thread.currentThread();
649+
waiter = currentThread;
648650
try {
649651
while ((runnable = poll()) == null) {
650652
LockSupport.park(this);
653+
throwIfInterrupted(currentThread);
651654
}
652655
} finally {
653656
waiter = null;
@@ -662,13 +665,16 @@ public void waitAndDrain() throws InterruptedException {
662665
} while ((runnable = poll()) != null);
663666
}
664667

668+
private static void throwIfInterrupted(Thread currentThread) throws InterruptedException {
669+
if (currentThread.isInterrupted()) {
670+
throw new InterruptedException();
671+
}
672+
}
673+
665674
@Override
666675
public void execute(Runnable runnable) {
667676
add(runnable);
668-
final Thread waitingThread = waiter;
669-
if (waitingThread != null) {
670-
LockSupport.unpark(waitingThread);
671-
}
677+
LockSupport.unpark(waiter); // no-op if null
672678
}
673679
}
674680
}

0 commit comments

Comments
 (0)