Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d28535b

Browse filesBrowse files
committed
Fail call if more than responses received while only one is expected
1 parent 686dcff commit d28535b
Copy full SHA for d28535b

1 file changed

+13-2Lines changed: 13 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: stub/src/main/java/io/grpc/stub/ClientCalls.java
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ private static <ReqT, RespT> void asyncUnaryRequestCall(
167167
ReqT param,
168168
ClientCall.Listener<RespT> responseListener) {
169169
call.start(responseListener, new Metadata.Headers());
170-
call.request(1);
170+
// Initially ask for two responses from flow-control so that if a misbehaving server sends more
171+
// than one responses, we can catch it.
172+
call.request(2);
171173
try {
172174
call.sendPayload(param);
173175
call.halfClose();
@@ -182,7 +184,9 @@ private static <ReqT, RespT> StreamObserver<ReqT> asyncStreamingRequestCall(
182184
boolean streamingResponse) {
183185
call.start(new StreamObserverToCallListenerAdapter<RespT>(
184186
call, responseObserver, streamingResponse), new Metadata.Headers());
185-
call.request(1);
187+
// Initially ask for two responses from flow-control so that if a misbehaving server sends more
188+
// than one responses, we can catch it.
189+
call.request(2);
186190
return new CallToStreamObserverAdapter<ReqT>(call);
187191
}
188192

@@ -215,6 +219,7 @@ private static class StreamObserverToCallListenerAdapter<RespT>
215219
private final ClientCall<?, RespT> call;
216220
private final StreamObserver<RespT> observer;
217221
private final boolean streamingResponse;
222+
private boolean firstResponseReceived;
218223

219224
public StreamObserverToCallListenerAdapter(
220225
ClientCall<?, RespT> call, StreamObserver<RespT> observer, boolean streamingResponse) {
@@ -229,6 +234,12 @@ public void onHeaders(Metadata.Headers headers) {
229234

230235
@Override
231236
public void onPayload(RespT payload) {
237+
if (firstResponseReceived && !streamingResponse) {
238+
throw Status.INTERNAL
239+
.withDescription("More than one responses received for unary or client-streaming call")
240+
.asRuntimeException();
241+
}
242+
firstResponseReceived = true;
232243
observer.onValue(payload);
233244

234245
if (streamingResponse) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.