AggregationException : Task has been cancelled. when trying to Publish message.

537 views
Skip to first unread message

P

unread,
Feb 5, 2016, 9:01:06 AM2/5/16
to masstransit-discuss
Hi Guys , 

I am hoping someone has encountered this error before. 

I am getting a intermittent AggergationException when calling the Publish method from my code.

This is my code. 

public void Send(T message)
     {
         lock(_locker)
         { 
         using (Bus.Start())
         {
             
             var publishTask = Bus.Publish<T>(message);
 
 
 
                 publishTask.Wait();
             
 
             }
         }
     }


The code always succeds when I call it from my ASP.NET web application , but it fails when i call it from some of my consumer thread. 

It seems some consumers struggle to send messages using the Publish method , generall it seems to be consumers which are small .

For example this code inside one of my consumers fails consistenlty

           lock(ExpectedTaskCode)
           {
 
           
           try
           {
// get the wrapper class which has the Publish method in it.
               var msg = Container.GetInstanceWrapper<IMessageBus<NotificationEvent>>();
// call the Send function above which is part of wrapper class
               msg.Send(new NotificationEvent() {  }" });

           }
           catch(Exception ex)
           { 
// always get a 'Task was cancelled' exception.
}

What could be the reason ? My guess is that the parent thread in which the send function is executing is dying before the publishTask finishes .
But i dont know what else to check to debug this issue .

Is there a log i can switch on to see whats really going wrong ?

P

unread,
Feb 5, 2016, 9:02:32 AM2/5/16
to masstransit-discuss

PS :This error only occurs when calling the publish inside a consumer , it works fine otherwise
 

Chris Patterson

unread,
Feb 5, 2016, 9:25:18 AM2/5/16
to masstrans...@googlegroups.com
Yeah, so you're mixing things like lock() and calling .Wait() and all sorts of ways to really block the .NET thread pool which is a really bad way to use the TPL. I'd highly suggest looking at some of the samples on the MassTransit GitHib repo to see some examples of how to properly use Tasks from controllers.

Also, it looks like you're using a custom wrapper on the bus, which I can't see but may contain some of the same threading problems.

Another thing, if you're in a consumer, you should use the ConsumeContext to Publish, since it maintains the lineage of the ConversationId. 

__
Chris Patterson




On Fri, Feb 5, 2016 at 6:02 AM -0800, "Pratik Vasani" <pnvp...@gmail.com> wrote:


PS :This error only occurs when calling the publish inside a consumer , it works fine otherwise
 

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/27fca0d6-8b0d-4272-b268-e1f91069a217%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wayne Brantley

unread,
Feb 5, 2016, 1:41:43 PM2/5/16
to masstransit-discuss
Definitely would help if you could start using the async/await tasks instead of the synchronous code trying to 'wait' the async.
Nothing but problems await you.  (pun intended)  :-)


On Friday, February 5, 2016 at 9:25:18 AM UTC-5, Chris Patterson wrote:
Yeah, so you're mixing things like lock() and calling .Wait() and all sorts of ways to really block the .NET thread pool which is a really bad way to use the TPL. I'd highly suggest looking at some of the samples on the MassTransit GitHib repo to see some examples of how to properly use Tasks from controllers.

Also, it looks like you're using a custom wrapper on the bus, which I can't see but may contain some of the same threading problems.

Another thing, if you're in a consumer, you should use the ConsumeContext to Publish, since it maintains the lineage of the ConversationId. 

__
Chris Patterson




On Fri, Feb 5, 2016 at 6:02 AM -0800, "Pratik Vasani" <pnvp...@gmail.com> wrote:


PS :This error only occurs when calling the publish inside a consumer , it works fine otherwise
 

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
Message has been deleted

P

unread,
Feb 8, 2016, 11:20:16 AM2/8/16
to masstransit-discuss
The ConsumerContext chris mentioned was promising. I have got my code to work using the Consumer Observer and Context on that observer.

I must confess my code is pretty messy with the Locks and waits , part of it is because i copied the code i had changed to debug what was going wrong ( i was suspecting that the Task created by Publish is getting killed before it completed execution )
 and part of it is having to support a partially async application. 

The consumer context does seem like a much cleaner way of doing what I want.
Thank you for the replies.

Pratik
Reply all
Reply to author
Forward
0 new messages