Skip to content

Commit

Permalink
core: logging the error message when onClose() itself fails (#11880)
Browse files Browse the repository at this point in the history
  • Loading branch information
NaveenPrasannaV authored Feb 11, 2025
1 parent dc316f7 commit 302342c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/java/io/grpc/internal/ClientCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,11 @@ public Attributes getAttributes() {
}

private void closeObserver(Listener<RespT> observer, Status status, Metadata trailers) {
observer.onClose(status, trailers);
try {
observer.onClose(status, trailers);
} catch (RuntimeException ex) {
log.log(Level.WARNING, "Exception thrown by onClose() in ClientCall", ex);
}
}

@Override
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/io/grpc/internal/ClientCallImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,32 @@ public void getAttributes() {
assertEquals(attrs, call.getAttributes());
}

@Test
public void onCloseExceptionCaughtAndLogged() {
DelayedExecutor executor = new DelayedExecutor();
ClientCallImpl<Void, Void> call = new ClientCallImpl<>(
method,
executor,
baseCallOptions,
clientStreamProvider,
deadlineCancellationExecutor,
channelCallTracer, configSelector);

call.start(callListener, new Metadata());
verify(stream).start(listenerArgumentCaptor.capture());
final ClientStreamListener streamListener = listenerArgumentCaptor.getValue();
streamListener.headersRead(new Metadata());

doThrow(new RuntimeException("Exception thrown by onClose() in ClientCall")).when(callListener)
.onClose(any(Status.class), any(Metadata.class));

Status status = Status.RESOURCE_EXHAUSTED.withDescription("simulated");
streamListener.closed(status, PROCESSED, new Metadata());
executor.release();

verify(callListener).onClose(same(status), any(Metadata.class));
}

private static final class DelayedExecutor implements Executor {
private final BlockingQueue<Runnable> commands = new LinkedBlockingQueue<>();

Expand Down

0 comments on commit 302342c

Please sign in to comment.