Skip to content

Commit abb4a2a

Browse files
authored
core: Fix a bug for exception handling at messageRead
Found this bug in some unit test in which client-streaming call is hanging because `ServerCallImpl#messageRead()` did not handle RuntimeException properly
1 parent 9606995 commit abb4a2a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core/src/main/java/io/grpc/internal/ServerCallImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,13 @@ public void messageRead(final InputStream message) {
245245
try {
246246
message.close();
247247
} catch (IOException e) {
248+
throw new RuntimeException(e);
249+
} finally {
248250
if (t != null) {
249251
// TODO(carl-mastrangelo): Maybe log e here.
250252
Throwables.propagateIfPossible(t);
251253
throw new RuntimeException(t);
252254
}
253-
throw new RuntimeException(e);
254255
}
255256
}
256257
}

core/src/test/java/io/grpc/internal/ServerCallImplTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.junit.Assert.assertNull;
3838
import static org.junit.Assert.assertTrue;
3939
import static org.junit.Assert.fail;
40+
import static org.mockito.Matchers.any;
4041
import static org.mockito.Matchers.isA;
4142
import static org.mockito.Mockito.doThrow;
4243
import static org.mockito.Mockito.verify;
@@ -311,6 +312,20 @@ public void streamListener_messageRead_onlyOnce() {
311312
verify(callListener).onMessage(1234L);
312313
}
313314

315+
@Test
316+
public void streamListener_unexpectedRuntimeException() {
317+
ServerStreamListenerImpl<Long> streamListener =
318+
new ServerCallImpl.ServerStreamListenerImpl<Long>(
319+
call, callListener, context, statsTraceCtx);
320+
doThrow(new RuntimeException("unexpected exception"))
321+
.when(callListener)
322+
.onMessage(any(Long.class));
323+
324+
thrown.expect(RuntimeException.class);
325+
thrown.expectMessage("unexpected exception");
326+
streamListener.messageRead(method.streamRequest(1234L));
327+
}
328+
314329
private void checkStats(Status.Code statusCode) {
315330
CensusTestUtils.MetricsRecord record = censusCtxFactory.pollRecord();
316331
assertNotNull(record);

0 commit comments

Comments
 (0)