Skip to content

Commit

Permalink
stub: Have ClientCalls.ThreadlessExecutor reject Runnables after end …
Browse files Browse the repository at this point in the history
…of RPC

Changes originally proposed as part of #7106.

Fixes #3557
  • Loading branch information
njhill committed Feb 23, 2021
1 parent 2bfa003 commit 61e0f30
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions stub/src/main/java/io/grpc/stub/ClientCalls.java
Expand Up @@ -39,6 +39,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -161,6 +162,7 @@ public static <ReqT, RespT> RespT blockingUnaryCall(
// Something very bad happened. All bets are off; it may be dangerous to wait for onClose().
throw cancelThrow(call, e);
} finally {
executor.shutdown();
if (interrupt) {
Thread.currentThread().interrupt();
}
Expand Down Expand Up @@ -626,6 +628,9 @@ private Object waitForNext() {
// Now wait for onClose() to be called, so interceptors can clean up
}
}
if (next == this || next instanceof StatusRuntimeException) {
threadless.shutdown();
}
return next;
}
} finally {
Expand Down Expand Up @@ -712,7 +717,10 @@ private static final class ThreadlessExecutor extends ConcurrentLinkedQueue<Runn
implements Executor {
private static final Logger log = Logger.getLogger(ThreadlessExecutor.class.getName());

private volatile Thread waiter;
private static final Object SHUTDOWN = new Object(); // sentinel

// Set to the calling thread while it's parked, SHUTDOWN on RPC completion
private volatile Object waiter;

// Non private to avoid synthetic class
ThreadlessExecutor() {}
Expand All @@ -736,14 +744,29 @@ public void waitAndDrain() throws InterruptedException {
}
}
do {
try {
runnable.run();
} catch (Throwable t) {
log.log(Level.WARNING, "Runnable threw exception", t);
}
runQuietly(runnable);
} while ((runnable = poll()) != null);
}

/**
* Called after final call to {@link #waitAndDrain()}, from same thread.
*/
public void shutdown() {
waiter = SHUTDOWN;
Runnable runnable;
while ((runnable = poll()) != null) {
runQuietly(runnable);
}
}

private static void runQuietly(Runnable runnable) {
try {
runnable.run();
} catch (Throwable t) {
log.log(Level.WARNING, "Runnable threw exception", t);
}
}

private static void throwIfInterrupted() throws InterruptedException {
if (Thread.interrupted()) {
throw new InterruptedException();
Expand All @@ -753,7 +776,12 @@ private static void throwIfInterrupted() throws InterruptedException {
@Override
public void execute(Runnable runnable) {
add(runnable);
LockSupport.unpark(waiter); // no-op if null
Object waiter = this.waiter;
if (waiter != SHUTDOWN) {
LockSupport.unpark((Thread) waiter); // no-op if null
} else if (remove(runnable)) {
throw new RejectedExecutionException();
}
}
}

Expand Down

0 comments on commit 61e0f30

Please sign in to comment.