Fix potential channel corruption after cancelled ReceiveMessageOrClosed()#40663
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a Windows socket-channel edge case where cancelling ReceiveMessageOrClosed() after the reply bytes have already been read can leave the channel desynchronized/corrupted. It introduces channel-level tracking of already-received bytes so a subsequent receive can consume those bytes deterministically instead of re-reading from the socket stream.
Changes:
- Extend
ReadSocketMessageHandleto accept and persist “pending” already-received bytes across aborted receives, draining them before issuing a newWSARecv. - Add
SocketChannelstate (m_pendingBytes) to store pending bytes at the channel level and pass them intoReadSocketMessageHandle. - Add new unit-test scenarios validating delivery from
PendingBytes(complete message, partial header, partial body, invalid header size).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/windows/UnitTests.cpp | Adds test coverage for PendingBytes-based message assembly and validation. |
| src/windows/common/HandleIO.h | Updates ReadSocketMessageHandle API to accept a PendingBytes reference and adds ProcessChunk(). |
| src/windows/common/HandleIO.cpp | Implements pending-bytes draining, cancellation capture of already-buffered bytes, and chunk processing logic. |
| src/shared/inc/SocketChannel.h | Adds per-channel m_pendingBytes and wires it into the Windows receive path. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
benhillis
approved these changes
May 29, 2026
Member
benhillis
left a comment
There was a problem hiding this comment.
Looks good to me, thanks for adding the unit test.
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 of the Pull Request
This change fixes a potential channel corruption if a transaction is cancelled after its reply has been fully read.
This solves the issue by keeping track of the receives bytes at the channel level, and injecting them in the next receive. Stale messages will be discarded based on transaction ids.
This issue could manifest as the following error:
Which causes the session termination logic to hang for up to a minute, since it fails to signal processes
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed