Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/NATS.Client.Core/Commands/CommandWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal sealed class CommandWriter : IAsyncDisposable
// https://github.com/nats-io/nats.net/pull/383#discussion_r1484344102
private const int MinSegmentSize = 16384;

private static readonly TimeSpan ReaderLoopDisposeTimeout = TimeSpan.FromSeconds(5);
private readonly ILogger<CommandWriter> _logger;
private readonly bool _trace;
private readonly string _name;
Expand Down Expand Up @@ -158,13 +159,8 @@ public async ValueTask DisposeAsync()

#if NET8_0_OR_GREATER
await _cts.CancelAsync().ConfigureAwait(false);
if (_ctsReader != null)
{
await _ctsReader.CancelAsync().ConfigureAwait(false);
}
#else
_cts.Cancel();
_ctsReader?.Cancel();
#endif

_channelLock.Writer.TryComplete();
Expand All @@ -178,7 +174,25 @@ public async ValueTask DisposeAsync()
}

if (readerTask != null)
await readerTask.ConfigureAwait(false);
{
try
{
await readerTask.WaitAsync(ReaderLoopDisposeTimeout).ConfigureAwait(false);
}
catch (TimeoutException)
{
#if NET8_0_OR_GREATER
var ctsReader = _ctsReader;
if (ctsReader != null)
{
await ctsReader.CancelAsync().ConfigureAwait(false);
}
#else
_ctsReader?.Cancel();
#endif
await readerTask.WaitAsync(ReaderLoopDisposeTimeout).ConfigureAwait(false);
}
}
}

public ValueTask ConnectAsync(ClientOpts connectOpts, CancellationToken cancellationToken)
Expand Down
Loading