Volodymyr Usarskyy
Volodymyr Usarskyy

Reputation: 1287

MassTransit (over RabbitMQ) IServiceBus.Publish(...) hangs unexpectedly

I'm developing web based solution using MassTransit + RabbitMQ. Some time ago I noticed that when I run my integration tests some of them start unexpectedly and very inconsistently hanging for a very loooong time. After short investigation I found out that my code hangs somewhere inside ServiceBus.Publish(T message) method.

Unfortunately MassTransit documentation and blog posts that I found do not answer what is the reason and how to solve this problem. Hopefully here somebody could help to resolve it.

Here is how I initialize my service bus:

ServiceBusFactory.New(sbc =>
      {
        sbc.ReceiveFrom(busAddress);
        sbc.UseRabbitMq(r =>
        {
          r.ConfigureHost(new Uri(busAddress),
            cfg =>
            {
              if (!string.IsNullOrEmpty(busUser) && !string.IsNullOrEmpty(busPass))
              {
                cfg.SetPassword(busPass);
                cfg.SetUsername(busUser);
              }
            });
        });

        sbc.UseControlBus();
        sbc.SetCreateMissingQueues(true);
        sbc.SetCreateTransactionalQueues(true);
        sbc.SetNetwork("workgroup");
        sbc.UseBsonSerializer();
        sbc.SetDefaultRetryLimit(2);
        sbc.SetDefaultTransactionTimeout(new TimeSpan(0, 0, timeoutSec));

        sbc.Validate();
      });

timeoutSec value is 10 Service bus is initialized once and registered in Autofac container.

Publishing happens by calling IServiceBus.Publish(...) method.

One of the solutions is to wrap Publish(...) method into Task and use Task.Wait(...) method to enforce timeout but this does not look like a good solution.

I would be really appreciated for any kind of help!

IMPORTANT UPDATE 2017-01-26: this question is related to MT v2. It seems that MT v3 has similar issue if one forgets "await". My problem is not related to "await" and currently (still) does not have a good solution (as far as I know). I would strongly recommend everyone to migrate to MT v3.

Upvotes: 0

Views: 2760

Answers (2)

Alexey Zimarev
Alexey Zimarev

Reputation: 19640

Send and Publish in MassTransit v3 are asynchronous by nature. This is how they are designed. You get typical issue when calling async methods without awaiting them - the calling context gets disposed before the task is completed. This is very common. You have to await it. MassTransit gives you a utility to call it from synchronous method:

TaskUtil.Await(() => bus.Publish(message));

Upvotes: 1

Volodymyr Usarskyy
Volodymyr Usarskyy

Reputation: 1287

I asked same question on MassTransit Google group and one of the participants pointed me to his question and the only one (at this point of time) solution: https://groups.google.com/forum/#!msg/masstransit-discuss/oC1FOe6KsAU/VdoTrkcdHS0J

Basically, it seems that the only way to solve my issue is to wrap message publishing code into a Task.

Upvotes: 0

Related Questions