Skip to content
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

Fix: NetworkStream throwing inconsistent exceptions #40772

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,17 @@ public override int Read(Span<byte> buffer)
ThrowIfDisposed();
if (!CanRead) throw new InvalidOperationException(SR.net_writeonlystream);

int bytesRead = _streamSocket.Receive(buffer, SocketFlags.None, out SocketError errorCode);
int bytesRead;
SocketError errorCode;
try
{
bytesRead = _streamSocket.Receive(buffer, SocketFlags.None, out errorCode);
}
catch (Exception exception) when (!(exception is OutOfMemoryException))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is consistency with the array-based overload, why do the catch blocks differ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These overloads do not throw SocketException, but return SocketError as out parameter instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can refactor all overloads to use either the throwing or the out variant if we really care, but I would do it in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it would be better to be consistent here, and use the throwing versions consistently. That said, I'm fine not doing this in this PR.

{
throw GetCustomException(SR.Format(SR.net_io_readfailure, exception.Message), exception);
}

if (errorCode != SocketError.Success)
{
var socketException = new SocketException((int)errorCode);
Expand Down Expand Up @@ -320,7 +330,16 @@ public override void Write(ReadOnlySpan<byte> buffer)
ThrowIfDisposed();
if (!CanWrite) throw new InvalidOperationException(SR.net_readonlystream);

_streamSocket.Send(buffer, SocketFlags.None, out SocketError errorCode);
SocketError errorCode;
try
{
_streamSocket.Send(buffer, SocketFlags.None, out errorCode);
}
catch (Exception exception) when (!(exception is OutOfMemoryException))
{
throw GetCustomException(SR.Format(SR.net_io_writefailure, exception.Message), exception);
}

if (errorCode != SocketError.Success)
{
var socketException = new SocketException((int)errorCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ await RunWithConnectedNetworkStreamsAsync((server, _) =>
});
}

[Fact]
public async Task DisposeSocketDirectly_ReadWriteThrowNetworkException()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task DisposeSocketDirectly_ReadWriteThrowNetworkException(bool derivedNetworkStream)
{
using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
Expand All @@ -318,20 +320,22 @@ public async Task DisposeSocketDirectly_ReadWriteThrowNetworkException()

Task<Socket> acceptTask = listener.AcceptAsync();
await Task.WhenAll(acceptTask, client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndPoint).Port)));
using (Socket serverSocket = await acceptTask)
using (DerivedNetworkStream server = new DerivedNetworkStream(serverSocket))
{
serverSocket.Dispose();
using Socket serverSocket = await acceptTask;
using NetworkStream server = derivedNetworkStream ? (NetworkStream)new DerivedNetworkStream(serverSocket) : new NetworkStream(serverSocket);

serverSocket.Dispose();

Assert.Throws<IOException>(() => server.Read(new byte[1], 0, 1));
Assert.Throws<IOException>(() => server.Write(new byte[1], 0, 1));
Assert.Throws<IOException>(() => server.Read(new byte[1], 0, 1));
Assert.Throws<IOException>(() => server.Write(new byte[1], 0, 1));

Assert.Throws<IOException>(() => server.BeginRead(new byte[1], 0, 1, null, null));
Assert.Throws<IOException>(() => server.BeginWrite(new byte[1], 0, 1, null, null));
Assert.Throws<IOException>(() => server.Read((Span<byte>)new byte[1]));
Assert.Throws<IOException>(() => server.Write((ReadOnlySpan<byte>)new byte[1]));

Assert.Throws<IOException>(() => { server.ReadAsync(new byte[1], 0, 1); });
Assert.Throws<IOException>(() => { server.WriteAsync(new byte[1], 0, 1); });
}
Assert.Throws<IOException>(() => server.BeginRead(new byte[1], 0, 1, null, null));
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved
Assert.Throws<IOException>(() => server.BeginWrite(new byte[1], 0, 1, null, null));

Assert.Throws<IOException>(() => { server.ReadAsync(new byte[1], 0, 1); });
Assert.Throws<IOException>(() => { server.WriteAsync(new byte[1], 0, 1); });
}
}

Expand Down