Skip to content

fix: properly cancel token passed to AsyncEventingBasicConsumer receiver #1789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ protected override async Task ProcessChannelAsync()
work.DeliveryTag, work.BasicProperties!, work.Body.Size))
{
await work.Consumer.HandleBasicDeliverAsync(
work.ConsumerTag!, work.DeliveryTag, work.Redelivered,
work.Exchange!, work.RoutingKey!, work.BasicProperties!, work.Body.Memory)
work.ConsumerTag!, work.DeliveryTag, work.Redelivered,
work.Exchange!, work.RoutingKey!, work.BasicProperties!,
work.Body.Memory,
work.Consumer.Channel?.ChannelCancellationToken ?? default)
.ConfigureAwait(false);
}
break;
Expand Down
5 changes: 5 additions & 0 deletions projects/RabbitMQ.Client/IChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,5 +450,10 @@ Task QueueUnbindAsync(string queue, string exchange, string routingKey,
/// timing out.
/// </summary>
TimeSpan ContinuationTimeout { get; set; }

/// <summary>
/// The <see cref="CancellationToken"/> associated with channel closure.
/// </summary>
CancellationToken ChannelCancellationToken { get; }
}
}
1 change: 1 addition & 0 deletions projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public IEnumerable<string> ConsumerTags
public int ChannelNumber => InnerChannel.ChannelNumber;

public ShutdownEventArgs? CloseReason => InnerChannel.CloseReason;
public CancellationToken ChannelCancellationToken => InnerChannel.ChannelCancellationToken;

public IAsyncBasicConsumer? DefaultConsumer
{
Expand Down
16 changes: 8 additions & 8 deletions projects/RabbitMQ.Client/Impl/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ internal partial class Channel : IChannel, IRecoverable
private bool _disposed;
private int _isDisposing;

private CancellationTokenSource _closeAsyncCts = new CancellationTokenSource();

public Channel(ISession session, CreateChannelOptions createChannelOptions)
{
ContinuationTimeout = createChannelOptions.ContinuationTimeout;
Expand All @@ -85,6 +87,8 @@ public Channel(ISession session, CreateChannelOptions createChannelOptions)
Session = session;
}

public CancellationToken ChannelCancellationToken => _closeAsyncCts.Token;

internal TimeSpan HandshakeContinuationTimeout { get; set; } = TimeSpan.FromSeconds(10);

public TimeSpan ContinuationTimeout { get; set; }
Expand Down Expand Up @@ -208,13 +212,6 @@ public Task CloseAsync(ushort replyCode, string replyText, bool abort,
public async Task CloseAsync(ShutdownEventArgs args, bool abort,
CancellationToken cancellationToken)
{
CancellationToken argCancellationToken = cancellationToken;
if (IsOpen)
{
// Note: we really do need to try and close this channel!
cancellationToken = CancellationToken.None;
}

bool enqueued = false;
var k = new ChannelCloseAsyncRpcContinuation(ContinuationTimeout, cancellationToken);

Expand All @@ -236,6 +233,8 @@ await ModelSendAsync(in method, k.CancellationToken)

AssertResultIsTrue(await k);

_closeAsyncCts.Cancel();

await ConsumerDispatcher.WaitForShutdownAsync()
.ConfigureAwait(false);
}
Expand Down Expand Up @@ -265,7 +264,7 @@ await ConsumerDispatcher.WaitForShutdownAsync()
MaybeDisposeContinuation(enqueued, k);
_rpcSemaphore.Release();
ChannelShutdownAsync -= k.OnConnectionShutdownAsync;
argCancellationToken.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
}
}

Expand Down Expand Up @@ -591,6 +590,7 @@ protected virtual void Dispose(bool disposing)
{
_rpcSemaphore.Dispose();
_confirmSemaphore.Dispose();
_closeAsyncCts.Dispose();
}
catch
{
Expand Down
1 change: 1 addition & 0 deletions projects/RabbitMQ.Client/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RabbitMQ.Client.Exceptions.PublishReturnException.PublishReturnException(ulong p
RabbitMQ.Client.Exceptions.PublishReturnException.ReplyCode.get -> ushort
RabbitMQ.Client.Exceptions.PublishReturnException.ReplyText.get -> string!
RabbitMQ.Client.Exceptions.PublishReturnException.RoutingKey.get -> string!
RabbitMQ.Client.IChannel.ChannelCancellationToken.get -> System.Threading.CancellationToken
RabbitMQ.Client.RabbitMQTracingOptions
RabbitMQ.Client.RabbitMQTracingOptions.RabbitMQTracingOptions() -> void
RabbitMQ.Client.RabbitMQTracingOptions.UsePublisherAsParent.get -> bool
Expand Down
Loading