Repro hang of integration tests after upgrading to ASP.NET Core 6 #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem description
After upgrading from ASP.NET 5 to 6, our integration tests would hang indefinitely on Azure Pipelines with
ubuntu-latest
.After analyzing dumps with
dotnet test --blame-hang-timeout
, we found a deadlock onWebApplicationFactory.Dispose()
and concurrent tests calling the API.The root cause
WebApplicationFactory
instance per test class, reused by its test methods to call the API.Task.Delay(TimeSpan.FromSeconds(5), ct)
in itsIHostedService.StopAsync()
, which is used to flush logs and telemetry before the API is allowed to shut down.Workaround
Do not delay
IHostedService.StopAsync()
for "Test" environment.Investigation
dotnet test
on Azure Pipelinesubuntu-20.04
would hang indefinitely after upgrading to ASP.NET Core 6windows-2019
dotnet test
on WindowsTest setup
This mimics our particular test setup:
AddHostedService<MySlowStoppingHostedService>()
, to add a 5 second delay onStopAsync()
.IClassFixture<TWebApplicationFactory>
to create a shared factory for each class, reused by its test methods that call one or more API methods.Repro
Clone
agl/test-repro
branch.Run tests:
Test setup
Test.Net5WebApi
are ASP.NET Core 5 integration tests (all green).Test.Net6WebApi
are ASP.NET Core 6 integration tests (red and green).Common test setup for both:
Repro_GREEN_ConcurrentTestsWithIndividualFactories
Repro_RED_ConcurrentTestsOnSharedFactory
On Ubuntu 20.04 - dotnet test hangs
Observed on both
ubuntu-20.04
in Azure Pipelines and WSL Ubuntu 20.10 locally, with .NET 6.0.100 SDK.On Windows - Test cleanup fails, but test does not hang
Windows 10 with .NET 6.0.100 SDK.