Skip to content

Handle zero size hints in Http1OutputProducer - #69005

Open
DeagleGross wants to merge 2 commits into
dotnet:mainfrom
DeagleGross:deaglegross-fix-http1-zero-size-hint
Open

Handle zero size hints in Http1OutputProducer#69005
DeagleGross wants to merge 2 commits into
dotnet:mainfrom
DeagleGross:deaglegross-fix-http1-zero-size-hint

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

IBufferWriter<byte>.GetMemory(0) and GetSpan(0) must return non-empty buffers, but Http1OutputProducer passed zero directly to MemoryPool<byte>.Rent(). MemoryPool<byte>.Rent(0) only guarantees memory with at least zero elements, and MemoryPool<byte>.Shared returns empty memory for that request. This conflicts with the stronger IBufferWriter<byte> contract, which requires GetMemory(0) and GetSpan(0) to return non-empty buffers.

This change normalizes zero size hints to Kestrel's minimum segment size before renting memory in the pre-start buffering and completed-output scratch-memory paths. Using the existing minimum segment size keeps allocation behavior consistent with Kestrel's pipe configuration and avoids inefficient tiny buffers.

Fixes #58644

@DeagleGross DeagleGross self-assigned this Sep 2, 2026
Copilot AI lite review requested due to automatic review settings September 2, 2026 17:34
@DeagleGross DeagleGross added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Sep 2, 2026
@DeagleGross
DeagleGross deployed to copilot-pat-pool September 2, 2026 17:34 — with GitHub Actions Active
@DeagleGross
DeagleGross deployed to copilot-pat-pool September 2, 2026 17:35 — with GitHub Actions Active

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.

Copilot review overview

🟡 Changes recommended

The new test disposes the producer inside a using var scope, causing a double-dispose that should be avoided (use Stop() or otherwise ensure a single dispose).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 Medium severity

New issues introduced by this change (1)
Severity Finding
Medium severity src/​Servers/​Kestrel/​Core/​test/​Http1/​Http1OutputProducerTests.csoutput is declared with using var, but the test also calls Dispose() explicitly. This results…
What changed in this PR

This PR fixes Http1OutputProducer’s handling of sizeHint: 0 by ensuring GetMemory(0)/GetSpan(0) never indirectly rent empty buffers from MemoryPool<byte>, aligning behavior with the IBufferWriter<byte> contract and Kestrel’s segment sizing.

Changes:

  • Normalize sizeHint == 0 to Kestrel’s minimum segment size before renting in pre-start buffering (AddSegment) and completed-output scratch memory (GetFakeMemory).
  • Add a regression test ensuring GetMemory(0)/GetSpan(0) return non-empty buffers for both pre-start and completed output paths.
  • Extend the test helper to allow injecting a custom MemoryPool<byte>.
File Description
src/​Servers/​Kestrel/​Core/​src/​Internal/​Http/​Http1OutputProducer.cs Normalizes zero size hints to a minimum segment size before renting buffers.
src/​Servers/​Kestrel/​Core/​test/​Http1/​Http1OutputProducerTests.cs Adds a regression test and enables injecting a custom MemoryPool<byte> into the producer.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Servers/Kestrel/Core/test/Http1/Http1OutputProducerTests.cs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-kestrel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Http1OutputProducer does not handle GetSpan/GetMemory(sizeHint: 0) correctly

2 participants