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

Drop messages on the client for closed streams/subscriptions #133

Conversation

cbrewster
Copy link
Member

Why

Each stream/subscription has a messages channel with a capacity of 128 messages. In our main receive loop, we push messages into the channel, blocking until the channel has room. This adds some backpressure, but becomes problematic if the stream is not making any progress. For example, the client could start a stream and then decide to cancel it and not read any of the messages. If the server sends >128 messages, it will fill up the stream's channel leading to a deadlock for the session.

This will be more correctly fixed when river v2 support is landed, as that adds support for proper cancellation. In the meantime, we can close the channel when we know we are not going to be reading from it anymore, and then drop any messages destiined for a closed channel.

rpc/upload are not affected because the server is only allowed to send 1 payload, and the channel has a buffer size of 1, so there will always be room.

Note

A deadlock can still occur if the client holds a reference to the AsyncGenerator but doesn't service it. This is likely a client bug if it happens, but we can probably add some timeouts to putting messages in the stream channel for defense in depth. I'll do this as a followup as I need to think a little bit more about how to properly handle that case. This PR as-is should be a quick win for our usage since we shouldn't be holding references to async generators that we aren't also actively servicing.

What changed

  • Close stream aiochannel in a finalizer in stream/subscription impls
  • Ignore ChannelClosed errors when adding message to stream
  • Fix tests to use correct method kinds, this was causing subscription/upload RPCs to not work correctly in tests. Luckily things are fine on the server codegen side.

Test plan

  • Added a test which caused a deadlock on the client before this change, but works properly after this change.

@cbrewster
Copy link
Member Author

cbrewster commented Jan 8, 2025

Current dependencies on/for this PR:

This comment was autogenerated by Freephite.

@cbrewster cbrewster requested a review from a team as a code owner January 8, 2025 20:04
@cbrewster cbrewster requested review from masad-frost, blast-hardcheese and jackyzha0 and removed request for a team January 8, 2025 20:04
@cbrewster cbrewster force-pushed the 01-08-Drop_messages_on_the_client_for_closed_streams/subscriptions branch from 71db42d to debcae1 Compare January 8, 2025 20:05
@cbrewster cbrewster added bug Something isn't working patch Bump patch version and removed bug Something isn't working labels Jan 8, 2025
@cbrewster cbrewster force-pushed the 01-08-Drop_messages_on_the_client_for_closed_streams/subscriptions branch 3 times, most recently from e64debf to b963ede Compare January 8, 2025 20:11
@cbrewster cbrewster enabled auto-merge (squash) January 8, 2025 20:11
@cbrewster cbrewster force-pushed the 01-08-Drop_messages_on_the_client_for_closed_streams/subscriptions branch from b963ede to 4537d33 Compare January 8, 2025 20:12
@cbrewster cbrewster merged commit ecd1b17 into main Jan 8, 2025
3 checks passed
@cbrewster cbrewster deleted the 01-08-Drop_messages_on_the_client_for_closed_streams/subscriptions branch January 8, 2025 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working patch Bump patch version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants