Commit 45a0ec1
authored
Fix: Socket.SendFile throws when Blocking=false (#122831)
Hi, I noticed this issue was unassigned and marked "good first issue" so
I took a crack at it. Note this is a breaking change — happy to discuss
the approach or add breaking change documentation if needed.
---
### Problem
`Socket.SendFile` does not validate that the socket is in blocking mode
before attempting the operation. This leads to undefined,
platform-specific behavior:
- **Windows:** The operation blocks regardless of the `Blocking`
property setting
- **Linux:** May send only partial data with no indication of how much
was actually transmitted
### Solution
Added a validation check at the start of `SendFile` to throw
`InvalidOperationException` when `Blocking == false`. This follows the
same pattern used by other synchronous socket operations that require
blocking mode.
### Testing
- Added `NonBlocking_ThrowsInvalidOperationException` test covering both
`SendFile` overloads
- All 161 existing SendFile tests continue to pass
### Breaking Change
This is a breaking change. Code that previously called `SendFile` on a
non-blocking socket will now throw an exception:
- **On Linux:** This surfaces existing bugs — code was silently losing
data
- **On Windows:** Code that relied on the undocumented behavior of
Windows ignoring the `Blocking` flag will now fail
The previous behavior was undefined and platform-inconsistent, so
failing explicitly is preferable to silent misbehavior.
Fixes #472871 parent 6c54ede commit 45a0ec1
2 files changed
Lines changed: 19 additions & 0 deletions
File tree
- src/libraries/System.Net.Sockets
- src/System/Net/Sockets
- tests/FunctionalTests
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1325 | 1325 | | |
1326 | 1326 | | |
1327 | 1327 | | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
1328 | 1335 | | |
1329 | 1336 | | |
1330 | 1337 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
40 | 52 | | |
41 | 53 | | |
42 | 54 | | |
| |||
0 commit comments