Skip to content

Commit

Permalink
Merge pull request #569 from caioavidal/fix/socket-connection-error
Browse files Browse the repository at this point in the history
fix: adjustments to intermittent socket error on unexpected client closed
  • Loading branch information
caioavidal authored Nov 6, 2023
2 parents 3a5b9dd + c7e756c commit 5147f8a
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Connection : IConnection
private readonly ILogger _logger;

private readonly Socket _socket;
private readonly Stream _stream;
private readonly NetworkStream _stream;
private readonly object _writeLock;

public Connection(Socket socket, ILogger logger)
Expand Down Expand Up @@ -246,7 +246,7 @@ private bool CompleteRead(IAsyncResult ar)

while (totalBytesRead < size)
{
if (!_stream.CanRead)
if (!_stream.CanRead || !_stream.DataAvailable)
{
return false;
}
Expand All @@ -262,6 +262,11 @@ private bool CompleteRead(IAsyncResult ar)

return true;
}

catch (ObjectDisposedException)
{
// this exception is expected when the clientListener got disposed. In this case we don't want to spam the log.
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to complete stream read");
Expand Down

0 comments on commit 5147f8a

Please sign in to comment.