Repository: MassTransit/MassTransit

Issue #571: RabbitMqBasicConsumer+SerilogLogger+LazyMemoryCache concurrency fail
Created: 2016-05-25T09:02:47Z
My code:
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    var logger = new LoggerConfiguration()
        .MinimumLevel.Verbose()
        .WriteTo.ColoredConsole()
        .CreateLogger();
    cfg.UseSerilog(logger);
....
}
bus.Start();

Sometimes an exception 'System.InvalidOperationException' is thrown:

The item was not added to the cache: MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer
at MassTransit.Util.Caching.LazyMemoryCache`2.<>c__DisplayClass16_0.b__0() in E:\Projects\Packages\External\MassTransit-develop\src\MassTransit\Util\Caching\LazyMemoryCache.cs:line 130

The 'MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer' is already added to cache in other thread from:
readonly ILog _log = Logger.Get<RabbitMqBasicConsumer>();
of RabbitMqBasicConsumer.
Example.
Winner stack:
MassTransit.RabbitMqTransport.dll!MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer.RabbitMqBasicConsumer(MassTransit.RabbitMqTransport.ModelContext model, System.Uri inputAddress, MassTransit.Pipeline.IPipe<MassTransit.ReceiveContext> receivePipe, MassTransit.IReceiveObserver receiveObserver, MassTransit.Util.ITaskScope taskSupervisor) Row 70    C#
MassTransit.RabbitMqTransport.dll!MassTransit.RabbitMqTransport.Pipeline.RabbitMqConsumerFilter.MassTransit.Pipeline.IFilter<MassTransit.RabbitMqTransport.ModelContext>.Send(MassTransit.RabbitMqTransport.ModelContext context, MassTransit.Pipeline.IPipe<MassTransit.RabbitMqTransport.ModelContext> next) Row 55   C#
MassTransit.RabbitMqTransport.dll!MassTransit.RabbitMqTransport.Pipeline.PrepareReceiveQueueFilter.MassTransit.Pipeline.IFilter<MassTransit.RabbitMqTransport.ModelContext>.Send(MassTransit.RabbitMqTransport.ModelContext context, MassTransit.Pipeline.IPipe<MassTransit.RabbitMqTransport.ModelContext> next) Row 100   C#
MassTransit.dll!MassTransit.Util.LimitedConcurrencyLevelTaskScheduler.NotifyThreadPoolOfPendingWork.AnonymousMethod__8_0(object _) Row 105  C#


Loser stack:
MassTransit.dll!MassTransit.Util.Caching.LazyMemoryCache<string, MassTransit.Logging.ILog>.Get.AnonymousMethod__0() Row 127 C#
MassTransit.dll!MassTransit.Util.LimitedConcurrencyLevelTaskScheduler.TryExecuteTaskInline(System.Threading.Tasks.Task task, bool taskWasPreviouslyQueued) Row 131  C#
MassTransit.SeriLogIntegration.dll!MassTransit.SeriLogIntegration.SerilogLogger.Get(string name) Row 44 C#
MassTransit.dll!MassTransit.Logging.Logger.Get(string name) Row 39  C#
MassTransit.dll!MassTransit.Logging.Logger.Get<MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer>() Row 29   C#
MassTransit.RabbitMqTransport.dll!MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer.RabbitMqBasicConsumer(MassTransit.RabbitMqTransport.ModelContext model, System.Uri inputAddress, MassTransit.Pipeline.IPipe<MassTransit.ReceiveContext> receivePipe, MassTransit.IReceiveObserver receiveObserver, MassTransit.Util.ITaskScope taskSupervisor) Row 36    C#
MassTransit.RabbitMqTransport.dll!MassTransit.RabbitMqTransport.Pipeline.RabbitMqConsumerFilter.MassTransit.Pipeline.IFilter<MassTransit.RabbitMqTransport.ModelContext>.Send(MassTransit.RabbitMqTransport.ModelContext context, MassTransit.Pipeline.IPipe<MassTransit.RabbitMqTransport.ModelContext> next) Row 55   C#
MassTransit.RabbitMqTransport.dll!MassTransit.RabbitMqTransport.Pipeline.PrepareReceiveQueueFilter.MassTransit.Pipeline.IFilter<MassTransit.RabbitMqTransport.ModelContext>.Send(MassTransit.RabbitMqTransport.ModelContext context, MassTransit.Pipeline.IPipe<MassTransit.RabbitMqTransport.ModelContext> next) Row 100   C#
MassTransit.dll!MassTransit.Util.LimitedConcurrencyLevelTaskScheduler.NotifyThreadPoolOfPendingWork.AnonymousMethod__8_0(object _) Row 105  C#

Duplicate issue #580: Item not added to the cache
Created: 2016-06-02T14:58:18Z
Updated to MT3.3.3.
Tried to start service - got the error below.
Tried to start it again - it worked the next time.
This same sequence of errors/success has happened on all services since the upgrade to MT3.3.3.    (from 3.2)
First error received contained only this:  RabbitMQ receive transport failed: One or more errors occurred.
Followed by this
System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: The item was not added to the cache: MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer
   at MassTransit.Util.Caching.LazyMemoryCache`2.<>c__DisplayClass16_0.<Get>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at MassTransit.SeriLogIntegration.SerilogLogger.Get(String name)
   at MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer..ctor(ModelContext model, Uri inputAddress, IPipe`1 receivePipe, IReceiveObserver receiveObserver, ITaskScope taskSupervisor)
   at MassTransit.RabbitMqTransport.Pipeline.RabbitMqConsumerFilter.<MassTransit-Pipeline-IFilter<MassTransit-RabbitMqTransport-ModelContext>-Send>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at MassTransit.RabbitMqTransport.Pipeline.PrepareReceiveQueueFilter.<MassTransit-Pipeline-IFilter<MassTransit-RabbitMqTransport-ModelContext>-Send>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.Pipeline.ReceiveModelFilter.<MassTransit-Pipeline-IFilter<MassTransit-RabbitMqTransport-ConnectionContext>-Send>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.Integration.RabbitMqConnectionCache.<SendUsingExistingConnection>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.RabbitMqTransport.RabbitMqReceiveTransport.<>c__DisplayClass12_0.<<Receiver>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.MassTransitBus.BusReady.<ReadyOrNot>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.Internals.Extensions.TaskExtensions.<WithCancellation>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.MassTransitBus.<StartAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at MassTransit.MassTransitBus.<StartAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MassTransit.Util.TaskUtil.Await[T](Func`1 taskFactory, CancellationToken cancellationToken)
   at MassTransit.MassTransitBus.MassTransit.IBusControl.Start()
   at Miniter.Services.Messaging.DocumentProcessing.Consumer.Program.<>c.<Main>b__0_2(Service tc)
   at Topshelf.ServiceConfiguratorExtensions.<>c__DisplayClass2_0`1.<WhenStarted>b__0(T service, HostControl control)
   at Topshelf.Builders.DelegateServiceBuilder`1.DelegateServiceHandle.Start(HostControl hostControl)
   at Topshelf.Runtime.Windows.WindowsServiceHost.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
---> (Inner Exception #0) System.InvalidOperationException: The item was not added to the cache: MassTransit.RabbitMqTransport.Pipeline.RabbitMqBasicConsumer
   at MassTransit.Util.Caching.LazyMemoryCache`2.<>c__DisplayClass16_0.<Get>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()<---

Closing comment (2016-06-06T23:57:52Z): This is a duplicate #571 I think?