Simplify ManagedWebSocket.EnsureBufferContainsAsync#127470
Open
cittaz wants to merge 1 commit intodotnet:mainfrom
Open
Simplify ManagedWebSocket.EnsureBufferContainsAsync#127470cittaz wants to merge 1 commit intodotnet:mainfrom
cittaz wants to merge 1 commit intodotnet:mainfrom
Conversation
The inner `if (_receiveBufferCount < minimumRequiredBytes)` was a fossil left over from when this method had a manual `while` loop calling `Stream.ReadAsync` until the buffer held enough data. After dotnet#69272 replaced that loop with a single `Stream.ReadAtLeastAsync` call (which loops internally), the inner predicate became unreachable in any branch where it could be false: nothing between the outer check and the inner check mutates `_receiveBufferCount`. The accompanying `// While we don't have enough data, read more.` comment also no longer matches the code. Remove the dead branch, drop the stale comment, and dedent the body. No behavior change.
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
This was referenced Apr 28, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cleans up
ManagedWebSocket.EnsureBufferContainsAsyncby removing a dead branch and a stale comment that have been left behind since #69272 replaced the manual read loop with a singleStream.ReadAtLeastAsynccall. No behavior change.Background
Before #69272 (May 2022), the method ended with a real loop:
That PR converted it to a single
ReadAtLeastAsynccall (which loops internally until enough bytes are read or EOF) and downgradedwhiletoif, but kept both the inner predicate and the original comment.What was wrong
CopyTo(which shifts data within the buffer) and_receiveBufferOffset = 0execute. Neither mutates_receiveBufferCount, so the inner predicate is provably the same as the outer one. The body always runs.// While we don't have enough data, read more.describes the originalwhileloop. The current code performs a singleReadAtLeastAsynccall (the loop is insideReadAtLeastAsyncitself), so "while" no longer matches.Change
Remove the inner
if, drop the misleading comment, and dedent the body. Behavior is unchanged.