Skip to content

Drop redundant fixed in HttpSys SetDataChunkWithPinnedData - #66578

Merged
BrennanConroy merged 3 commits into
dotnet:mainfrom
unsafePtr:feature/httpsys-drop-redundant-fixed
Oct 23, 2026
Merged

BrennanConroy merged 3 commits into
dotnet:mainfrom
unsafePtr:feature/httpsys-drop-redundant-fixed

Conversation

@unsafePtr

Copy link
Copy Markdown
Contributor

Description

Drops the fixed (byte* ptr = bytes) block from the two SetDataChunkWithPinnedData helpers in HttpSys. The pin was redundant: the implementation captures ptr into chunk.pBuffer for HTTP.SYS to read later, so the fixed scope ends before the value is consumed. Safety always relied on the caller keeping the data alive, not the fixed block — the helper's name reflects that contract.

All 5 call sites verified

Call site Data Why already at a stable address
ResponseBody.cs:196, :248, :252 Helpers.ChunkTerminator / CRLF "...\r\n..."u8 — embedded in assembly RVA section, never moved
ResponseBody.cs:275 span over handle.AddrOfPinnedObject() + offset GCHandle.Alloc(..., Pinned), freed by FreeDataBuffers
ResponseStreamAsyncResult.cs:70, :134 Helpers.CRLF same UTF-8 literal

UTF-8 literals ("..."u8) live in assembly metadata, not on the GC heap. The GCHandle-pinned site keeps the buffer alive for the lifetime of the chunk's use.

Behavior preservation

fixed (byte* ptr = bytes) returns null when bytes.IsEmpty, regardless of how the span was constructed. MemoryMarshal.GetReference(bytes) does not — per its documentation, for an empty span built from a non-null pointer (e.g. a zero-length ArraySegment of a GCHandle-pinned array) it returns a non-null ref. The bytes.IsEmpty ? null : ... guard preserves the original null-on-empty semantic.

Consistency with existing code

ResponseStreamAsyncResult.cs:80 already uses the equivalent pattern for an externally-pinned array — (void*)Marshal.UnsafeAddrOfPinnedArrayElement(...) with no fixed block. The two helpers were the outliers in this file. This change aligns them with that neighboring pattern.

No behavior change at runtime; pure cleanup.

@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label May 4, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 4, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @unsafePtr. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@unsafePtr

Copy link
Copy Markdown
Contributor Author

@BrennanConroy , @halter73 . Is the change useless because Kestrel is used mainly? Shall I close the PR?

@BrennanConroy

Copy link
Copy Markdown
Member

I wouldn't say useless, but it doesn't really improve much. Really, we should either pass in a pointer so it's clearer the caller should pin the bytes, or inline the method so it's easier to see the lifetime of the pinned bytes.

SetDataChunkWithPinnedData took a span whose pinning was an unwritten caller
contract. Keep one copy of each constant on the pinned object heap and build
the HTTP_DATA_CHUNK once, so the literal call sites just assign a ready-made
chunk. The remaining caller derives its pointer next to the GCHandle it
allocates, making the pin and its lifetime visible in one place.
@unsafePtr
unsafePtr requested a review from SamMonoRT as a code owner August 23, 2026 21:15
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 26, 2026
@BrennanConroy

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@dotnet-policy-service dotnet-policy-service Bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 22, 2026
@BrennanConroy
BrennanConroy enabled auto-merge (squash) October 22, 2026 22:22
@BrennanConroy

Copy link
Copy Markdown
Member

/ba-g unrelated

@BrennanConroy
BrennanConroy merged commit ceb0eec into dotnet:main Oct 23, 2026
28 of 30 checks passed
@BrennanConroy

Copy link
Copy Markdown
Member

Thanks @unsafePtr

@github-actions

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

Confident no docs needed: pure internal cleanup removing a redundant fixed block in HttpSys helpers.

Documentation surfaces considered

  • Conceptual article: Not required — Internal HttpSys refactor; no public API, config, or behavior change; no signals.
  • Migration guidance: Not required — No user action needed when upgrading.
  • Breaking change: Not required — PR states no runtime behavior change; no compatibility impact.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 970ea0fb Deployed Oct 23, 2026 by BrennanConroy via conclusion #98
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants