question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retry filter should cancel when the bus is stopped

See original GitHub issue

When a retry policy is waiting for a retry delay to elapsed, or if the filter is checking whether or not a retry can be performed, it should obey the cancellationToken of the endpoint being stopped so that an subsequent retries will be avoided and the message should remain on the queue.

It should not be moved to the error or skipped queues.

Original Issue

In the following example, pressing a key causes serviceBus.Stop() to hang for a while (until all retries are exhausted?) and then prints:

MassTransit.Util.TaskSupervisor Error: 0 : Failed to close scope MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer - rabbitmq://localhost/RetryBreakRepro/Test_Send, System.OperationCanceledException: The operation was canceled.

public class MyConsumer : IConsumer<IMy> {
	public Task Consume(ConsumeContext<IMy> context) {
		throw new NotImplementedException();
	}
}

...

	var serviceBus = Bus.Factory.CreateUsingRabbitMq(config => {
		var host = config.Host(new Uri("rabbitmq://localhost/RetryBreakRepro"), h => {
			h.Username("guest");
			h.Password("guest");
		});

		config.ReceiveEndpoint(host, "Test_Send", e => {
			e.UseRetry(r => r.Interval(10, TimeSpan.FromSeconds(10)) );
			e.Consumer<MyConsumer>();
		});
	});

	serviceBus.Start();
	serviceBus.Publish(new My());
	Console.ReadKey();
	serviceBus.Stop();

Is this expected?

To workaround I tried switching to using .UseDelayedRedelivery() as described in issue #493, but can’t get it to compile no matter how I type it out:

	config.UseDelayedExchangeMessageScheduler();
	
	...
	
	e.Consumer<MyConsumer>(c => {
		c.UseDelayedRedelivery<IMy>(Retry.Interval(10, TimeSpan.FromSeconds(10)));
	});

I noticed the DelayRetry_Specs.cs test uses .Handler(…) instead of .Consumer(…) and indeed that compiles, runs, and throws no exceptions in serviceBus.Stop().

Should .UseDelayedRedelivery() work with consumers too?

Dependencies: “MassTransit”: “3.5.4”, “MassTransit.RabbitMQ”: “3.5.4”

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
ushenkodmitrycommented, Feb 28, 2018

I personally vote for this change. Stopping the bus should cancel all activities, including retries. I my case i only stop the bus when the application is going to close. I don’t know any scenarios, when the bus must be stopped and then started again, during the lifetime of the host process. But, if such scenarios do exist, then it is desirable that retry attempts are not going to be lost during bus restart.

вт, 27 февр. 2018 г. в 22:27, Chris Patterson notifications@github.com:

I’m considering this change, but I need some feedback.

When the bus is stopped, I changed it so that the CancellationToken on every ReceiveContext is signaled. If a consumer is in a retry loop, it exits immediately and the message remains on the queue in the current position (it is a NACK essentially).

This also means that if a consumer is executing, and makes an asynchronous call using the same token, that async operation would also be cancelled. Since previously the consumers would complete gracefully and then the consumer would exit, this is a pretty serious change, and honestly, I don’t think I like it at all. But, in reality, it does make sense in some ways.

The other option is to keep the CancellationToken on the ConsumeContext separate from the one on the ReceiveContext, but again, not sure what value that adds other than confusing things.

Or I can add a “Stopping” token to the receive context, and use that in the retry filter, but that isn’t easy since the retry filter uses the PipeContext CancellationToken, so that’s sort of not going to work…

I dont’ want to make it weird, I want it to be consistent.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/MassTransit/MassTransit/issues/780#issuecomment-368996203, or mute the thread https://github.com/notifications/unsubscribe-auth/AP7WHx2v2sKTH5Gz2Fpx83YVLl-4eAB1ks5tZFbPgaJpZM4LrIh_ .

1reaction
paveldaynekocommented, Jun 23, 2017

Hello, I ran into exactly the same issue and using UseDelayedRedelivery() looks like a great workaround. But is there any update regarding that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stopping the bus when consumer is in retry state somehow ...
I'm testing a message consumption retry mechanism and testing what will happen if I cancel processing (stop the bus) while message is still ......
Read more >
Exceptions
MassTransit retry filters execute in memory and maintain a lock on the message. As such, they should only be used to handle short,...
Read more >
Event Grid message delivery and retry - Azure
Describes how Azure Event Grid delivers events and how it handles undelivered messages.
Read more >
Error Handling and Message Redelivery in MassTransit
In this article, I will try to explain some of the problems like Error handling and message Redelivery that I have encountered in...
Read more >
Messaging through a service bus in .NET using MassTransit ...
Retry.All() will include all exception types in the retry policy. Finally we have a Filter method which accepts a Func that returns a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found