-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
TestServer causes a deadlock #21218
Comments
We should fix this and potentially backport to 3.1 cc @Tratcher |
We have already addressed this in 3.0+ as part of the move to IHostBuilder. aspnetcore/src/Hosting/TestHost/test/TestServerTests.cs Lines 67 to 79 in 54722a5
|
@Tratcher hm, I did not quite get how it was solved. The blocking call is still in the constructor of the
Hence, it will still be invoked the same way it is invoked now. |
When using IHostBuilder and the container registration, that affected IWebHostBuilder constructor is not used anymore. |
Ah, I see. Ok, another question then. What are the plans for this? |
@Tratcher we should fix the blocking code in the constructor and update the docs.. |
How? And why bother when WebHostBuilder is scheduled for obsoletion in 5.0 (#20964)? People need to move to IHostBuilder regardless. @maxcherednik can you give a specific example from the docs that's out of date? That doc primarily covers WebApplicationFactory and barely mentions TestServer. The doc for TestServer is still in progress. dotnet/AspNetCore.Docs#16953 |
Start initialization in the constructor and finish it when the first incoming request is made.
That issue is reasonable until the related issues are fixed. Most of our own test code still uses WebHostBuilder, so we haven't even fixed the issue for our own tests... |
@Tratcher in the documentation I mentioned:
So anyway - the TestSever is blocking. @davidfowl there is no need to actually start anything in the constructor. We can use the constructor for cheap wiring and the actual start could be a separate method. Btw, it can still be lazy, then CreateClient should be async as well. |
@maxcherednik that doesn't follow. The stack trace should look like this:
DI shouldn't be able to call the aspnetcore/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs Lines 161 to 164 in c7a2c06
|
Right, we fixed this in 3.0. I don't think we can fix it in 2.1 without breaking a lot of people. |
I would agree. Backporting to 2.1 is a very high bar and if it's even remotely breaking it's going to be a non-starter. Are there workarounds? |
I was not specifically asking to fix this in 2.1, but thank you for the validation. Btw, for the ones who are facing similar issue we came up with a workaround - reduce the number of concurrently blocking things to 1. private static readonly SemaphoreSlim s_asyncLock = new SemaphoreSlim(1, 1);
private async Task StartInternalTestServer()
{
// Aspnet core test server built by wep application factory blocks async operation in the test server constructor
// which means it blocks a worker thread in xunit thread pool. Throttling test server creation to one at a time
// ensures at most one threads will be blocked. Otherwise we have to increase xunit config maxParallelThreads
// which then ends up in maximum CPU and high IOPs resulting in sql timeouts
await s_asyncLock.WaitAsync();
try
{
// Before this web server is not started yet
_appFactory.CreateClient();
}
finally
{
s_asyncLock.Release();
}
} |
At the end of all this, even in 3.1 |
Only if you're using the legacy WebHostBuilder rather than the new HostBuilder. |
Closing as we don't plan to backport this to 2.1 and there are resolutions in 3.0+. |
Describe the bug
We have a lot of tests which are written in a way described here:
https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1#basic-tests-with-the-default-webapplicationfactory
Sometimes WebApplicationFactory + TestServer cause a deadlock while running under xUnit.
This particular line causes this:
aspnetcore/src/Hosting/TestHost/src/TestServer.cs
Line 66 in 54722a5
Proposition
Can we have an explicit Start/Stop methods both on WebApplicationFactory and TestServer which could be called from the xunit lifetime interfaces of the fixture?
To Reproduce
You need to have a lot of integration tests starting lots(more than the cores you have) of TestServers in parallel.
Further technical details
The text was updated successfully, but these errors were encountered: