RabbitMQ channel leak when bus was failed to start

29 views
Skip to first unread message

Alina

unread,
Jul 8, 2017, 4:01:06 AM7/8/17
to masstransit-discuss
Hello,

(Using MassTransit 3.5.7 (via Nuget), RabbitMQ 3.6.10, Erlang 19.0)

Looks like that MassTransit not clear up RabbitMQ channels, when bus is failed to start.

Here is the test prodgram - 

using System;
using System.Threading;
using MassTransit;

namespace Test
{
  class Program
  {
        static void Main()
        {
            IBusControl busControl = null;
            var failCount = 0;
            var busNotInitialised = true;

            //Keep RabbitMQ switched off for a few iterations of this loop, then switch it on.
            while (busNotInitialised)
            {
                busControl = Bus.Factory.CreateUsingRabbitMq(sbc =>
                {
                    var host = sbc.Host(new Uri("rabbitmq://localhost/"), h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    });

                    sbc.ReceiveEndpoint(host, "some_queue", endpoint =>
                    {
                        endpoint.Handler<string>(async context =>
                        {
                            await Console.Out.WriteLineAsync($"Received: {context.Message}");
                        });
                    });
                });

                try
                {
                    busControl.Start();
                    busNotInitialised = false;            
                }
                catch (Exception)
                {
                    Console.WriteLine($"Attempt:{++failCount} failed.");

                    //wait some time
                    Thread.Sleep(5000);
                }
            }

            //At this point, using RabbitMq's management web page, you will see failCount + 1 channels.
            busControl.Stop();
            //At this point, using RabbitMq's management web page, you will see failCount channels.

            Console.ReadLine();
    }
  }
}

It continuously tries to create a service bus that uses RabbitMQ. 
The program breaks out of the loop as soon as the service bus is created successfully.
After running the program for a couple of minutes (with stopped RabbitMQ) and stopping on a breakpoint, I can see a lot of worker threads (one for each failed service bus creation attempt).
After startup RabbitMQ, all of those "dangling" connect threads will connect to RabbitMQ.

If I try to shut down the bus, the latest connection ( that belongs to the bus that was successfully created), is closed. All of the other dangling connections are still connected to RabbitMQ.

Problem here is that those dangling threads, when connected, reads data from the queue which leads to data loss.

Is there any way to get around this problem?
Thanks.
Reply all
Reply to author
Forward
0 new messages