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

gRPC bi-directional streaming: Close stream from client. #3230

Closed
ysong4 opened this issue Dec 5, 2019 · 5 comments
Closed

gRPC bi-directional streaming: Close stream from client. #3230

ysong4 opened this issue Dec 5, 2019 · 5 comments

Comments

@ysong4
Copy link

ysong4 commented Dec 5, 2019

I have a bi-directional streaming RPC. On the client side, I have a goroutine like this

func listenEvents(stream pb.XXXXX, ) {
    for {
        msg, err := stream.Recv()
        ...
    }
}

Now I want to close the stream, which receive data from server. But, when I invoke context cancel() and ClientConn.Close(), the stream.Recv is still blocking there.

Any suggestion on how to close the server stream on client side?

@ysong4
Copy link
Author

ysong4 commented Dec 5, 2019

#990

Well, I found out the solution... But I am confused. Why we need to create a new context from the old context to cancel the stream?

@ysong4 ysong4 closed this as completed Dec 5, 2019
@menghanl
Copy link
Contributor

menghanl commented Dec 5, 2019

Which cancel did you call if you didn't create a new context?

And ClientConn.Close() should close the stream, and unblock Recv with error.

@ysong4
Copy link
Author

ysong4 commented Dec 6, 2019

Thanks for the reply!

I double checked my code and now the cancel can work as expected. I do not need to generate a 'new context' from the 'old context'. I guess previously, I wrote some stupid bugs in my code... Sorry for the previous question...

And I tested that both cancel and ClientConn.Close() can unblock Recv.

Today I have another question...

I have created a context ctx and cancel using context.WithCancel(context.Background()) just after I setup the the client stub. And then whenever client wants to send requests, I will use that ctx as first parameter.

Can I use a shared context in a sequence of requests? Currently I am doing so and it works fine. But after reading some posts about context, it makes me feel that using a shared context is a bad idea. But I can't see any potential problem. Thanks!

@menghanl
Copy link
Contributor

menghanl commented Dec 6, 2019

Can I use a shared context in a sequence of requests?

In general I would say: don't.

It might work now, but that's mostly because you never do anything to those contexts.

If you share the context between two RPCs, you won't be able to cancel only one, because canceling the context will cancel both.
In a more gRPC specific case, context is also used to send metadata. Metadata for one RPC can be leaked to another if the context is shared.

@ysong4
Copy link
Author

ysong4 commented Dec 9, 2019

Got it! Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants