Skip to content

Deadlock when configuring ApplicationInsightsServiceOptions #88390

Closed

Description

@jonathan-vogel-siemens

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

  1. Call ConfigureOptions<ConfigureApplicationInsightsServiceOptions>() on the IServiceCollection as shown in the description
  2. Call AddApplicationInsightsTelemetry() on the IServiceCollection as shown in the description

Or see here: https://github.com/jonathan-vogel-siemens/IssueRepro88390

Expected behavior

ConfigureApplicationInsightsServiceOptions.Configure should be invoked when the WebApplicationBuilder is building

Actual behavior

Deadlock. See running tasks here:

image

Somehow it is blocking at

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

  1. ghost added
    untriaged
    on Jun 28, 2023
  2. transferred this issue fromdotnet/runtimeon Jun 29, 2023
  3. tarekgh commented on Jun 29, 2023

    @tarekgh
    Member

    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.

  4. jonathan-vogel-siemens commented on Jun 30, 2023

    @jonathan-vogel-siemens
    Author

    Just checked and it is the same even without any debugger attached

  5. davidfowl commented on Jul 3, 2023

    @davidfowl
    Member

    @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

  6. tarekgh commented on Jul 3, 2023

    @tarekgh
    Member

    @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.

  7. davidfowl commented on Jul 3, 2023

    @davidfowl
    Member

    @davidfowl this doesn't seem to be the runtime problem either.

    Pretty sure it's not a problem at all, more of a misuse/misunderstanding of DI.

    I am just seeing the hot reload in the deadlock stack so I thought would be good if aspnet can do the initial investigation.

    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?

  8. jonathan-vogel-siemens commented on Jul 4, 2023

    @jonathan-vogel-siemens
    Author

    @davidfowl see below.

    Thing is, we are using the same approach to configure MicrosoftIdentityOptions, i.e. consuming SecretClient to fetch the app registration's client secret, which works seamlessly. Sample code:

    public class ConfigureMicrosoftIdentityOptions : IConfigureNamedOptions<MicrosoftIdentityOptions>
    {
        public ConfigureMicrosoftIdentityOptions(
            SecretClient secretClient,
            CloudHubConfiguration config)
        {
            SecretClient = secretClient;
            Config = config;
        }
    
        public SecretClient SecretClient { get; }
        public CloudHubConfiguration Config { get; }
    
        public void Configure(MicrosoftIdentityOptions options)
        {
            KeyVaultSecret secret = SecretClient.GetSecret(Config.ApplicationClientId);
            options.Instance = Config.AzureADInstance;
            options.TenantId = Config.TenantId;
            options.ClientId = Config.ApplicationClientId;
            options.ClientSecret = secret.Value;
    
            options.ResponseType = "id_token";
        }
    
        public void Configure(string name, MicrosoftIdentityOptions options) => Configure(options);
    }

    ParallelStacks_49099

  9. davidfowl commented on Jul 4, 2023

    @davidfowl
    Member

    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)

  10. transferred this issue fromdotnet/aspnetcoreon Jul 4, 2023
  11. tarekgh commented on Jul 4, 2023

    @tarekgh
    Member

    @davidfowl I moved it back to the runtime repo.

  12. 15 remaining items

  13. modified the milestones: 8.0.0, 9.0.0 on Aug 11, 2023
  14. modified the milestones: 9.0.0, 10.0.0 on Jul 25, 2024
  15. modified the milestones: 10.0.0, Future on Jul 26, 2025
  16. added
    in-pr
    on Feb 12, 2026
  17. locked and limited conversation to collaborators on Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-Extensions-DependencyInjectionin-prneeds-further-triage

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions