Skip to content

Commit

Permalink
Handle socket exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Feb 2, 2024
1 parent 6334b57 commit ad5c0b2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/NATS.Client.Core/Commands/CommandWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Linq.Expressions;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -323,12 +325,12 @@ private static async Task ReaderLoopAsync(ILogger<CommandWriter> logger, ISocket
var buffer = result.Buffer;
var consumed = buffer.Start;
var examined = buffer.GetPosition(examinedOffset);
var readBuffer = buffer.Slice(examinedOffset);

try
{
if (!buffer.IsEmpty)
if (!buffer.IsEmpty && !readBuffer.IsEmpty)
{
var readBuffer = buffer.Slice(examinedOffset);
var bufferLength = (int)readBuffer.Length;

var bytes = ArrayPool<byte>.Shared.Rent(bufferLength);
Expand All @@ -341,7 +343,15 @@ private static async Task ReaderLoopAsync(ILogger<CommandWriter> logger, ISocket
var totalSize = 0;
while (totalSent < bufferLength)
{
var sent = await connection.SendAsync(memory).ConfigureAwait(false);
int sent;
try
{
sent = await connection.SendAsync(memory).ConfigureAwait(false);
}
catch (SocketException)
{
break;
}

totalSent += sent;
memory = memory[sent..];
Expand Down

0 comments on commit ad5c0b2

Please sign in to comment.