Repository navigation
Description
Description
When adding ApplicationInsights to an ASP.NET core web app's IServiceCollection via AddApplicationInsightsTelemetry() and adding a call to ConfigureOptions<ConfigureApplicationInsightsServiceOptions>() to configure ApplicationInsightsServiceOptions, we are experiencing a deadlock.
Sample code
Program.cs:
services.ConfigureOptions<ConfigureApplicationInsightsServiceOptions>();
services.AddApplicationInsightsTelemetry();ConfigureApplicationInsightsServiceOptions.cs:
public class ConfigureApplicationInsightsServiceOptions : IConfigureOptions<ApplicationInsightsServiceOptions>
{
public ConfigureApplicationInsightsServiceOptions(
SecretClient secretClient)
{
SecretClient = secretClient;
}
public SecretClient SecretClient { get; }
public void Configure(ApplicationInsightsServiceOptions options)
{
KeyVaultSecret secret = SecretClient.GetSecret("appinsights");
options.ConnectionString = secret.Value;
}
}Reproduction Steps
- Call
ConfigureOptions<ConfigureApplicationInsightsServiceOptions>()on theIServiceCollectionas shown in the description - Call
AddApplicationInsightsTelemetry()on theIServiceCollectionas shown in the description
Expected behavior
ConfigureApplicationInsightsServiceOptions.Configure should be invoked when the WebApplicationBuilder is building
Actual behavior
Deadlock. See running tasks here:
Somehow it is blocking at
Line 84 in 7575716
| lock (callSite) |
Regression?
Seems like it is a regression. A similar method (yielding the same deadlock) is documented here, from 2021: https://www.uveta.io/categories/blog/unclutter-startup-cs/#Application-Insights
Known Workarounds
No response
Configuration
- .NET 6.0
- Windows
- x64
- Plain ASP.NET Razor Web App
Other information
No response

Activity
tarekgh commentedon Jun 29, 2023
Moved it to aspnet side as looks it is not a runtime issue. The stack showing hot reload usage.
CC @reyang as app insights is part of this issue.
jonathan-vogel-siemens commentedon Jun 30, 2023
Just checked and it is the same even without any debugger attached
davidfowl commentedon Jul 3, 2023
@tarekgh I’m not sure this is an asp.net problem. I’m guessing there’s some sync over async happening resulting in the deadlock. It’s easy to make this sorta thing happen if you try to use async with DI
tarekgh commentedon Jul 3, 2023
@davidfowl this doesn't seem to be the runtime problem either. I am just seeing the hot reload in the deadlock stack so I thought would be good if aspnet can do the initial investigation. Also, this involves app insights which might be contributing to the issue. This is why I tagged @reyang just in case I have some thoughts.
davidfowl commentedon Jul 3, 2023
Pretty sure it's not a problem at all, more of a misuse/misunderstanding of DI.
That image isn't showing a stack, it's the task window in visual studio.
@jonathan-vogel-siemens Can you show the parallel stacks window?
jonathan-vogel-siemens commentedon Jul 4, 2023
@davidfowl see below.
Thing is, we are using the same approach to configure
MicrosoftIdentityOptions, i.e. consumingSecretClientto fetch the app registration's client secret, which works seamlessly. Sample code:davidfowl commentedon Jul 4, 2023
Options are resolved on first use not on startup. @tarekgh this is a dependency injection issue, I’m going to move it back to runtime.
@jonathan-vogel-siemens can you also show parallel tasks (switch threads to tasks in the parallel stacks window)
tarekgh commentedon Jul 4, 2023
@davidfowl I moved it back to the runtime repo.
15 remaining items
Add test for factory-based circular dependency detection
Implement circular dependency detection for factory-based services
Implement re-entrance detection in VisitRootCache using ThreadStatic …
Detect circular dependencies in factory-based service registrations a…