Skip to content

Commit

Permalink
skip send response if grpc channel is closed by client (#2420)
Browse files Browse the repository at this point in the history
* skip send response if channel closed

* update log

* update log

* update requestid

---------

Co-authored-by: Ankith Gunapal <[email protected]>
  • Loading branch information
lxning and agunapal committed Jun 24, 2023
1 parent 603e89f commit 9833774
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion frontend/server/src/main/java/org/pytorch/serve/job/GRPCJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.protobuf.ByteString;
import io.grpc.Status;
import io.grpc.stub.ServerCallStreamObserver;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -67,10 +68,16 @@ public void response(
int statusCode,
String statusPhrase,
Map<String, String> responseHeaders) {

ByteString output = ByteString.copyFrom(body);
if (this.getCmd() == WorkerCommands.PREDICT
|| this.getCmd() == WorkerCommands.STREAMPREDICT) {
if (((ServerCallStreamObserver<PredictionResponse>) predictionResponseObserver)
.isCancelled()) {
logger.warn(
"grpc client call already cancelled, not able to send this response for requestId: {}",
getPayload().getRequestId());
return;
}
PredictionResponse reply =
PredictionResponse.newBuilder().setPrediction(output).build();
predictionResponseObserver.onNext(reply);
Expand Down Expand Up @@ -118,6 +125,14 @@ public void sendError(int status, String error) {
Status responseStatus = GRPCUtils.getGRPCStatusCode(status);
if (this.getCmd() == WorkerCommands.PREDICT
|| this.getCmd() == WorkerCommands.STREAMPREDICT) {
if (((ServerCallStreamObserver<PredictionResponse>) predictionResponseObserver)
.isCancelled()) {
logger.warn(
"grpc client call already cancelled, not able to send error: {}, for requestId: {}",
error,
getPayload().getRequestId());
return;
}
predictionResponseObserver.onError(
responseStatus
.withDescription(error)
Expand Down

0 comments on commit 9833774

Please sign in to comment.