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

Make stream client closers non-blocking #791

Merged
merged 1 commit into from
Dec 5, 2024
Merged

Make stream client closers non-blocking #791

merged 1 commit into from
Dec 5, 2024

Conversation

emcfarlane
Copy link
Contributor

@emcfarlane emcfarlane commented Oct 28, 2024

This updates the behavior of the streaming client methods BidiStreamForClient.CloseResponse and ServerStreamForClient.Close to be non-blocking, aligning it with the standard behavior of net/http's Request.Body closure.

Previously, the implementation used a graceful, blocking closure that fully read from the stream before closing. This allows for reuse of the underlying TCP connection. However, this behavior could lead to unexpected client hangs, as users may not anticipate blocking on close.

To address this, the closers no longer drain the stream. Documentation has been updated to clarify the behavior and provide users a workaround to keep the optimization by calling receive until the stream is drained. This avoids unexpected blocking behavior in client applications.

Fixes #789

This updates the behavior of the client streaming methods
`BidiStreamForClient.CloseResponse` and `ServerStreamForClient.Close` to
be non-blocking, aligning it with the standard behavior of `net/http`'s
`Request.Body` closure.

Previously, the implementation used a graceful, blocking closure that
fully read from the stream before closing. This allows for reuse of the
underlying TCP connection. However, this behavior could lead to
unexpected client hangs, as users may not anticipate blocking on close.

To address this, the closers no long drain the stream. Documentation has
been updated to clarify the behavior and provide users a workaround to
keep the optimization by Receiving messages until the stream is drain.
This avoids unexpected blocking behavior in client applications.

Signed-off-by: Edward McFarlane <[email protected]>
@emcfarlane emcfarlane marked this pull request as ready for review November 25, 2024 19:33
Copy link
Member

@jhump jhump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At one point, I understood the point of draining the response stream to not only enable connection re-use with HTTP 1.1 but also to allow the application to get the status and trailers.

However, after playing around with it, this has never really worked (just as you expected, @emcfarlane).

If you are using the gRPC protocol and your application calls stream.Receive after stream.CloseResponse, then the application can get to the trailers. But that call to Receive returns an error along the lines of "connection closed" (with, unexpectedly, an "invalid_argument" status code) even if trailers indicated an RPC error. This of course doesn't work with Connect or gRPC-web protocols since the discard operation is blindly draining the body and not looking at envelopes and thus not trying to extract the final status/trailers from the body.

So I can now confidently say that this change is sufficiently backwards-compatible to merge.

@emcfarlane emcfarlane merged commit d55ebd8 into main Dec 5, 2024
17 checks passed
@emcfarlane emcfarlane deleted the ed/closeRead branch December 5, 2024 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ServerStreamForClient.Close hangs forever
2 participants