-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Changes from 1 commit
7d2d6aa
3c847a5
875be8d
b90a9a8
94e243c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,13 +264,20 @@ 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); | ||
if (errorCode != SocketError.Success) | ||
try | ||
{ | ||
var exception = new SocketException((int)errorCode); | ||
throw NetworkErrorHelper.MapSocketException(exception); | ||
int bytesRead = _streamSocket.Receive(buffer, SocketFlags.None, out SocketError errorCode); | ||
if (errorCode != SocketError.Success) | ||
{ | ||
var exception = new SocketException((int)errorCode); | ||
throw NetworkErrorHelper.MapSocketException(exception); | ||
} | ||
return bytesRead; | ||
} | ||
catch (Exception exception) when (!(exception is OutOfMemoryException)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These overloads do not throw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can refactor all overloads to use either the throwing or the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 GetCustomNetworkException(SR.Format(SR.net_io_writefailure, exception.Message), exception); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Presumably this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
} | ||
return bytesRead; | ||
} | ||
|
||
public override unsafe int ReadByte() | ||
|
@@ -348,11 +355,18 @@ public override void Write(ReadOnlySpan<byte> buffer) | |
ThrowIfDisposed(); | ||
if (!CanWrite) throw new InvalidOperationException(SR.net_readonlystream); | ||
|
||
_streamSocket.Send(buffer, SocketFlags.None, out SocketError errorCode); | ||
if (errorCode != SocketError.Success) | ||
try | ||
{ | ||
var exception = new SocketException((int)errorCode); | ||
throw NetworkErrorHelper.MapSocketException(exception); | ||
_streamSocket.Send(buffer, SocketFlags.None, out SocketError errorCode); | ||
if (errorCode != SocketError.Success) | ||
{ | ||
var exception = new SocketException((int)errorCode); | ||
throw NetworkErrorHelper.MapSocketException(exception); | ||
} | ||
} | ||
catch (Exception exception) when (!(exception is OutOfMemoryException)) | ||
{ | ||
throw GetCustomNetworkException(SR.Format(SR.net_io_writefailure, exception.Message), exception); | ||
} | ||
} | ||
|
||
|
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.
This combined with the catch below is going to result in two NetworkExceptions, one inside the other, isn't it?
Same issue below in ReadByte
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.
Yeah, good catch.