Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,9 @@ void notifyTerminated() {
readyTimeoutFuture.cancel(false);
readyTimeoutFuture = null;
}
if (preAuthResultFuture != null) {
preAuthResultFuture.cancel(false); // No effect if already complete.
}
if (authResultFuture != null) {
authResultFuture.cancel(false); // No effect if already complete.
}
cancelAsyncIfNeeded(preAuthResultFuture);
cancelAsyncIfNeeded(authResultFuture);

serviceBinding.unbind();
clientTransportListener.transportTerminated();
}
Expand Down Expand Up @@ -399,6 +396,17 @@ protected void handlePingResponse(Parcel parcel) {
pingTracker.onPingResponse(parcel.readInt());
}

/**
* Enqueues a future for cancellation later, on another thread.
* Useful when the caller wants to cancel while holding locks but the future is visible to
* user code which might have added listeners to run on directExecutor().
*/
private void cancelAsyncIfNeeded(@Nullable ListenableFuture<?> future) {
if (future != null && !future.isDone()) {
offloadExecutor.execute(() -> future.cancel(false));
}
}

private static ClientStream newFailingClientStream(
Status failure, Attributes attributes, Metadata headers, ClientStreamTracer[] tracers) {
StatsTraceContext statsTraceContext =
Expand Down