Skip to content

Commit 5244303

Browse files
authored
Cleanup Dispose implementations (#6730)
1 parent cc3fae0 commit 5244303

File tree

15 files changed

+43
-113
lines changed

15 files changed

+43
-113
lines changed

src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ namespace Microsoft.Testing.Extensions.Diagnostics;
1818

1919
internal sealed class HangDumpActivityIndicator : IDataConsumer, ITestSessionLifetimeHandler,
2020
#if NETCOREAPP
21-
IAsyncDisposable
22-
#else
23-
IDisposable
21+
IAsyncDisposable,
2422
#endif
23+
IDisposable
2524
{
2625
private readonly ICommandLineOptions _commandLineOptions;
2726
private readonly IEnvironment _environment;
@@ -267,7 +266,7 @@ private async Task ExitSignalActivityIndicatorTaskAsync()
267266
#if NETCOREAPP
268267
public async ValueTask DisposeAsync()
269268
{
270-
await DisposeHelper.DisposeAsync(_namedPipeClient).ConfigureAwait(false);
269+
_namedPipeClient?.Dispose();
271270

272271
// If the OnTestSessionFinishingAsync is not called means that something unhandled happened
273272
// and we didn't correctly coordinate the shutdown with the HangDumpProcessLifetimeHandler.
@@ -277,12 +276,12 @@ public async ValueTask DisposeAsync()
277276
await DisposeHelper.DisposeAsync(_singleConnectionNamedPipeServer).ConfigureAwait(false);
278277
}
279278

280-
_pipeNameDescription?.Dispose();
281279
_mutexCreated.Dispose();
282280
_signalActivity.Dispose();
283281
_activityIndicatorMutex?.Dispose();
284282
}
285-
#else
283+
#endif
284+
286285
public void Dispose()
287286
{
288287
_namedPipeClient?.Dispose();
@@ -295,10 +294,8 @@ public void Dispose()
295294
_singleConnectionNamedPipeServer?.Dispose();
296295
}
297296

298-
_pipeNameDescription?.Dispose();
299297
_mutexCreated.Dispose();
300298
_signalActivity.Dispose();
301299
_activityIndicatorMutex?.Dispose();
302300
}
303-
#endif
304301
}

src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ namespace Microsoft.Testing.Extensions.Diagnostics;
2525

2626
internal sealed class HangDumpProcessLifetimeHandler : ITestHostProcessLifetimeHandler, IOutputDeviceDataProducer, IDataProducer,
2727
#if NETCOREAPP
28-
IAsyncDisposable
29-
#else
30-
IDisposable
28+
IAsyncDisposable,
3129
#endif
30+
IDisposable
3231
{
3332
private readonly IMessageBus _messageBus;
3433
private readonly OutputDeviceWriter _outputDisplay;
@@ -140,14 +139,7 @@ private async Task<IResponse> CallbackAsync(IRequest request)
140139
{
141140
await _logger.LogDebugAsync("Session end received by the test host").ConfigureAwait(false);
142141
_exitActivityIndicatorTask = true;
143-
#if NET
144-
if (_namedPipeClient is not null)
145-
{
146-
await _namedPipeClient.DisposeAsync().ConfigureAwait(false);
147-
}
148-
#else
149142
_namedPipeClient?.Dispose();
150-
#endif
151143
return VoidResponse.CachedInstance;
152144
}
153145
else if (request is ConsumerPipeNameRequest consumerPipeNameRequest)
@@ -519,7 +511,6 @@ public void Dispose()
519511
_waitConsumerPipeName.Dispose();
520512
_mutexNameReceived.Dispose();
521513
_singleConnectionNamedPipeServer?.Dispose();
522-
_pipeNameDescription.Dispose();
523514
}
524515

525516
#if NETCOREAPP
@@ -534,7 +525,6 @@ public async ValueTask DisposeAsync()
534525
_waitConsumerPipeName.Dispose();
535526
_mutexNameReceived.Dispose();
536527
_singleConnectionNamedPipeServer?.Dispose();
537-
_pipeNameDescription.Dispose();
538528
}
539529
#endif
540530
}

src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ public Task WaitForConnectionAsync(CancellationToken cancellationToken)
4545
=> _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellationToken);
4646

4747
public void Dispose()
48-
{
49-
_singleConnectionNamedPipeServer.Dispose();
50-
_pipeNameDescription.Dispose();
51-
}
48+
=> _singleConnectionNamedPipeServer.Dispose();
5249

5350
private Task<IResponse> CallbackAsync(IRequest request)
5451
{

src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@
1414

1515
namespace Microsoft.Testing.Extensions.Policy;
1616

17-
internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime,
18-
#if NETCOREAPP
19-
IAsyncDisposable
20-
#else
21-
IDisposable
22-
#endif
17+
internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime, IDisposable
2318
{
2419
private readonly IServiceProvider _serviceProvider;
2520
private readonly ICommandLineOptions _commandLineOptions;
@@ -73,15 +68,5 @@ public Task<bool> IsEnabledAsync()
7368
public Task AfterRunAsync(int exitCode, CancellationToken cancellation)
7469
=> Task.CompletedTask;
7570

76-
#if NETCOREAPP
77-
public async ValueTask DisposeAsync()
78-
{
79-
if (Client is not null)
80-
{
81-
await Client.DisposeAsync().ConfigureAwait(false);
82-
}
83-
}
84-
#else
8571
public void Dispose() => Client?.Dispose();
86-
#endif
8772
}

src/Platform/Microsoft.Testing.Extensions.Telemetry/AppInsightsProvider.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ namespace Microsoft.Testing.Extensions.Telemetry;
1818
/// Allows to log telemetry events via AppInsights.
1919
/// </summary>
2020
internal sealed partial class AppInsightsProvider :
21-
ITelemetryCollector
22-
#pragma warning disable SA1001 // Commas should be spaced correctly
21+
ITelemetryCollector,
2322
#if NETCOREAPP
24-
, IAsyncDisposable
25-
#else
26-
, IDisposable
23+
IAsyncDisposable,
2724
#endif
28-
#pragma warning restore SA1001 // Commas should be spaced correctly
25+
IDisposable
2926
{
3027
// Note: We're currently using the same environment variable as dotnet CLI.
3128
public static readonly string SessionIdEnvVar = "TESTINGPLATFORM_APPINSIGHTS_SESSIONID";
@@ -282,11 +279,14 @@ Task LogEventAsync(string eventName, IDictionary<string, object> paramsMap, Canc
282279
#endif
283280
}
284281

285-
#if !NETCOREAPP
286282
// Adding dispose on graceful shutdown per https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152#issuecomment-518742922
287283
public void Dispose()
288284
{
285+
#if NETCOREAPP
286+
_payloads.Writer.Complete();
287+
#else
289288
_payloads.CompleteAdding();
289+
#endif
290290
if (!_isDisposed)
291291
{
292292
if (_telemetryTask is null)
@@ -304,7 +304,6 @@ public void Dispose()
304304
_isDisposed = true;
305305
}
306306
}
307-
#endif
308307

309308
#if NETCOREAPP
310309
public async ValueTask DisposeAsync()

src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ internal sealed class TrxProcessLifetimeHandler :
2828
IDataProducer,
2929
IOutputDeviceDataProducer,
3030
#if NETCOREAPP
31-
IAsyncDisposable
32-
#else
33-
IDisposable
31+
IAsyncDisposable,
3432
#endif
33+
IDisposable
3534
{
3635
private readonly ICommandLineOptions _commandLineOptions;
3736
private readonly IEnvironment _environment;
@@ -231,21 +230,11 @@ private Task<IResponse> CallbackAsync(IRequest request)
231230

232231
#if NETCOREAPP
233232
public async ValueTask DisposeAsync()
234-
{
235-
await DisposeHelper.DisposeAsync(_singleConnectionNamedPipeServer).ConfigureAwait(false);
233+
=> await DisposeHelper.DisposeAsync(_singleConnectionNamedPipeServer).ConfigureAwait(false);
234+
#endif
236235

237-
// Dispose the pipe descriptor after the server to ensure the pipe is closed.
238-
_pipeNameDescription.Dispose();
239-
}
240-
#else
241236
public void Dispose()
242-
{
243-
_singleConnectionNamedPipeServer?.Dispose();
244-
245-
// Dispose the pipe descriptor after the server to ensure the pipe is closed.
246-
_pipeNameDescription.Dispose();
247-
}
248-
#endif
237+
=> _singleConnectionNamedPipeServer?.Dispose();
249238

250239
private sealed class ExtensionInfo : IExtension
251240
{

src/Platform/Microsoft.Testing.Platform/CommandLine/InformativeCommandLineTestHost.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
namespace Microsoft.Testing.Platform.CommandLine;
99

1010
internal sealed class InformativeCommandLineHost(int returnValue, IServiceProvider serviceProvider) : IHost, IDisposable
11-
#if NETCOREAPP
12-
#pragma warning disable SA1001 // Commas should be spaced correctly
13-
, IAsyncDisposable
14-
#pragma warning restore SA1001 // Commas should be spaced correctly
15-
#endif
1611
{
1712
private readonly int _returnValue = returnValue;
1813

@@ -21,14 +16,4 @@ internal sealed class InformativeCommandLineHost(int returnValue, IServiceProvid
2116
public Task<int> RunAsync() => Task.FromResult(_returnValue);
2217

2318
public void Dispose() => PushOnlyProtocol?.Dispose();
24-
25-
#if NETCOREAPP
26-
public async ValueTask DisposeAsync()
27-
{
28-
if (PushOnlyProtocol is not null)
29-
{
30-
await PushOnlyProtocol.DisposeAsync().ConfigureAwait(false);
31-
}
32-
}
33-
#endif
3419
}

src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControlledHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void Dispose()
4747
public async ValueTask DisposeAsync()
4848
{
4949
await DisposeHelper.DisposeAsync(_innerHost).ConfigureAwait(false);
50-
await _namedPipeClient.DisposeAsync().ConfigureAwait(false);
50+
_namedPipeClient.Dispose();
5151
}
5252
#endif
5353
}

src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ public void Dispose()
241241
}
242242

243243
#if NETCOREAPP
244+
[Obsolete("All owned fields are disposed synchronously. Introduction of DisposeAsync here is unnecessary complexity.")]
245+
// NOTE: While NamedPipeClient is internal API, it's breaking to change it as it's consumed via IVT by MTP extensions.
246+
// If we removed DisposeAsync in newer MTP version, but an old MTP extension is used with newer MTP version, we will get MissingMethodException.
247+
// It might be more safe to obsolete for now, and potentially remove after few versions are released when most users will
248+
// already be on those newer versions, and the risk of break is reduced.
244249
public async ValueTask DisposeAsync()
245250
{
246251
if (_disposed)

src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeServer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ public void Dispose()
320320
}
321321

322322
_namedPipeServerStream.Dispose();
323-
PipeName.Dispose();
324323

325324
_disposed = true;
326325
}
@@ -351,7 +350,6 @@ public async ValueTask DisposeAsync()
351350
}
352351

353352
_namedPipeServerStream.Dispose();
354-
PipeName.Dispose();
355353

356354
_disposed = true;
357355
}

0 commit comments

Comments
 (0)