Skip to content

Commit ff15e6e

Browse files
committed
Socket.SendFile throws InvalidOperationException when Blocking=false
The behavior of SendFile on non-blocking sockets is undefined (Windows blocks anyway, Linux may send partial data), so we now throw InvalidOperationException early.
1 parent ccd875b commit ff15e6e

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,13 @@ public void SendFile(string? fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpa
13251325

13261326
ThrowIfDisposed();
13271327

1328+
// SendFile is not supported on non-blocking sockets.
1329+
// ValidateBlockingMode() below checks for async mismatch; this checks explicit non-blocking.
1330+
if (!Blocking)
1331+
{
1332+
throw new InvalidOperationException(SR.net_sockets_blocking);
1333+
}
1334+
13281335
if (!IsConnectionOriented || !Connected)
13291336
{
13301337
throw new NotSupportedException(SR.net_notconnected);

src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public async Task NotConnected_ThrowsNotSupportedException()
3737
await Assert.ThrowsAsync<NotSupportedException>(() => SendFileAsync(s, null, null, null, TransmitFileOptions.UseDefaultWorkerThread));
3838
}
3939

40+
[Fact]
41+
public void SendFile_NonBlockingSocket_ThrowsInvalidOperationException()
42+
{
43+
(Socket client, Socket server) = SocketTestExtensions.CreateConnectedSocketPair();
44+
using (client)
45+
using (server)
46+
{
47+
client.Blocking = false;
48+
Assert.Throws<InvalidOperationException>(() => client.SendFile(null));
49+
Assert.Throws<InvalidOperationException>(() => client.SendFile(null, ReadOnlySpan<byte>.Empty, ReadOnlySpan<byte>.Empty, TransmitFileOptions.UseDefaultWorkerThread));
50+
}
51+
}
52+
4053
[Theory]
4154
[InlineData(false)]
4255
[InlineData(true)]

0 commit comments

Comments
 (0)