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

Deadlock in integration tests after upgrading to ASP.NET Core 6 #38649

Closed
angularsen opened this issue Nov 26, 2021 · 9 comments
Closed

Deadlock in integration tests after upgrading to ASP.NET Core 6 #38649

angularsen opened this issue Nov 26, 2021 · 9 comments
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-testing MVC testing package investigate Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@angularsen
Copy link

angularsen commented Nov 26, 2021

Describe the bug

After upgrading from ASP.NET Core 5 to 6, our integration tests would hang indefinitely on Azure Pipelines with ubuntu-20.04.

After analyzing dumps with dotnet test --blame-hang-timeout, we found a deadlock on WebApplicationFactory.Dispose() and concurrent tests calling the API.

The root cause

  • Single WebApplicationFactory instance per test class, reused by its test methods to call the API.
  • Hosted service with Task.Delay(TimeSpan.FromSeconds(5), ct) in its IHostedService.StopAsync(), which is used to flush logs and telemetry before the API is allowed to shut down.
  • The delay seems to create a deadlock for shared factories, only on Linux/Ubuntu and only on .NET6.

Workaround
Do not delay IHostedService.StopAsync() for "Test" environment.

Investigation

  • dotnet test on Azure Pipelines ubuntu-20.04 would hang indefinitely after upgrading to ASP.NET Core 6
  • Could not reproduce on Azure Pipelines windows-2019
  • Could not reproduce on Windows, with Rider or Visual Studio 2022
  • Could not reproduce on Windows with dotnet test
  • Could not reproduce on ASP.NET Core 5
  • Could not reproduce on ASP.NET Core 6 when creating individual factories per test method

To Reproduce

Detailed description and repro integration tests here:
Repro hang of integration tests after upgrading to ASP.NET Core 6 - Pull Request #1 · angularsen/NET6-Testing

Exceptions (if any)

On Ubuntu, deadlock.
On Windows (as expected):

[Test Class Cleanup Failure (Test.Net6WebApi.Repro_RED_ConcurrentTestsOnSharedFactory+TestClass3)]: System.AggregateException : One or more hosted services failed to stop. (A task was canceled.)
---- System.Threading.Tasks.TaskCanceledException : A task was canceled.
  Stack Trace:
     at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.DisposeAsync()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.Dispose(Boolean disposing)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.Dispose()
----- Inner Stack Trace -----
   at Test.Net6WebApi.Setup.MySlowStoppingHostedService.StopAsync(CancellationToken cancellationToken) in C:\dev\digma\NET6-Testing\src\Test.Net6WebApi\Setup\MySlowStoppingHostedService.cs:line 30
   at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken)

Further technical details

  • ASP.NET Core version: 6.0.0
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: Rider 2021.2.2, VS 2022 17.1.0 Preview 1.0
  • Include the output of dotnet --info:
dotnet --info Output

WSL Ubuntu 20.10:

.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  21.10
 OS Platform: Linux
 RID:         ubuntu.21.10-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.100/

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  3.1.415 [/usr/share/dotnet/sdk]
  5.0.403 [/usr/share/dotnet/sdk]
  6.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.21 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.12 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.21 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.12 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Windows:

.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  3.1.415 [C:\Program Files\dotnet\sdk]
  5.0.209 [C:\Program Files\dotnet\sdk]
  5.0.301 [C:\Program Files\dotnet\sdk]
  5.0.302 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.2.21505.57 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
@javiercn javiercn added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-testing MVC testing package area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Nov 26, 2021
@halter73
Copy link
Member

halter73 commented Dec 1, 2021

Hosted service with Task.Delay(TimeSpan.FromSeconds(5), ct) in its IHostedService.StopAsync(), which is used to flush logs and telemetry before the API is allowed to shut down.

I'm surprised this doesn't hang in .NET 5. Why should WebApplicationFactory.Dispose() not wait for StopAsync() to complete on the IHost and all its IHostedServices? if the issue is just the blocking, WebApplicationFactory implements IAsyncDisposable as well.

@angularsen
Copy link
Author

@halter73 It is not the 5 second hang that is the problem, that is naturally expected as shown in Windows logs above. What is unexpected is deadlock and infinite hang on Ubuntu, only on .NET6.

@BrennanConroy
Copy link
Member

Could you try using a Noop IHostLifetime implementation and see if the hang still occurs.
See https://github.com/dotnet/aspnetcore/pull/23761/files for reference.

@rafikiassumani-msft rafikiassumani-msft added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Dec 2, 2021
@ghost
Copy link

ghost commented Dec 2, 2021

Hi @angularsen. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@angularsen
Copy link
Author

@BrennanConroy I am certain that a Noop IHostLifetime works, because the workaround I described was to remove the Task.Delay in StopAsync of our IHostedService implementation. This fixed our tests. I believe this is effectively the same as the Noop implementation, because our implementation did little beyond logging and waiting on stop.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Dec 2, 2021
@rafikiassumani-msft rafikiassumani-msft added this to the .NET 7 Planning milestone Dec 7, 2021
@ghost
Copy link

ghost commented Dec 7, 2021

Thanks for contacting us.
We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ghost
Copy link

ghost commented Oct 11, 2022

Thanks for contacting us.
We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

JoeShook pushed a commit to JoeShook/udap-dotnet that referenced this issue May 1, 2023
Receiving this error
"System.AggregateException : One or more hosted services failed to stop. (Not started. Call Start first"
From tests using WebApplicationFactory .
Info here: dotnet/aspnetcore#38649
And possible solution here:  https://github.com/dotnet/aspnetcore/pull/23761/files
JoeShook pushed a commit to JoeShook/udap-dotnet that referenced this issue May 1, 2023
Receiving this error
"System.AggregateException : One or more hosted services failed to stop. (Not started. Call Start first"
From tests using WebApplicationFactory .
Info here: dotnet/aspnetcore#38649
And possible solution here: https://github.com/dotnet/aspnetcore/pull/23761/files
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jun 20, 2023
@mitchdenny
Copy link
Member

Looks like @BrennanConroy's change merged in, I'm assuming this is the long term fix for this issue. Re-open if it isn't Brennan.

@gdoron
Copy link

gdoron commented Jul 16, 2023

@mitchdenny which change were you referring to? Can you kindly add a link so I could see if it would fix our issue as well?

@ghost ghost locked as resolved and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-testing MVC testing package investigate Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
No open projects
Status: No status
Development

No branches or pull requests

8 participants