-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix IndexOutOfRangeException when sending >2GB files on Windows #122917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add dictionary mapping file paths to handle indices to properly handle partitioned file elements. When PR #120634 partitions a >2GB file into multiple SendPacketsElement entries, the code now correctly maps each element back to its original file handle using the file path, instead of incrementing fileIndex which caused array out of bounds. Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
Use a parallel array to track which file handle each partitioned element uses, addressing code review feedback about duplicate file paths. This approach correctly handles cases where the same file path might appear multiple times in the original SendPacketsElement array. Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
|
@copilot, I don't see any tests being changed or added in this PR. Presumably one is needed? Or are there existing outerloop tests that catch this and that are failing? |
The existing outerloop test |
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs
Show resolved
Hide resolved
Add assertion to catch programming errors where a file element would incorrectly have -1 as its handle index. This should never happen since -1 is only stored for non-file elements (buffers) in the parallel array. Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes an IndexOutOfRangeException that occurs when sending files >2GB on Windows via Socket.SendFile or Socket.SendFileAsync. The issue was introduced by PR #120634, which implemented file partitioning for >2GB files but incorrectly indexed file handles when multiple elements were created from a single partitioned file.
Key Changes:
- Introduced parallel tracking array to map each expanded element to its corresponding file handle
- Modified handle lookup logic to use parallel array when partitioning occurs, with fallback to original counter-based logic
- Added proper cleanup for the new tracking array
Description
PR #120634 introduced file partitioning for >2GB files by expanding one
SendPacketsElementinto multiple chunks. However,SetupPinHandlesSendPacketsused an incrementing counter to index_sendPacketsFileHandles, causingIndexOutOfRangeExceptionsince the array contains one handle per original file but multiple elements per partitioned file.Fix: Track file handle indices via parallel array
_sendPacketsElementsFileHandleIndicesbuilt during partitioning. Each expanded element records which original file handle it uses. Falls back to original counter logic when no partitioning occurs. IncludesDebug.Assertto validate handle indices are valid (not -1) for file elements.Example scenario:
[0, 0, 1]maps elements to handles correctlyCustomer Impact
Test failure on Windows when attempting to send files >2GB via
Socket.SendFileorSocket.SendFileAsync. Feature added in PR #120634 is broken.Regression
Yes. Introduced in PR #120634 merged December 1, 2025.
Testing
GreaterThan2GBFile_SendsAllBytesvalidates the fixRisk
Low. Minimal change confined to Windows-specific partitioning path. No partitioning (files ≤2GB) uses original code path. Parallel array approach handles edge cases: duplicate file paths, mixed element types, multiple partitions per file. Debug assertion provides additional safety without affecting release builds.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.