Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serverCallStreamObserver.isCancelled() cannot detect client disconnection for gRPC version higher than 1.41.0 #9087

Closed
billwenboli opened this issue Apr 16, 2022 · 2 comments
Assignees
Labels
Milestone

Comments

@billwenboli
Copy link

billwenboli commented Apr 16, 2022

I have recently discovered that serverCallStreamObserver.isCancelled() cannot detect client disconnection when gRPC version is higher than 1.41.0. We have several dependencies related to gRPC, namely grpc-netty-shaded, grpc-protobuf, grpc-stub and grpc-testing. I found that ONLY when grpc-netty-shaded is above 1.41.0, the disconnection cannot be detected. All other dependencies can be set to the latest release version, which is 1.45.1.

Here is a sample code snippet of what we have on the server side:

Greeting greeting = request.getGreeting();
String firstName = greeting.getFirstName();
String lastName = greeting.getLastName();

int i = 0;

ServerCallStreamObserver<GreetResponse> serverCallStreamObserver =
    (ServerCallStreamObserver<GreetResponse>)responseObserver;

try {
  while (true) {
    if (serverCallStreamObserver.isCancelled()) {
      System.out.println("cancelled");
      serverCallStreamObserver.onCompleted();
      break;
    }

    String result = "Hello " + firstName + " " + lastName + ", response number: " + i;
    GreetResponse response = GreetResponse.newBuilder()
        .setResult(result)
        .build();
    responseObserver.onNext(response);

    Thread.sleep(1000L);
    i++;
  }
} catch (InterruptedException e) {
  e.printStackTrace();
}

Our client is written in Python. Here is the sample code snippet:

def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = greet_pb2_grpc.GreetServiceStub(channel=channel)
        greeting = greet_pb2.Greeting()
        greeting.first_name = 'Bill'
        greeting.last_name = 'Li'
        for response in stub.GreetServerStreaming(greet_pb2.GreetRequest(greeting=greeting)):
            print(response.result)
        channel.close()
@ejona86
Copy link
Member

ejona86 commented Apr 18, 2022

#8408 was in v1.41.0, and the observed behavior here does seem to match the current implementation. So I think something isn't right in the code. A workaround is to swap to Context.current().isCancelled().

I saw similar behavior, but seemingly not introduced in v1.41.0, and the sleep duration mattered, so we need to fitz with my "reproduction" in https://groups.google.com/g/grpc-io/c/Zowxz7JR3LI/m/163eWqTpCAAJ more to see what's going on.

CC @temawi

@ejona86 ejona86 added bug and removed question labels Apr 18, 2022
@ejona86 ejona86 added this to the Next milestone May 3, 2022
@temawi
Copy link
Contributor

temawi commented Aug 31, 2022

PR #9501 should assure that ServerCallStreamObserver.isCancelled() always returns true when the call has been cancelled.

@temawi temawi closed this as completed Aug 31, 2022
@temawi temawi modified the milestones: Next, 1.50 Aug 31, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants