-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Is ServerStream.Context() guaranteed to expire when handler returns? #7860
Comments
@starius i am assuming you are talking about your service handler? If yes, you likely don't need to wrap
If you need to cancel the long-running function independently of the gRPC stream's lifecycle (e.g., based on some internal server condition), then context.WithCancel would be appropriate. The only thing you need to make sure your long-running function respects context cancellation. Check Let me know if you have anymore questions |
Closing this issue. Feel free to re-open if anymore questions. |
Hey @purnesh42H, Could you clarify what happens if my RPC handler on the server side returns nil? To be clear, I'm referring to a scenario where it's not an error but simply the normal end of a stream. This case doesn't seem to be addressed by any of the situations in the list you shared. |
@starius in both cases when the client cancels the rpc or rpc finish successfully, the provided context is canceled. Hence, you don't have to close it manually. However, as i mentioned in case you are spawning resources that needs to be cleaned up when context get's canceled, you need watch for context.Done() and do the cleanup. For example, in case of deadlines. You can read more about deadlines on the server and deadline propagation here https://grpc.io/docs/guides/deadlines/#deadlines-on-the-server https://grpc.io/docs/guides/cancellation/#cancelling-an-rpc-call-on-the-client-side might be a good read to understand what happens when client cancels an RPC. |
Thanks, @purnesh42H ! I got an answer to my question. |
I’m trying to determine whether it’s necessary to wrap
ServerStream.Context()
withcontext.WithCancel
in my handler or if it can be avoided. The context is passed to a long-running function that needs to be canceled as soon as the handler returns to release resources.I assume that
ServerStream.Context()
will automatically expire when the handler returns, so it should be safe to pass it directly to the long-running function. However, I couldn’t find any confirmation of this behavior in the documentation or source code.Could someone clarify whether
ServerStream.Context()
expires when the handler returns? If so, could you please point me to the relevant documentation or source code reference?The text was updated successfully, but these errors were encountered: