Description
Packages and version:
2.23.0
<PackageVersion Include="Microsoft.ApplicationInsights" Version="$(AppInsightsVersion)" />
<PackageVersion Include="Microsoft.ApplicationInsights.DependencyCollector" Version="$(AppInsightsVersion)" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(AppInsightsVersion)" />
<PackageVersion Include="Microsoft.ApplicationInsights.ServiceFabric.Native" Version="2.3.1" />
<PackageVersion Include="Microsoft.ApplicationInsights.TraceListener" Version="$(AppInsightsVersion)" />
<PackageVersion Include="Microsoft.ApplicationInsights.WorkerService" Version="$(AppInsightsVersion)" />
<PackageVersion Include="Microsoft.ApplicationInsights.WindowsServer" Version="$(AppInsightsVersion)" />
Runtime version:
.Net 8
Hosting environment:
Windows
Describe the bug
After adding app insights to dependency injection, when the ApplicationInsightsLoggerProvider is called it is passed an IOptions. The underlying implementation is UnnamedOptionsManager which has a Value property with a get method that creates an instance of TelemetryConfiguraton which is IDisposable. The value is then passed to the constructor of TelemetryClient which is not IDisposable. TelemetryConfiguration and the listener thread that it starts are leaked. We ended up with close to 1M threads in memory along with the ExecutionContext those threads dragged along.
To work around this, we grab the instance of IOptions from the service provider and save the value so we can dispose it later.
To Reproduce
- add a valid app insights connection string where specified
- set a breakpoint at the end of the for loop scope
- open a Threads window before debugging
- start debugging
- observe a leaked InMemoryTransmitter appear in the threads list for each iteration
Note: - you may not see one the first time but 2 may appear on the second iteration. if you run the loop without a breakpoint you may not see any appear. We had 950K of these leak in a long running application.
For Immediate Support
For immediate support relating to the Application Insights .NET SDK we encourage you to file an Azure Support Request with Microsoft Azure instead of filing a GitHub Issue in this repository.
You can do so by going online to the Azure portal and submitting a support request. Access to subscription management and billing support is included with your Microsoft Azure subscription, and technical support is provided through one of the Azure Support Plans. For step-by-step guidance for the Azure portal, see How to create an Azure support request. Alternatively, you can create and manage your support tickets programmatically using the Azure Support ticket REST API