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.

Consume is repeated indefinetely when raised OperationCancelledException

See original GitHub issue

Is this a bug report?

Yes

Can you also reproduce the problem with the latest version?

Yes

Environment

  1. Operating system: Windows 10 Enterprise
  2. Visual Studio version: 2017, v15.9.8
  3. Dotnet version: NetCore SDK 2.2.104

Steps to Reproduce

  1. Bus configured with RabbitMQ transport
  2. Retry policy on ‘ReceiveEndpoint’ set to Immediate(1)
  3. Publish one event
  4. Consumer throws TaskCancellationException or OperationCancelledException

Expected Behavior

The system calls consumer trying to consume message, then retry policy reiterate it exactly one time (UseMessageRetry(r => r.Immediate(1)))

Actual Behavior

The system calls consumer trying to consume message without end. The same scenario with ‘InMemory’ transport works as we expected.

Reproducible Demo

    public class Program
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.Host(new Uri("rabbitmq://localhost/"), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });

                cfg.ReceiveEndpoint("input-queue", e =>
                {
                    e.UseMessageRetry(r => r.Immediate(1));

                    e.Consumer<MyConsumer>();
                });
            });
            bus.Start();

            bus.Publish(new SimpleEvent { Id = 123 });

            Console.ReadLine();
        }
    }
    public class MyConsumer : IConsumer<SimpleEvent>
    {
        public Task Consume(ConsumeContext<SimpleEvent> context)
        {
            var attempt = context.GetRetryAttempt();
            throw new TaskCanceledException("task cancelled");
        }
    }
    public class SimpleEvent
    {
        public int Id { get; set; }
    }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
newpmacommented, May 18, 2019

Thank you for quick response. What do you recommend to avoid infinite loop in case, that we actually faced, when consumer calls http api that is not accessible due to some network issue. I know, easy answer is to solve network issue, but still it could sometimes happen. What I missed in above ‘Reproducible Demo’ is that our abstraction over http client wraps exceptions.

    public class Program
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.Host(new Uri("rabbitmq://localhost/"), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });

                cfg.ReceiveEndpoint("input-queue", e =>
                {
                    e.UseMessageRetry(r => 
                    {
                        r.Immediate(1);
                    });

                    e.Consumer<MyConsumer>();
                });
            });
            bus.Start();

            bus.Publish(new SimpleEvent { Id = 123 });

            Console.ReadLine();
        }
    }
    public class MyConsumer : IConsumer<SimpleEvent>
    {
        public Task Consume(ConsumeContext<SimpleEvent> context)
        {
            var attempt = context.GetRetryAttempt();
            throw new MyException("my exception", new TaskCanceledException("task cancelled"));
        }
    }
    public class SimpleEvent
    {
        public int Id { get; set; }
    }
    public class MyException : ApplicationException
    {
        public MyException(string message, Exception inner)
            : base(message, inner)
        {
        }
    }
0reactions
phatboygcommented, Sep 26, 2019

This was addressed in v6.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Using BlockingCollection<T>
When there are items in the queue and no limit such as BoundedCapacity or Empty has been reached then the producers and consumer...
Read more >
Confusing Cancellation in BackgroundService · Issue #1245
At this point, I would expect the DerpService to see the cancellation, throw an OperationCanceledException , and thus pass the assertion.
Read more >
Create Sophisticated Asynchronous Applications with ...
The Try…Catch block that catches my OperationCanceledException encloses all of my processing so that raising the Exception skips everything.
Read more >
System.IO.Pipelines in .NET
PipeWriter.FlushAsync supports a way to cancel the current flush operation via PipeWriter.CancelPendingFlush without raising an exception.
Read more >
Polly 7.2.4
All Polly policies are fully thread-safe. You can safely re-use policies at multiple call sites, and execute through policies concurrently on different threads....
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