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

Reqnroll.Microsoft.Extensions.DependencyInjection: Root service provider not disposed #465

Open
olaviedoc opened this issue Feb 14, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@olaviedoc
Copy link

olaviedoc commented Feb 14, 2025

Reqnroll Version

2.3.0

Which test runner are you using?

MSTest

Test Runner Version Number

3.4.3

.NET Implementation

.NET 8.0

Test Execution Method

Visual Studio Test Explorer

Content of reqnroll.json configuration file

Not sure where to find it, but this is what I found when I searched for the file name on the Windows system drive.

{
  "$schema": "https://schemas.reqnroll.net/reqnroll-config-latest.json",

  "bindingAssemblies": [
  ]
}

Issue Description

Singleton services are not disposed when using Reqnroll.Microsoft.Extensions.DependencyInjection.ScenarioDependenciesAttribute to add servicess to the Microsoft.Extensions.DependencyInjection.ServiceCollection.

Steps to Reproduce

  1. Register a disposable singleton service in the service collection, using ScenarioDependenciesAttribute, ie [ScenarioDependencies] public static IServiceCollection RegisterMyServices().
  2. Have the service injected into some class used by a step (resolved from the service provider)
  3. When the test run ends, notice that the Dispose method of the disposable singleton service is not triggered.

Link to Repro Project

https://github.com/olaviedoc/ReqnrollDisposeBugRepro

@olaviedoc olaviedoc added the bug Something isn't working label Feb 14, 2025
@olaviedoc
Copy link
Author

olaviedoc commented Feb 14, 2025

Update: added a repro project link.

Please see MySingletonService for the service that should be disposed (but never is).

Here's the output of the debug window when running it (abbreviated for brevity and privacy reasons):

'testhost.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.13\System.Threading.Tasks.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
16:59:29:929	MySingletonService.MySingletonService executing <<<<<<<<<<<<<<<<<<<<<<
16:59:29:929	MySingletonService.UseTheService executing <<<<<<<<<<<<<<<<<<<<<<
16:59:30:001	The thread '.NET Long Running Task' (8584) has exited with code 0 (0x0).
16:59:30:001	The program '[36444] testhost.exe' has exited with code 0 (0x0).

As you can see MySingletonService.Dispose executing <<<<<<<<<<<<<<<<<<<<<< is never outputted, nor is the debugger triggering any breakpoints for the Dispose method.

@gasparnagy gasparnagy changed the title Root service provider not disposed Reqnroll.Microsoft.Extensions.DependencyInjection: Root service provider not disposed Feb 19, 2025
@304NotModified
Copy link
Contributor

As far as I known this is tricky as this will also dispose other services like the FeatureContext (but correct me if wrong).

But good to know, why do we need to dispose the instance? Is it an issue when the test is finisched?

@gasparnagy
Copy link
Contributor

@304NotModified The dispose behavior of MSDI is pretty strange but this is now addressed with the new design model. However, we can still do a quick fix here. And yes, it will dispose things like FeatureContext (that is normally bad), but fortunately at the current moment the dispose method of FeatureContext and ScenarioContext does not do anything, so it is not causing a direct issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants