Skip to content

Commit 89d4634

Browse files
committed
Add exception handling to ByteHandlers.
The exception should be captured by the returned ValueTask, not propagated synchronously.
1 parent debd2c1 commit 89d4634

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/MySqlConnector/Protocol/Serialization/SocketByteHandler.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,28 @@ public ValueTask<int> ReadBytesAsync(ArraySegment<byte> buffer, IOBehavior ioBeh
2828

2929
ValueTask<int> DoReadBytesSync(ArraySegment<byte> buffer_)
3030
{
31-
if (RemainingTimeout == Constants.InfiniteTimeout)
32-
return new ValueTask<int>(m_socket.Receive(buffer_.Array, buffer_.Offset, buffer_.Count, SocketFlags.None));
33-
34-
while (RemainingTimeout > 0)
31+
try
3532
{
36-
var startTime = Environment.TickCount;
37-
if (m_socket.Poll(Math.Min(int.MaxValue / 1000, RemainingTimeout) * 1000, SelectMode.SelectRead))
33+
if (RemainingTimeout == Constants.InfiniteTimeout)
34+
return new ValueTask<int>(m_socket.Receive(buffer_.Array, buffer_.Offset, buffer_.Count, SocketFlags.None));
35+
36+
while (RemainingTimeout > 0)
3837
{
39-
var bytesRead = m_socket.Receive(buffer_.Array, buffer_.Offset, buffer_.Count, SocketFlags.None);
38+
var startTime = Environment.TickCount;
39+
if (m_socket.Poll(Math.Min(int.MaxValue / 1000, RemainingTimeout) * 1000, SelectMode.SelectRead))
40+
{
41+
var bytesRead = m_socket.Receive(buffer_.Array, buffer_.Offset, buffer_.Count, SocketFlags.None);
42+
RemainingTimeout -= unchecked(Environment.TickCount - startTime);
43+
return new ValueTask<int>(bytesRead);
44+
}
4045
RemainingTimeout -= unchecked(Environment.TickCount - startTime);
41-
return new ValueTask<int>(bytesRead);
4246
}
43-
RemainingTimeout -= unchecked(Environment.TickCount - startTime);
47+
return ValueTaskExtensions.FromException<int>(MySqlException.CreateForTimeout());
48+
}
49+
catch (Exception ex)
50+
{
51+
return ValueTaskExtensions.FromException<int>(ex);
4452
}
45-
return ValueTaskExtensions.FromException<int>(MySqlException.CreateForTimeout());
4653
}
4754

4855
async Task<int> DoReadBytesAsync(ArraySegment<byte> buffer_)
@@ -79,13 +86,16 @@ async Task<int> DoReadBytesAsync(ArraySegment<byte> buffer_)
7986
public ValueTask<int> WriteBytesAsync(ArraySegment<byte> data, IOBehavior ioBehavior)
8087
{
8188
if (ioBehavior == IOBehavior.Asynchronous)
82-
{
8389
return new ValueTask<int>(DoWriteBytesAsync(data));
84-
}
85-
else
90+
91+
try
8692
{
8793
m_socket.Send(data.Array, data.Offset, data.Count, SocketFlags.None);
88-
return default(ValueTask<int>);
94+
return default;
95+
}
96+
catch (Exception ex)
97+
{
98+
return ValueTaskExtensions.FromException<int>(ex);
8999
}
90100

91101
async Task<int> DoWriteBytesAsync(ArraySegment<byte> data_)

src/MySqlConnector/Protocol/Serialization/StreamByteHandler.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ async Task<int> DoReadBytesAsync(ArraySegment<byte> buffer_)
8080
public ValueTask<int> WriteBytesAsync(ArraySegment<byte> data, IOBehavior ioBehavior)
8181
{
8282
if (ioBehavior == IOBehavior.Asynchronous)
83-
{
8483
return new ValueTask<int>(DoWriteBytesAsync(data));
85-
}
86-
else
84+
85+
try
8786
{
8887
m_stream.Write(data.Array, data.Offset, data.Count);
89-
return default(ValueTask<int>);
88+
return default;
89+
}
90+
catch (Exception ex)
91+
{
92+
return ValueTaskExtensions.FromException<int>(ex);
9093
}
9194

9295
async Task<int> DoWriteBytesAsync(ArraySegment<byte> data_)

0 commit comments

Comments
 (0)