Skip to content

dotnet: release oversized JSON-RPC receive buffers#2047

Open
adirh3 wants to merge 2 commits into
github:mainfrom
adirh3:fix/dotnet-jsonrpc-buffer-retention
Open

dotnet: release oversized JSON-RPC receive buffers#2047
adirh3 wants to merge 2 commits into
github:mainfrom
adirh3:fix/dotnet-jsonrpc-buffer-retention

Conversation

@adirh3

@adirh3 adirh3 commented Jul 21, 2026

Copy link
Copy Markdown

Problem

The .NET JSON-RPC read loop reuses the largest receive buffer for the lifetime of the connection. A single multi-megabyte frame therefore remains rooted while the loop waits for the next message.

Fix

After a completed frame is parsed, replace receive buffers larger than 1 MiB with the smallest buffer needed to preserve any bytes already carried from the next frame. The connection and all sessions remain active.

Validation

  • New regression test observed a 2,097,206-byte next-read buffer before the fix and passes with the bounded buffer afterward.
  • 163 .NET unit tests pass on net8.0.
  • The regression test passes on net472.
  • A real 16 MiB tool-result scenario completed, the same client connection remained healthy, and a post-GC process dump contained no managed byte arrays larger than 10 MB.

Bound the reusable receive buffer after a completed large frame while preserving any carried bytes for the next frame. Add a regression test that verifies the next read does not retain a multi-megabyte buffer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 21, 2026 20:25
@adirh3
adirh3 requested a review from a team as a code owner July 21, 2026 20:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Bounds retained .NET JSON-RPC receive buffers after processing oversized frames.

Changes:

  • Adds a 1 MiB retention threshold while preserving carried bytes.
  • Adds a regression test for oversized frames.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
dotnet/src/JsonRpc.cs Shrinks oversized receive buffers after parsing.
dotnet/test/Unit/JsonRpcTests.cs Adds oversized-buffer regression coverage.

Comment thread dotnet/test/Unit/JsonRpcTests.cs Outdated
[Fact]
public async Task JsonRpc_Does_Not_Retain_Oversized_Receive_Buffer()
{
using var receiveStream = new FrameThenEofStream(CreateResponseFrame(2 * 1024 * 1024));

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in ec5ff68. The regression now keeps the stream open, forces a second valid response to be coalesced into the same read as the oversized frame, verifies that carried response is processed, and then asserts the subsequent read buffer is bounded. The focused test passes on net8.0 and net472; all 163 .NET SDK unit tests pass on net8.0.

Keep the receive stream open, coalesce a second valid response with the oversized frame, and verify both carried-message processing and the subsequent bounded read buffer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 22, 2026 05:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

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.

2 participants