From 0bb8b7b29f523ebc87691cfe18e078d7a84b92ed Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 24 Oct 2026 16:38:05 +0800 Subject: [PATCH] Fix telemetry event handler leak --- .../Internal/Telemetry/TelemetryService.cs | 54 +++--- .../TelemetryApplicationLifetime_Tests.cs | 142 ++++++++++++++ .../Abp/Telemetry/TelemetryService_Tests.cs | 182 ++++++++++++++++++ 3 files changed, 354 insertions(+), 24 deletions(-) create mode 100644 framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/TelemetryApplicationLifetime_Tests.cs create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetryService_Tests.cs diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/TelemetryService.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/TelemetryService.cs index 4f8f77b245..577d396d04 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/TelemetryService.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/Telemetry/TelemetryService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; @@ -81,33 +82,38 @@ public class TelemetryService : ITelemetryService, IScopedDependency telemetryActivitySender); }); - AppDomain.CurrentDomain.ProcessExit += (_, _) => - { - try - { - telemetryTask.Wait(TimeSpan.FromSeconds(10)); - } - catch - { - // ignored - } - }; - - Console.CancelKeyPress += (_, _) => - { - try - { - telemetryTask.Wait(TimeSpan.FromSeconds(10)); - } - catch - { - // ignored - } - }; + WaitForTelemetryTaskOnExit(telemetryTask); return Task.CompletedTask; } + private static void WaitForTelemetryTaskOnExit(Task telemetryTask) + { + EventHandler processExitHandler = (_, _) => WaitForTelemetryTask(telemetryTask); + ConsoleCancelEventHandler cancelKeyPressHandler = (_, _) => WaitForTelemetryTask(telemetryTask); + + AppDomain.CurrentDomain.ProcessExit += processExitHandler; + Console.CancelKeyPress += cancelKeyPressHandler; + + _ = telemetryTask.ContinueWith(_ => + { + AppDomain.CurrentDomain.ProcessExit -= processExitHandler; + Console.CancelKeyPress -= cancelKeyPressHandler; + }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); + } + + private static void WaitForTelemetryTask(Task telemetryTask) + { + try + { + telemetryTask.Wait(TimeSpan.FromSeconds(10)); + } + catch + { + // ignored + } + } + private static async Task BuildAndSendActivityAsync( ActivityContext context, ITelemetryActivityEventBuilder telemetryActivityEventBuilder, @@ -130,4 +136,4 @@ public class TelemetryService : ITelemetryService, IScopedDependency //ignored } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/TelemetryApplicationLifetime_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/TelemetryApplicationLifetime_Tests.cs new file mode 100644 index 0000000000..7c2f5c8c8c --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/TelemetryApplicationLifetime_Tests.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Autofac; +using Autofac.Core; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; +using NSubstitute; +using Shouldly; +using Volo.Abp.Autofac; +using Volo.Abp.Internal.Telemetry; +using Volo.Abp.Internal.Telemetry.Activity; +using Volo.Abp.Internal.Telemetry.Activity.Contracts; +using Volo.Abp.Internal.Telemetry.Constants; +using Volo.Abp.Modularity; +using Volo.Abp.Testing; +using Xunit; + +namespace Volo.Abp.AspNetCore; + +public class TelemetryApplicationLifetime_Tests +{ + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Should_Release_Applications_And_Autofac_Containers_After_Telemetry_Completes(bool useIntegratedTest) + { + var references = new List(); + for (var i = 0; i < 10; i++) + { + references.Add(CreateAndDisposeApplication(useIntegratedTest)); + } + + var collected = SpinWait.SpinUntil(() => + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + return references.All(reference => !reference.Application.IsAlive && !reference.Container.IsAlive); + }, TimeSpan.FromSeconds(10)); + + collected.ShouldBeTrue("Disposed applications and their root Autofac containers must be collectible."); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static ApplicationReferences CreateAndDisposeApplication(bool useIntegratedTest) + { + if (useIntegratedTest) + { + using var test = new TelemetryIntegratedTest(); + return test.GetReferences(); + } + + using var application = AbpApplicationFactory.Create(options => options.UseAutofac()); + application.Initialize(); + try + { + return ObserveApplication(application, application.ServiceProvider); + } + finally + { + application.Shutdown(); + } + } + + private static ApplicationReferences ObserveApplication(IAbpApplication application, IServiceProvider serviceProvider) + { + serviceProvider.GetService().ShouldBeNull(); + serviceProvider.GetRequiredService().EnvironmentName.ShouldBe(Environments.Development); + serviceProvider.GetRequiredService().GetValue("Abp:Telemetry:IsEnabled").ShouldBeNull(); + + var completion = serviceProvider.GetRequiredService(); + completion.ScopeDisposed.Task.Wait(TimeSpan.FromSeconds(10)).ShouldBeTrue(); + completion.ActivityCount.ShouldBe(1); + completion.ActivityName.ShouldBe(ActivityNameConsts.ApplicationRun); + + var lifetimeScope = (ISharingLifetimeScope)serviceProvider.GetRequiredService(); + return new ApplicationReferences(new WeakReference(application), new WeakReference(lifetimeScope.RootLifetimeScope)); + } + + private sealed record ApplicationReferences(WeakReference Application, WeakReference Container); + + private sealed class TelemetryIntegratedTest : AbpIntegratedTest + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + + public ApplicationReferences GetReferences() + { + return ObserveApplication(Application, ServiceProvider); + } + } + + [DependsOn(typeof(AbpAspNetCoreModule), typeof(AbpAutofacModule))] + public class TelemetryTestModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.Replace(ServiceDescriptor.Singleton(new ConfigurationBuilder().Build())); + context.Services.AddSingleton(); + context.Services.Replace(ServiceDescriptor.Scoped()); + context.Services.Replace(ServiceDescriptor.Singleton(Substitute.For())); + context.Services.Replace(ServiceDescriptor.Singleton(Substitute.For())); + } + } + + public class TelemetryCompletion + { + public TaskCompletionSource ScopeDisposed { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + public int ActivityCount; + public string ActivityName { get; set; } + } + + public class TestActivityEventBuilder : ITelemetryActivityEventBuilder, IDisposable + { + private readonly TelemetryCompletion _completion; + + public TestActivityEventBuilder(TelemetryCompletion completion) + { + _completion = completion; + } + + public Task BuildAsync(ActivityContext context) + { + Interlocked.Increment(ref _completion.ActivityCount); + _completion.ActivityName = context.Current.Get(ActivityPropertyNames.ActivityName); + return Task.FromResult(null); + } + + public void Dispose() + { + _completion.ScopeDisposed.TrySetResult(true); + } + } +} diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetryService_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetryService_Tests.cs new file mode 100644 index 0000000000..a94fa7be5e --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Telemetry/TelemetryService_Tests.cs @@ -0,0 +1,182 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; +using Volo.Abp.Internal.Telemetry; +using Volo.Abp.Internal.Telemetry.Activity; +using Volo.Abp.Internal.Telemetry.Activity.Contracts; +using Xunit; + +namespace Volo.Abp.Telemetry; + +public class TelemetryService_Tests +{ + [Theory] + [InlineData(TaskStatus.RanToCompletion)] + [InlineData(TaskStatus.Faulted)] + [InlineData(TaskStatus.Canceled)] + public void Should_Unsubscribe_And_Release_Service_When_Activity_Completes(TaskStatus status) + { + using var started = new ManualResetEventSlim(); + using var release = new ManualResetEventSlim(); + var serviceProvider = Substitute.For(); + var builder = Substitute.For(); + builder.BuildAsync(Arg.Any()).Returns(Task.FromResult(null)); + serviceProvider.GetService(typeof(ITelemetryActivityEventBuilder)).Returns(builder); + serviceProvider.GetService(typeof(ITelemetryActivityStorage)).Returns(Substitute.For()); + serviceProvider.GetService(typeof(ITelemetryActivitySender)).Returns(Substitute.For()); + var scope = Substitute.For(); + scope.ServiceProvider.Returns(serviceProvider); + var scopeFactory = Substitute.For(); + scopeFactory.CreateScope().Returns(_ => + { + started.Set(); + if (!release.Wait(TimeSpan.FromSeconds(10))) + { + throw new TimeoutException(); + } + + if (status == TaskStatus.Faulted) + { + throw new InvalidOperationException(); + } + + if (status == TaskStatus.Canceled) + { + throw new OperationCanceledException(); + } + + return scope; + }); + + var service = StartActivity(scopeFactory); + try + { + started.Wait(TimeSpan.FromSeconds(10)).ShouldBeTrue(); + GetProcessExitHandlers().Length.ShouldBe(1); + GetCancelKeyPressHandlers().Length.ShouldBe(1); + } + finally + { + release.Set(); + } + + WaitForHandlersToBeRemoved(); + for (var i = 0; i < 3; i++) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + + service.IsAlive.ShouldBeFalse(); + } + + [Theory] + [InlineData(TaskStatus.RanToCompletion)] + [InlineData(TaskStatus.Faulted)] + [InlineData(TaskStatus.Canceled)] + public void Should_Not_Leave_Handlers_For_Already_Completed_Tasks(TaskStatus status) + { + var task = status switch + { + TaskStatus.Faulted => Task.FromException(new InvalidOperationException()), + TaskStatus.Canceled => Task.FromCanceled(new CancellationToken(true)), + _ => Task.CompletedTask + }; + + RegisterExitHandlers(task); + + GetProcessExitHandlers().ShouldBeEmpty(); + GetCancelKeyPressHandlers().ShouldBeEmpty(); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Should_Wait_For_Pending_Task_On_Exit(bool cancelKeyPress) + { + var completion = new TaskCompletionSource(); + var handlerStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + RegisterExitHandlers(completion.Task); + var handler = cancelKeyPress ? GetCancelKeyPressHandlers().Single() : GetProcessExitHandlers().Single(); + var handlerTask = Task.Run(() => + { + handlerStarted.SetResult(true); + if (handler is ConsoleCancelEventHandler consoleHandler) + { + consoleHandler(null, null!); + } + else + { + ((EventHandler)handler)(null, EventArgs.Empty); + } + }); + + try + { + await handlerStarted.Task.WaitAsync(TimeSpan.FromSeconds(10)); + await Task.Delay(100); + handlerTask.IsCompleted.ShouldBeFalse(); + } + finally + { + completion.TrySetResult(true); + } + + await handlerTask.WaitAsync(TimeSpan.FromSeconds(10)); + WaitForHandlersToBeRemoved(); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static WeakReference StartActivity(IServiceScopeFactory scopeFactory) + { + var service = new TelemetryService(scopeFactory); + service.AddActivityAsync("TestActivity").GetAwaiter().GetResult(); + return new WeakReference(service); + } + + private static void RegisterExitHandlers(Task task) + { + typeof(TelemetryService).GetMethod("WaitForTelemetryTaskOnExit", BindingFlags.NonPublic | BindingFlags.Static)! + .Invoke(null, new object[] { task }); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void WaitForHandlersToBeRemoved() + { + SpinWait.SpinUntil(() => GetProcessExitHandlers().Length == 0 && GetCancelKeyPressHandlers().Length == 0, + TimeSpan.FromSeconds(10)).ShouldBeTrue(); + } + + private static Delegate[] GetProcessExitHandlers() + { + return typeof(AppDomain).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) + .Where(field => typeof(Delegate).IsAssignableFrom(field.FieldType)) + .Select(field => field.GetValue(field.IsStatic ? null : AppDomain.CurrentDomain)) + .OfType() + .SelectMany(handler => handler.GetInvocationList()) + .Where(IsTelemetryHandler) + .ToArray(); + } + + private static Delegate[] GetCancelKeyPressHandlers() + { + return typeof(Console).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) + .Where(field => typeof(Delegate).IsAssignableFrom(field.FieldType)) + .Select(field => field.GetValue(null)) + .OfType() + .SelectMany(handler => handler.GetInvocationList()) + .Where(IsTelemetryHandler) + .ToArray(); + } + + private static bool IsTelemetryHandler(Delegate handler) + { + return handler.Target?.GetType().DeclaringType == typeof(TelemetryService); + } +}