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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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?.ShutdownCts.Token ?? default)
.ConfigureAwait(false);
}
break;
Expand Down
2 changes: 2 additions & 0 deletions projects/RabbitMQ.Client/IChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
/// or the cause of its closure otherwise.
/// </summary>
ShutdownEventArgs? CloseReason { get; }

CancellationTokenSource ShutdownCts { get; }

Check warning on line 61 in projects/RabbitMQ.Client/IChannel.cs

View workflow job for this annotation

GitHub Actions / oauth2-uaa

Check warning on line 61 in projects/RabbitMQ.Client/IChannel.cs

View workflow job for this annotation

GitHub Actions / oauth2-keycloak


/// <summary>Signalled when an unexpected message is delivered.</summary>
///
Expand Down
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 CancellationTokenSource ShutdownCts => InnerChannel.ShutdownCts;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think IChannel should expose the token at max, not the writable source.

But there may be cleaner ways to pass this value. The calling function doesn't have cancellation either, and that is the usual way to pass something like that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look. Yes, I plan on modifying this PR when I get time to work on it.


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

private CancellationTokenSource _shutdownCts = new CancellationTokenSource();
public CancellationTokenSource ShutdownCts => _shutdownCts;

public Channel(ISession session, CreateChannelOptions createChannelOptions)
{
ContinuationTimeout = createChannelOptions.ContinuationTimeout;
Expand Down Expand Up @@ -208,6 +211,8 @@ public Task CloseAsync(ushort replyCode, string replyText, bool abort,
public async Task CloseAsync(ShutdownEventArgs args, bool abort,
CancellationToken cancellationToken)
{
_shutdownCts.Cancel();

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

Expand Down
Loading