Skip to content

HostedTaskService.StopAsync throws LockRecursionException when using WebApplicationFactory #304

Open
@MarcelMichau

Description

@MarcelMichau

Version

What package version of the SDK are you using.

  • Nuget package version: 1.0.1
  • dll product version: 1.0.1+611dcc0c64

Describe the bug
When running integration tests using WebApplicationFactory for a project which includes an agent added via builder.AddAgent<T>(), during test cleanup/disposal of the WebApplicationFactory, the following exception is thrown:

System.Threading.LockRecursionException
Recursive write lock acquisitions not allowed in this mode.
   at System.Threading.ReaderWriterLockSlim.TryEnterWriteLockCore(TimeoutTracker timeout)
   at System.Threading.ReaderWriterLockSlim.TryEnterWriteLock(TimeSpan timeout)
   at Microsoft.Agents.Hosting.AspNetCore.BackgroundQueue.HostedTaskService.StopAsync(CancellationToken stoppingToken)
   at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
   at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.DisposeAsync()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.DisposeAsync()
   at MyProject.IntegrationTestFixture.DisposeAsync() in C:\path\to\IntegrationTestFixture.cs:line 67
   at Xunit.Sdk.ExceptionAggregator.RunAsync(Func`1 code) in /_/src/xunit.core/Sdk/ExceptionAggregator.cs:line 90

To Reproduce
I will try & setup a minimal reproduction soon, as I've only experienced this in a larger project. The general steps would be:

  1. Use the Empty Agent sample as a base project
  2. Add an XUnit Tests Project & configure a simple integration test using the steps outlined here
  3. Run the tests & observe that they pass, but test execution continues for a few minutes until the above exception is thrown

Please provide Code Snippets, Channel type, and any special configuration we will need to reproduce this problem.
N/A

Expected behavior
Any Hosted Services setup by builder.AddAgent<T>() should stop gracefully without throwing exceptions when running integration tests with WebApplicationFactory.

Screenshots
N/A

Hosting Information (please complete the following information):

  • How are you Hosting this: Local Development Environment - Windows 11 Enterprise
  • Are you deploying: N/A
  • Are you using Azure Bot Services: N/A
  • What Client are you using: N/A
  • What .net version is your build in: .NET 9

Additional context
N/A

Metadata

Metadata

Assignees

Labels

triageInitial state for our team to determine nessessary action

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions