Avoid retaining returned PipeReader channels - #1528
Merged
Merged
Conversation
Track whether deserialization is handling a request so response channels are not retained in the inbound request cleanup map. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a potential unbounded memory growth scenario when RPC methods return PipeReader values by preventing response-deserialized channels from being tracked as inbound request resources, which previously kept completed channels strongly referenced on the client side.
Changes:
- Track whether the formatter is deserializing a request vs. a response (internal formatter state).
- Update duplex pipe tracking to associate inbound channels with the cleanup map only during request deserialization.
- Add a regression test ensuring a returned
PipeReadercan be drained, completed, and then garbage-collected (across all formatter-specific derived test classes).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/StreamJsonRpc.Tests/DuplexPipeMarshalingTests.cs | Adds a regression test and a server RPC method returning a PipeReader to validate collection after completion. |
| src/StreamJsonRpc/Reflection/MessageFormatterDuplexPipeTracker.cs | Avoids recording channels into inboundRequestChannelMap when deserializing responses. |
| src/StreamJsonRpc/Reflection/IJsonRpcFormatterState.cs | Introduces an internal state interface to distinguish request vs. response deserialization. |
| src/StreamJsonRpc/FormatterBase.cs | Implements the internal state (DeserializingRequest) and wires it into deserialization tracking. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Ryan Toth (RyanToth3)
approved these changes
Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Returned
PipeReadervalues were recorded in the inbound request channel map while deserializing RPC responses. SinceResponseSentis not raised on the receiving client, completed readers remained strongly referenced and could cause unbounded memory growth.Track whether the formatter is deserializing a request or a response, and only associate channels with the inbound request cleanup map for actual requests. Add regression coverage that drains and completes a returned reader, then verifies it can be collected across all supported formatters.
Fixes #1146