Skip to content

fix(gun): release a server stream the caller stops reading - #659

Open
Raed2180416 wants to merge 1 commit into
elixir-grpc:masterfrom
Raed2180416:fix/gun-release-abandoned-stream
Open

Raed2180416 wants to merge 1 commit into
elixir-grpc:masterfrom
Raed2180416:fix/gun-release-abandoned-stream

Conversation

@Raed2180416

@Raed2180416 Raed2180416 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

A server-streaming reply that the caller stops reading before it ends is never released: the stream stays open on the connection, and its response process keeps the rest of the reply queued for as long as the connection lives.

response_stream/4 in grpc/lib/grpc/client/adapters/gun.ex returns the reply as a Stream.unfold/2. unfold has no cleanup hook, so when the consumer halts early — Enum.take/2, Stream.take/2, a for with a break, or an exception in the caller's own code — nothing cancels the stream and nothing stops the StreamResponseProcess that buffers it. The connection process still holds the stream in response_processes, the process still holds the frames already delivered, and Gun has no reason to reset a stream nobody is reading. The only thing that eventually clears it is the deadline timer, and with :infinity (the default when no timeout is given) there is no timer at all.

Stopping early is the only way to abandon a reply: the enumeration returns a plain enumerable, not the GRPC.Client.Stream that GRPC.Stub.cancel/1 takes, so a caller who has read enough has no way to cancel it.

The reply is now a Stream.resource/3 whose after-function cancels the stream, so releasing it happens on halt and on an exception in the consumer as well as on normal completion. The after-function only cancels a stream that has not already seen its trailers or an error, so nothing changes for a reply that is read to the end.

Verified: MIX_ENV=test mix test test/grpc/adapters/gun_abandoned_stream_test.exs — before the change the connection process still tracks the response process of a stream the test took one message from (response_processes holds one entry), after the change it is empty. MIX_ENV=test mix test — 706 passed, 2 skipped. mix format --check-formatted — clean.

Copilot AI balanced review requested due to automatic review settings November 17, 2026 22:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Raed2180416
Raed2180416 force-pushed the fix/gun-release-abandoned-stream branch from da0c661 to bee8fd1 Compare November 18, 2026 09:06
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.

3 participants