ObjectContainer.Dispose() does not seem to be thread safe
I am writing a plugin for SpecFlow to support Ninject. Somewhere in my tests, it turns I resolve the IObjectContainer out of my Ninject kernel and then I dispose the IObjectContainer. To be more precise, I am doing something like this:
[Test]
public void ObjectContainer_Can_Be_Resolved_From_Scenario_Kernel()
{
// Arrange
var plugin = new NinjectPlugin();
this.globalContainer.RegisterTypeAs<NinjectTestObjectResolver, ITestObjectResolver>();
this.globalContainer
.RegisterTypeAs<TScenarioContainerFinder, ContainerFinder<ScenarioDependenciesAttribute>>();
this.globalContainer
.RegisterTypeAs<TFeatureContainerFinder, ContainerFinder<FeatureDependenciesAttribute>>();
this.globalContainer
.RegisterTypeAs<TTestThreadContainerFinder, ContainerFinder<TestThreadDependenciesAttribute>>();
plugin.Initialize(events, Mock.Of<RuntimePluginParameters>(), Mock.Of<UnitTestProviderConfiguration>());
events.RaiseCustomizeGlobalDependencies(this.globalContainer, this.specFlowConfiguration);
using var expectedScenarioContainer = new ObjectContainer(this.globalContainer);
expectedScenarioContainer .RegisterInstanceAs(
new ScenarioInfo(string.Empty, string.Empty, Array.Empty<string>(), new OrderedDictionary()));
this.pluginEvents.RaiseCustomizeScenarioDependencies(scenarioContainer);
var scenarioKernel = expectedScenarioContainer.Resolve<IKernel>();
// Act
var actualScenarioContainer = scenarioKernel.Get<IObjectContainer>();
// Assert
actualScenarioContainer.Should().BeSameAs(expectedScenarioContainer);
}
At the end of that test, expectedScenarioContainer.Dispose() is called, thanks to the using directive. That calls the Dispose() on the IObjectContainer, which calls the Dispose() method of my IKernel (my ninject container). Because my IKernel is provided with
kernel.Bind<IObjectContainer>().ToConstant(objectContainer);
the IObjectContainer.Dispose() is once again called, therefore we hit this code concurrently. In that process, the ObjectContainer.objectPool.Values are modified by another process within the loop, and the disposing process results in failure.
Would that be imagineable to fix that?
One more detail: this bug occurs with SpecFlow 3.9.8 and BoDi 1.5.0. The last versions the code in my above post worked was SpecFlow 3.6.23 and BoDi 1.4.1.