-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Remove Socket.SendFile and SendFileAsync 2GB limitation on Windows by partitioning large files #120634
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
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
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 removes the 2GB file size limitation for Socket.SendFile on Windows by implementing automatic file partitioning. The Windows TransmitFile API is limited to int.MaxValue bytes, but the solution partitions large files into multiple chunks while maintaining proper handling of pre/post buffers and flags.
- Implemented partitioning logic to handle files larger than 2GB by splitting them into manageable chunks
- Enhanced the TransmitFileHelper method to accept explicit byte counts for precise chunk transmission
- Enabled the previously skipped test case for files greater than 2GB on Windows
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs | Removed ActiveIssue attribute to enable >2GB file test on Windows |
| src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs | Added file partitioning logic and updated TransmitFileHelper signature |
| src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj | Added reference to SetFilePointerEx interop for file positioning |
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
…operation Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
/azp run runtime-libraries-coreclr outerloop |
|
@copilot, your fix only addressed the synchronous SendFile, not the asynchronous SendFileAsync. Please apply a similar partitioning fix for SendFileAsync. |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Fixed - SendFileAsync now partitions files larger than 2GB on Windows by creating multiple SendPacketsElement entries with appropriate offsets and chunk sizes. Each packet handles up to int.MaxValue bytes. Fixed in commit 8f9c3a2. |
src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs
Outdated
Show resolved
Hide resolved
…dles are opened Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
/azp run runtime-libraries-coreclr outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run runtime-libraries-coreclr outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run runtime-libraries-coreclr outerloop-windows |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/ba-g Test failures are unrelated |
Summary: Remove Socket.SendFile and SendFileAsync 2GB limitation on Windows
Problem
On Windows, the
Socket.SendFileandSocket.SendFileAsyncAPIs were limited to sending files up toint.MaxValue(2GB) due to the Win32TransmitFileandTransmitPacketsfunctions'int numberOfBytesToWriteparameter. This limitation did not exist on Linux/macOS. The issue has been tracked as #42534.Solution
Implemented automatic partitioning of large file sends into multiple
TransmitFilecalls (for synchronous SendFile) and multipleSendPacketsElemententries (for asynchronous SendFileAsync) when the file exceeds 2GB, as suggested by the TransmitFile Win32 API documentation. The implementation is optimized to minimize syscalls by combining pre/post buffers with file chunks.Implementation Details
Modified:
SocketPal.Windows.cs::SendFile(Synchronous)int.MaxValueusingRandomAccess.GetLengthint.MaxValue-sized chunks:TransmitFilecallModified:
SocketAsyncEventArgs.Windows.cs::DoOperationSendPackets(Asynchronous)int.MaxValuebytes usingRandomAccess.GetLengthSendPacketsElement[]array to accommodate partitioningSendPacketsElemententries for each large file, each with offset and countint.MaxValuebytesSendPacketsElementModified:
Socket.Tasks.cs::SendFileAsyncDoOperationSendPacketsafter files are openedModified:
TransmitFileHelpernumberOfBytesToWriteparameter (defaults to 0 for "send entire file")Modified:
System.Net.Sockets.csprojInterop.SetFilePointerEx.csto project referencesModified:
SendFile.cstests[ActiveIssue]attribute fromGreaterThan2GBFile_SendsAllBytestestPerformance Optimizations
Race Condition Prevention
DoOperationSendPacketsTesting
Changes
SocketPal.Windows.csto partition large file sends (synchronous)SocketAsyncEventArgs.Windows.csto partition large file sends (asynchronous)TransmitFileHelperto accept numberOfBytesToWrite parameterFixes #42534
Original prompt
Fixes #42534
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.