Skip to content

Do not return the DNS TCP buffer to the pool twice when growing it fails - #136585

Merged
EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:fix-dns-tcp-buffer-growth-double-return
Oct 28, 2026
Merged

EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:fix-dns-tcp-buffer-growth-double-return

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

When the length prefix of a DNS TCP response is larger than the rented buffer, SendTcpQueryAsync/SendTcpQuerySync returned the buffer to ArrayPool and then rented a larger one into the same variable. If that Rent threw (for example OutOfMemoryException), the variable still referred to the array that had just been returned, and the catch block returned it again. The pool could then hand the same array to two independent renters.

Rent the larger buffer first, then return the old one and switch to the new one, so the variable always refers to an array that is still owned and is returned exactly once.

When the length prefix of a DNS TCP response is larger than the rented buffer, SendTcpQueryAsync/SendTcpQuerySync returned the buffer to ArrayPool and then rented a larger one into the same variable. If that Rent threw (for example OutOfMemoryException), the variable still referred to the array that had just been returned, and the catch block returned it again. The pool could then hand the same array to two independent renters.

Rent the larger buffer first, then return the old one and switch to the new one, so the variable always refers to an array that is still owned and is returned exactly once.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: a13c23e0-b8e7-47bd-9714-cdc27528b527
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

The 2-byte length prefix bounds TCP responses to ushort.MaxValue, so renting
that size up front removes the grow-and-swap path entirely.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
@EgorBo
EgorBo marked this pull request as draft October 28, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants