JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
572 stars 119 forks source link

Duplicate key issue #66

Closed danspam closed 6 years ago

danspam commented 6 years ago

I am getting an intermittent error that I am still trying to pin down occurring at: https://github.com/JasperFx/lamar/blob/2693d503b560283950119bbae12987b3da877342/src/Lamar/IoC/Instances/Instance.cs#L221

Last time I managed to catch it, it was trying to resolve Microsoft.AspNetCore.Mvc.Infrastructure.DefaultOutputFormatterSelector, which is a singleton. Sure enough, it was trying to store an instance, when there was already one in the collection.

Looking up the stack, at:

https://github.com/JasperFx/lamar/blob/2693d503b560283950119bbae12987b3da877342/src/Lamar/IoC/Instances/ConstructorInstance.cs#L123

I am wondering how this could have possibly occurred.

Just a hypothesis, but could this be a threading issue where one thread is instantiating and storing the singleton instance whilst another attempts to resolve it and by the time it attempts to store it, the first thread has already stored an instance and so you get the duplicate key? Perhaps there is some locking elsewhere that stops such a thing happening?

I will continue to try and reproduce this reliably in the meantime but I thought I would report it.

jeremydmiller commented 6 years ago

@danspam There's some better locking in 1.1.1 that'll be available in Nuget when it's indexed later today.