question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BlockingStub returns iterator that cannot be closed

See original GitHub issue

I use a blocking sub for a method that returns a stream of messages. The return type of it is Iterator<T>. It turns out I cannot make grpc client to end the request (e.g. because of client failure). There are a number of cases where an open connection may stuck in client because of that

The implementation uses io.grpc.stub.ClientCalls#blockingServerStreamingCall(io.grpc.Channel, io.grpc.MethodDescriptor<ReqT,RespT>, io.grpc.CallOptions, ReqT)

call, which in turn uses BlockingResponseStream. There is no way to reach the io.grpc.ClientCall#cancel method form the Iterator. Could be nice to wrap an Iterator or make it implement an interface in the similar way as done with ServerStreamObserver/ClientStreamObserver The other approach could be to implement AutoCloseable from Java with this iterator

There is also a comment on the Iterator implementation class

  // TODO(ejona86): determine how to allow ClientCall.cancel() in case of application error.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mikehollercommented, Oct 25, 2019

Does anyone have any examples for the use of CancellableContext with a blocking stub? I’m having a hard time finding a good official source after an hour of searching.

0reactions
ejona86commented, Oct 10, 2019

@pskiwi, no, that wouldn’t work. After the cancellation you need to “drain” the Iterator; keep going until you get hasNext() == false. If you have a while (iter.hasNext()) iter.next(); after the finally, then it would work.

Draining the ThreadlessExecutor from a Context.CancellationListener scares me because of the threading involved.

It would be possible to add a close() method to the Iterator (and require you to cast), but in general we’re not wild about the blocking iterator API as it is far too hard to use properly. We think the proper fix here is to have a “real” blocking API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blocking stub should not return Iterator but Iterable/Collection ...
The stream can't be iterated more than once, so Iterator is the correct representation. It cannot implement Collection because the actual number of...
Read more >
GRPC Async + Blocking Stub Java - Stack Overflow
I originally intended to return this information from the server (bidirectionally), however stream observers do not lend themselves to set ...
Read more >
Basics tutorial | Java - gRPC
Accepts a stream of Points on a route being traversed, returning a ... Iterator<Feature> features; try { features = blockingStub.
Read more >
Closing Iterables is a Leaky Abstraction
If the value of done is true , you may or may not be given a value property. ... When invoked, [Symbol.iterator]() returns...
Read more >
gRPC - Quick Guide - Tutorialspoint
service Greeter { rpc greet(ClientInput) returns (ServerOutput) {} } ... Next, we create a blocking stub using the channel we created.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found