diff --git a/src/NATS.Client.Core/Commands/CommandWriter.cs b/src/NATS.Client.Core/Commands/CommandWriter.cs index 79e64429e..e211435e9 100644 --- a/src/NATS.Client.Core/Commands/CommandWriter.cs +++ b/src/NATS.Client.Core/Commands/CommandWriter.cs @@ -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; @@ -323,12 +325,12 @@ private static async Task ReaderLoopAsync(ILogger 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.Shared.Rent(bufferLength); @@ -341,7 +343,15 @@ private static async Task ReaderLoopAsync(ILogger 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..];