Skip to content

Commit 27ac3a6

Browse files
committed
Handle client disconnection correctly.
1 parent 5bb2d09 commit 27ac3a6

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

tests/MySqlConnector.Tests/FakeMySqlServerConnection.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ public async Task RunAsync(TcpClient client, CancellationToken token)
3333
var keepRunning = true;
3434
while (keepRunning)
3535
{
36-
var bytes = await ReadPayloadAsync(stream, token);
36+
byte[] bytes;
37+
try
38+
{
39+
bytes = await ReadPayloadAsync(stream, token);
40+
}
41+
catch (EndOfStreamException)
42+
{
43+
break;
44+
}
45+
3746
switch ((CommandKind) bytes[0])
3847
{
3948
case CommandKind.Quit:
@@ -118,8 +127,13 @@ private static async Task<byte[]> ReadPayloadAsync(Stream stream, CancellationTo
118127
private static async Task<byte[]> ReadBytesAsync(Stream stream, int count, CancellationToken token)
119128
{
120129
var bytes = new byte[count];
121-
for (var bytesRead = 0; bytesRead < count;)
122-
bytesRead += await stream.ReadAsync(bytes, bytesRead, count - bytesRead, token);
130+
for (var totalBytesRead = 0; totalBytesRead < count;)
131+
{
132+
var bytesRead = await stream.ReadAsync(bytes, totalBytesRead, count - totalBytesRead, token);
133+
if (bytesRead == 0)
134+
throw new EndOfStreamException();
135+
totalBytesRead += bytesRead;
136+
}
123137
return bytes;
124138
}
125139

0 commit comments

Comments
 (0)