You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Setting TransferEncodingChunked reuses a shared mutable TransferCodingHeaderValue instance #138286
usingSystem.Net.Http.Headers;varheaders=newHttpRequestMessage().Headers;headers.TransferEncodingChunked=true;Console.WriteLine(headers);// Transfer-Encoding: chunked// Modify a different requestvarnewHeaders=newHttpRequestMessage().Headers;newHeaders.TransferEncodingChunked=true;newHeaders.TransferEncoding.First().Parameters.Add(newNameValueHeaderValue("abc","def"));// Original changedConsole.WriteLine(headers);// Transfer-Encoding: chunked; abc=def
Doesn't look like anyone's hit this in practice since parameters are never used, but it's not ideal we're caching an instance that's still mutable in theory.
If we decide to fix it, it should be done in a way that doesn't impact the number of allocations in practice.
We investigated the shared singleton pattern in HttpGeneralHeaders.TransferEncodingChunked (delegated from HttpRequestHeaders and HttpResponseHeaders) and observed how mutability on TransferCodingHeaderValue.Parameters causes cross-request contamination.
Root Cause Analysis
In HttpGeneralHeaders.cs, when TransferEncodingChunked = true is assigned:
The static instance HeaderUtilities.TransferEncodingChunked is added by reference directly to the request's TransferEncoding collection (HttpHeaderValueCollection<TransferCodingHeaderValue>).
Because TransferCodingHeaderValue.Parameters is a mutable ObjectCollection<NameValueHeaderValue> without a read-only lock:
If client code or middleware adds chunk extensions (request1.Headers.TransferEncoding.First().Parameters.Add(...)), it directly mutates HeaderUtilities.TransferEncodingChunked.Parameters in place.
Any subsequent request across any thread that sets TransferEncodingChunked = true inherits those foreign parameters.
Concurrent additions or iterations over Parameters during serialization can throw InvalidOperationException: Collection was modified.
Minimal Surgical Fix
Allocating a fresh, request-scoped instance when TransferEncodingChunked = true in HttpGeneralHeaders.cs ensures strict isolation between independent requests:
diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs
index 9e4f1a2..d8c32b5 100644
--- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs+++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpGeneralHeaders.cs@@ -82,7 +82,7 @@ public bool? TransferEncodingChunked
{
if (value == true)
{
- TransferEncoding.Add(HeaderUtilities.TransferEncodingChunked);+ TransferEncoding.Add(new TransferCodingHeaderValue(HeaderUtilities.Chunked));
}
}
else
Value Equality & Removal Semantics (value == false)
Setting TransferEncodingChunked = false or querying get remains completely compatible:
HttpHeaderValueCollection<T> queries Contains and Remove via TransferCodingHeaderValue.Equals.
Because TransferCodingHeaderValue.Equals performs structural value comparison (string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase) and parameter comparison) rather than reference equality (object.ReferenceEquals), removing chunked encoding continues to function seamlessly with isolated instances.
@cyberenigma-lgtm are you reviewing these posts manually? If it's just AI-generated without oversight, please stop
@MihaZupan Yes, I am reviewing each issue and the relevant source paths manually—not running an automated bot. I use an AI-assisted workflow alongside a static/lifecycle modeling tool I'm building to analyze memory and concurrency bugs, and I manually verify the root cause before posting.
That said, I completely understand the noise concern on dotnet/runtime. I'll hold off on posting further issue comments and will only submit a clean PR with passing unit tests if I have a verified fix ready for review.
Activity
dotnet-policy-service commentedon Nov 25, 2026
Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
cyberenigma-lgtm commentedon Nov 26, 2026
Thanks for opening this issue @MihaZupan.
We investigated the shared singleton pattern in
HttpGeneralHeaders.TransferEncodingChunked(delegated fromHttpRequestHeadersandHttpResponseHeaders) and observed how mutability onTransferCodingHeaderValue.Parameterscauses cross-request contamination.Root Cause Analysis
In
HttpGeneralHeaders.cs, whenTransferEncodingChunked = trueis assigned:The static instance
HeaderUtilities.TransferEncodingChunkedis added by reference directly to the request'sTransferEncodingcollection (HttpHeaderValueCollection<TransferCodingHeaderValue>).Because
TransferCodingHeaderValue.Parametersis a mutableObjectCollection<NameValueHeaderValue>without a read-only lock:request1.Headers.TransferEncoding.First().Parameters.Add(...)), it directly mutatesHeaderUtilities.TransferEncodingChunked.Parametersin place.TransferEncodingChunked = trueinherits those foreign parameters.Parametersduring serialization can throwInvalidOperationException: Collection was modified.Minimal Surgical Fix
Allocating a fresh, request-scoped instance when
TransferEncodingChunked = trueinHttpGeneralHeaders.csensures strict isolation between independent requests:Value Equality & Removal Semantics (
value == false)Setting
TransferEncodingChunked = falseor queryinggetremains completely compatible:HttpHeaderValueCollection<T>queriesContainsandRemoveviaTransferCodingHeaderValue.Equals.TransferCodingHeaderValue.Equalsperforms structural value comparison (string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase)and parameter comparison) rather than reference equality (object.ReferenceEquals), removing chunked encoding continues to function seamlessly with isolated instances.Regression Test (xUnit)
Stand-alone patch diff, formal concurrency audit, and test suite available here:
https://github.com/cyberenigma-lgtm/UNIVERSAL-POLYGLOT-LAYER-UPL/tree/main/Proyectos_en_Revision/13_dotnet_runtime_138286_http_transfer_encoding_chunked_shared_state
MihaZupan commentedon Nov 26, 2026
@cyberenigma-lgtm are you reviewing these posts manually? If it's just AI-generated without oversight, please stop
cyberenigma-lgtm commentedon Nov 26, 2026
@MihaZupan Yes, I am reviewing each issue and the relevant source paths manually—not running an automated bot. I use an AI-assisted workflow alongside a static/lifecycle modeling tool I'm building to analyze memory and concurrency bugs, and I manually verify the root cause before posting.
That said, I completely understand the noise concern on
dotnet/runtime. I'll hold off on posting further issue comments and will only submit a clean PR with passing unit tests if I have a verified fix ready for review.