Skip to content

Commit 0758c5b

Browse files
committed
* Handle cancellation token edge case (immediately canceled) in connection and channel CloseAsync
1 parent 0731895 commit 0758c5b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

projects/RabbitMQ.Client/Impl/Channel.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ public Task CloseAsync(ushort replyCode, string replyText, bool abort,
208208
public async Task CloseAsync(ShutdownEventArgs args, bool abort,
209209
CancellationToken cancellationToken)
210210
{
211+
CancellationToken originalCancellationToken = cancellationToken;
212+
bool cancellationRequested = cancellationToken.IsCancellationRequested;
213+
if (IsOpen && cancellationRequested)
214+
{
215+
// Note: we really do need to try and close this channel!
216+
cancellationToken = CancellationToken.None;
217+
}
218+
211219
bool enqueued = false;
212220
var k = new ChannelCloseAsyncRpcContinuation(ContinuationTimeout, cancellationToken);
213221

@@ -259,6 +267,7 @@ await ConsumerDispatcher.WaitForShutdownAsync()
259267
MaybeDisposeContinuation(enqueued, k);
260268
_rpcSemaphore.Release();
261269
ChannelShutdownAsync -= k.OnConnectionShutdownAsync;
270+
originalCancellationToken.ThrowIfCancellationRequested();
262271
}
263272
}
264273

projects/RabbitMQ.Client/Impl/Connection.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ public Task CloseAsync(ushort reasonCode, string reasonText, TimeSpan timeout, b
320320
///</remarks>
321321
internal async Task CloseAsync(ShutdownEventArgs reason, bool abort, TimeSpan timeout, CancellationToken cancellationToken)
322322
{
323+
if (timeout < InternalConstants.DefaultConnectionAbortTimeout)
324+
{
325+
timeout = InternalConstants.DefaultConnectionAbortTimeout;
326+
}
327+
328+
if (IsOpen && cancellationToken.IsCancellationRequested)
329+
{
330+
// Note: we really do need to try and close this connection!
331+
cancellationToken = CancellationToken.None;
332+
}
333+
323334
using var timeoutCts = new CancellationTokenSource(timeout);
324335
using var cts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
325336

@@ -335,6 +346,7 @@ internal async Task CloseAsync(ShutdownEventArgs reason, bool abort, TimeSpan ti
335346
{
336347
await OnShutdownAsync(reason)
337348
.ConfigureAwait(false);
349+
338350
await _session0.SetSessionClosingAsync(false, cts.Token)
339351
.ConfigureAwait(false);
340352

@@ -393,7 +405,7 @@ await _session0.TransmitAsync(method, cts.Token)
393405

394406
try
395407
{
396-
await _mainLoopTask.WaitAsync(timeout, cts.Token)
408+
await _mainLoopTask.WaitAsync(cts.Token)
397409
.ConfigureAwait(false);
398410
}
399411
catch

0 commit comments

Comments
 (0)