Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectContainer.Dispose() does not seem to be thread safe #50

Open
zadigus opened this issue Jun 28, 2021 · 1 comment
Open

ObjectContainer.Dispose() does not seem to be thread safe #50

zadigus opened this issue Jun 28, 2021 · 1 comment

Comments

@zadigus
Copy link

zadigus commented Jun 28, 2021

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?

@zadigus
Copy link
Author

zadigus commented Jun 29, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant