From 3414d2db692dcae7893ee0675b70118b691b1ac2 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Sun, 12 Oct 2025 09:42:23 +0200 Subject: [PATCH] Cleanup Dispose implementations --- .../HangDumpActivityIndicator.cs | 13 +++++------- .../HangDumpProcessLifetimeHandler.cs | 14 ++----------- .../RetryFailedTestsPipeServer.cs | 5 +---- .../RetryLifecycleCallbacks.cs | 17 +-------------- .../AppInsightsProvider.cs | 15 +++++++------ .../TrxProcessLifetimeHandler.cs | 21 +++++-------------- .../InformativeCommandLineTestHost.cs | 15 ------------- .../Hosts/TestHostControlledHost.cs | 2 +- .../IPC/NamedPipeClient.cs | 5 +++++ .../IPC/NamedPipeServer.cs | 2 -- .../IPC/PipeNameDescription.cs | 1 + .../Logging/LoggerFactory.cs | 13 +++++++++--- .../DotnetTest/DotnetTestConnection.cs | 17 +-------------- .../ServerMode/IPushOnlyProtocol.cs | 6 +----- .../IPC/IPCTests.cs | 10 +++------ 15 files changed, 43 insertions(+), 113 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs index 46d2d28515e..636e6221cdd 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs @@ -18,10 +18,9 @@ namespace Microsoft.Testing.Extensions.Diagnostics; internal sealed class HangDumpActivityIndicator : IDataConsumer, ITestSessionLifetimeHandler, #if NETCOREAPP - IAsyncDisposable -#else - IDisposable + IAsyncDisposable, #endif + IDisposable { private readonly ICommandLineOptions _commandLineOptions; private readonly IEnvironment _environment; @@ -267,7 +266,7 @@ private async Task ExitSignalActivityIndicatorTaskAsync() #if NETCOREAPP public async ValueTask DisposeAsync() { - await DisposeHelper.DisposeAsync(_namedPipeClient).ConfigureAwait(false); + _namedPipeClient?.Dispose(); // If the OnTestSessionFinishingAsync is not called means that something unhandled happened // and we didn't correctly coordinate the shutdown with the HangDumpProcessLifetimeHandler. @@ -277,12 +276,12 @@ public async ValueTask DisposeAsync() await DisposeHelper.DisposeAsync(_singleConnectionNamedPipeServer).ConfigureAwait(false); } - _pipeNameDescription?.Dispose(); _mutexCreated.Dispose(); _signalActivity.Dispose(); _activityIndicatorMutex?.Dispose(); } -#else +#endif + public void Dispose() { _namedPipeClient?.Dispose(); @@ -295,10 +294,8 @@ public void Dispose() _singleConnectionNamedPipeServer?.Dispose(); } - _pipeNameDescription?.Dispose(); _mutexCreated.Dispose(); _signalActivity.Dispose(); _activityIndicatorMutex?.Dispose(); } -#endif } diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs index 5bdbf19f672..7059a180bbd 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs @@ -25,10 +25,9 @@ namespace Microsoft.Testing.Extensions.Diagnostics; internal sealed class HangDumpProcessLifetimeHandler : ITestHostProcessLifetimeHandler, IOutputDeviceDataProducer, IDataProducer, #if NETCOREAPP - IAsyncDisposable -#else - IDisposable + IAsyncDisposable, #endif + IDisposable { private readonly IMessageBus _messageBus; private readonly OutputDeviceWriter _outputDisplay; @@ -140,14 +139,7 @@ private async Task CallbackAsync(IRequest request) { await _logger.LogDebugAsync("Session end received by the test host").ConfigureAwait(false); _exitActivityIndicatorTask = true; -#if NET - if (_namedPipeClient is not null) - { - await _namedPipeClient.DisposeAsync().ConfigureAwait(false); - } -#else _namedPipeClient?.Dispose(); -#endif return VoidResponse.CachedInstance; } else if (request is ConsumerPipeNameRequest consumerPipeNameRequest) @@ -519,7 +511,6 @@ public void Dispose() _waitConsumerPipeName.Dispose(); _mutexNameReceived.Dispose(); _singleConnectionNamedPipeServer?.Dispose(); - _pipeNameDescription.Dispose(); } #if NETCOREAPP @@ -534,7 +525,6 @@ public async ValueTask DisposeAsync() _waitConsumerPipeName.Dispose(); _mutexNameReceived.Dispose(); _singleConnectionNamedPipeServer?.Dispose(); - _pipeNameDescription.Dispose(); } #endif } diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs index 0702dba5439..27679225f8b 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs @@ -45,10 +45,7 @@ public Task WaitForConnectionAsync(CancellationToken cancellationToken) => _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellationToken); public void Dispose() - { - _singleConnectionNamedPipeServer.Dispose(); - _pipeNameDescription.Dispose(); - } + => _singleConnectionNamedPipeServer.Dispose(); private Task CallbackAsync(IRequest request) { diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs index 0a7fcd004ed..57669e91b7d 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs @@ -14,12 +14,7 @@ namespace Microsoft.Testing.Extensions.Policy; -internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime, -#if NETCOREAPP - IAsyncDisposable -#else - IDisposable -#endif +internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime, IDisposable { private readonly IServiceProvider _serviceProvider; private readonly ICommandLineOptions _commandLineOptions; @@ -73,15 +68,5 @@ public Task IsEnabledAsync() public Task AfterRunAsync(int exitCode, CancellationToken cancellation) => Task.CompletedTask; -#if NETCOREAPP - public async ValueTask DisposeAsync() - { - if (Client is not null) - { - await Client.DisposeAsync().ConfigureAwait(false); - } - } -#else public void Dispose() => Client?.Dispose(); -#endif } diff --git a/src/Platform/Microsoft.Testing.Extensions.Telemetry/AppInsightsProvider.cs b/src/Platform/Microsoft.Testing.Extensions.Telemetry/AppInsightsProvider.cs index f29f3f7949c..eafc424bfef 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Telemetry/AppInsightsProvider.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Telemetry/AppInsightsProvider.cs @@ -18,14 +18,11 @@ namespace Microsoft.Testing.Extensions.Telemetry; /// Allows to log telemetry events via AppInsights. /// internal sealed partial class AppInsightsProvider : - ITelemetryCollector -#pragma warning disable SA1001 // Commas should be spaced correctly + ITelemetryCollector, #if NETCOREAPP - , IAsyncDisposable -#else - , IDisposable + IAsyncDisposable, #endif -#pragma warning restore SA1001 // Commas should be spaced correctly + IDisposable { // Note: We're currently using the same environment variable as dotnet CLI. public static readonly string SessionIdEnvVar = "TESTINGPLATFORM_APPINSIGHTS_SESSIONID"; @@ -282,11 +279,14 @@ Task LogEventAsync(string eventName, IDictionary paramsMap, Canc #endif } -#if !NETCOREAPP // Adding dispose on graceful shutdown per https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152#issuecomment-518742922 public void Dispose() { +#if NETCOREAPP + _payloads.Writer.Complete(); +#else _payloads.CompleteAdding(); +#endif if (!_isDisposed) { if (_telemetryTask is null) @@ -304,7 +304,6 @@ public void Dispose() _isDisposed = true; } } -#endif #if NETCOREAPP public async ValueTask DisposeAsync() diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs index c035ef36f49..c7171d02c27 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs @@ -28,10 +28,9 @@ internal sealed class TrxProcessLifetimeHandler : IDataProducer, IOutputDeviceDataProducer, #if NETCOREAPP - IAsyncDisposable -#else - IDisposable + IAsyncDisposable, #endif + IDisposable { private readonly ICommandLineOptions _commandLineOptions; private readonly IEnvironment _environment; @@ -231,21 +230,11 @@ private Task CallbackAsync(IRequest request) #if NETCOREAPP public async ValueTask DisposeAsync() - { - await DisposeHelper.DisposeAsync(_singleConnectionNamedPipeServer).ConfigureAwait(false); + => await DisposeHelper.DisposeAsync(_singleConnectionNamedPipeServer).ConfigureAwait(false); +#endif - // Dispose the pipe descriptor after the server to ensure the pipe is closed. - _pipeNameDescription.Dispose(); - } -#else public void Dispose() - { - _singleConnectionNamedPipeServer?.Dispose(); - - // Dispose the pipe descriptor after the server to ensure the pipe is closed. - _pipeNameDescription.Dispose(); - } -#endif + => _singleConnectionNamedPipeServer?.Dispose(); private sealed class ExtensionInfo : IExtension { diff --git a/src/Platform/Microsoft.Testing.Platform/CommandLine/InformativeCommandLineTestHost.cs b/src/Platform/Microsoft.Testing.Platform/CommandLine/InformativeCommandLineTestHost.cs index f832a1aa03d..be794d71334 100644 --- a/src/Platform/Microsoft.Testing.Platform/CommandLine/InformativeCommandLineTestHost.cs +++ b/src/Platform/Microsoft.Testing.Platform/CommandLine/InformativeCommandLineTestHost.cs @@ -8,11 +8,6 @@ namespace Microsoft.Testing.Platform.CommandLine; internal sealed class InformativeCommandLineHost(int returnValue, IServiceProvider serviceProvider) : IHost, IDisposable -#if NETCOREAPP -#pragma warning disable SA1001 // Commas should be spaced correctly - , IAsyncDisposable -#pragma warning restore SA1001 // Commas should be spaced correctly -#endif { private readonly int _returnValue = returnValue; @@ -21,14 +16,4 @@ internal sealed class InformativeCommandLineHost(int returnValue, IServiceProvid public Task RunAsync() => Task.FromResult(_returnValue); public void Dispose() => PushOnlyProtocol?.Dispose(); - -#if NETCOREAPP - public async ValueTask DisposeAsync() - { - if (PushOnlyProtocol is not null) - { - await PushOnlyProtocol.DisposeAsync().ConfigureAwait(false); - } - } -#endif } diff --git a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControlledHost.cs b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControlledHost.cs index e3ba508abfa..27dd2dc7616 100644 --- a/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControlledHost.cs +++ b/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControlledHost.cs @@ -47,7 +47,7 @@ public void Dispose() public async ValueTask DisposeAsync() { await DisposeHelper.DisposeAsync(_innerHost).ConfigureAwait(false); - await _namedPipeClient.DisposeAsync().ConfigureAwait(false); + _namedPipeClient.Dispose(); } #endif } diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeClient.cs b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeClient.cs index 63c04d39190..9b34c17d668 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeClient.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeClient.cs @@ -239,6 +239,11 @@ public void Dispose() } #if NETCOREAPP + [Obsolete("All owned fields are disposed synchronously. Introduction of DisposeAsync here is unnecessary complexity.")] + // NOTE: While NamedPipeClient is internal API, it's breaking to change it as it's consumed via IVT by MTP extensions. + // If we removed DisposeAsync in newer MTP version, but an old MTP extension is used with newer MTP version, we will get MissingMethodException. + // It might be more safe to obsolete for now, and potentially remove after few versions are released when most users will + // already be on those newer versions, and the risk of break is reduced. public async ValueTask DisposeAsync() { if (_disposed) diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs index f9ba102c10a..686e4d3db99 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs @@ -325,7 +325,6 @@ public void Dispose() } _namedPipeServerStream.Dispose(); - PipeName.Dispose(); _disposed = true; } @@ -356,7 +355,6 @@ public async ValueTask DisposeAsync() } _namedPipeServerStream.Dispose(); - PipeName.Dispose(); _disposed = true; } diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/PipeNameDescription.cs b/src/Platform/Microsoft.Testing.Platform/IPC/PipeNameDescription.cs index eebb652d3e2..c481e12b2f5 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/PipeNameDescription.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/PipeNameDescription.cs @@ -9,6 +9,7 @@ internal sealed class PipeNameDescription(string name) : IDisposable // This is available via IVT. // Avoid removing it as it can be seen as a binary breaking change when users use newer version of core MTP but older version of one of the extensions. + [Obsolete("No-op.")] public void Dispose() { } diff --git a/src/Platform/Microsoft.Testing.Platform/Logging/LoggerFactory.cs b/src/Platform/Microsoft.Testing.Platform/Logging/LoggerFactory.cs index c431ec49211..281b4dd85d4 100644 --- a/src/Platform/Microsoft.Testing.Platform/Logging/LoggerFactory.cs +++ b/src/Platform/Microsoft.Testing.Platform/Logging/LoggerFactory.cs @@ -61,15 +61,22 @@ public void Dispose() #if NETCOREAPP public async ValueTask DisposeAsync() { - foreach (IAsyncDisposable asyncDisposable in _loggerProviders.OfType()) + foreach (ILoggerProvider loggerProvider in _loggerProviders) { // FileLoggerProvider is special and needs to be disposed manually. - if (asyncDisposable is FileLoggerProvider) + if (loggerProvider is FileLoggerProvider) { continue; } - await asyncDisposable.DisposeAsync().ConfigureAwait(false); + if (loggerProvider is IAsyncDisposable asyncDisposable) + { + await asyncDisposable.DisposeAsync().ConfigureAwait(false); + } + else if (loggerProvider is IDisposable disposable) + { + disposable.Dispose(); + } } } #endif diff --git a/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/DotnetTestConnection.cs b/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/DotnetTestConnection.cs index dc9cc2902be..d71ee14538d 100644 --- a/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/DotnetTestConnection.cs +++ b/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/DotnetTestConnection.cs @@ -13,11 +13,7 @@ namespace Microsoft.Testing.Platform; -internal sealed class DotnetTestConnection : IPushOnlyProtocol, -#if NETCOREAPP - IAsyncDisposable, -#endif - IDisposable +internal sealed class DotnetTestConnection : IPushOnlyProtocol, IDisposable { private readonly CommandLineHandler _commandLineHandler; private readonly IEnvironment _environment; @@ -147,15 +143,4 @@ public async Task SendMessageAsync(IRequest message) public Task OnExitAsync() => Task.CompletedTask; public void Dispose() => _dotnetTestPipeClient?.Dispose(); - -#if NETCOREAPP - public async ValueTask DisposeAsync() - { - if (_dotnetTestPipeClient is not null) - { - await _dotnetTestPipeClient.DisposeAsync().ConfigureAwait(false); - } - } -#endif - } diff --git a/src/Platform/Microsoft.Testing.Platform/ServerMode/IPushOnlyProtocol.cs b/src/Platform/Microsoft.Testing.Platform/ServerMode/IPushOnlyProtocol.cs index 2d6f492e3bb..33a6c79bdfd 100644 --- a/src/Platform/Microsoft.Testing.Platform/ServerMode/IPushOnlyProtocol.cs +++ b/src/Platform/Microsoft.Testing.Platform/ServerMode/IPushOnlyProtocol.cs @@ -3,11 +3,7 @@ namespace Microsoft.Testing.Platform.ServerMode; -internal interface IPushOnlyProtocol : -#if NETCOREAPP - IAsyncDisposable, -#endif - IDisposable +internal interface IPushOnlyProtocol : IDisposable { bool IsServerMode { get; } diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs index aecd53925c6..bcd8df1ced6 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs @@ -65,23 +65,21 @@ public async Task SingleConnectionNamedPipeServer_MultipleConnection_Fails() await waitTask; #if NETCOREAPP - await namedPipeClient1.DisposeAsync(); + namedPipeClient1.Dispose(); await openedPipe[0].DisposeAsync(); #else namedPipeClient1.Dispose(); openedPipe[0].Dispose(); #endif - pipeNameDescription.Dispose(); // Verify double dispose #if NETCOREAPP - await namedPipeClient1.DisposeAsync(); + namedPipeClient1.Dispose(); await openedPipe[0].DisposeAsync(); #else namedPipeClient1.Dispose(); openedPipe[0].Dispose(); #endif - pipeNameDescription.Dispose(); } [TestMethod] @@ -159,14 +157,12 @@ public async Task SingleConnectionNamedPipeServer_RequestReplySerialization_Succ } #if NETCOREAPP - await namedPipeClient.DisposeAsync(); + namedPipeClient.Dispose(); await singleConnectionNamedPipeServer.DisposeAsync(); #else namedPipeClient.Dispose(); singleConnectionNamedPipeServer.Dispose(); #endif - - pipeNameDescription.Dispose(); } [TestMethod]