Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper method for awaiting task and ignoring cancellation #2565

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Aspire.Dashboard/Aspire.Dashboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<Compile Include="$(SharedDir)KnownResourceNames.cs" Link="Utils\KnownResourceNames.cs" />
<Compile Include="$(SharedDir)KnownFormats.cs" Link="Utils\KnownFormats.cs" />
<Compile Include="$(SharedDir)StringComparers.cs" Link="Utils\StringComparers.cs" />
<Compile Include="$(SharedDir)TaskHelpers.cs" Link="Utils\TaskHelpers.cs" />
<Compile Include="$(SharedDir)Model\KnownProperties.cs" Link="Utils\KnownProperties.cs" />
<Compile Include="$(SharedDir)Model\KnownResourceTypes.cs" Link="Utils\KnownResourceTypes.cs" />
<Compile Include="$(SharedDir)CircularBuffer.cs" Link="Otlp\Storage\CircularBuffer.cs" />
Expand Down
11 changes: 1 addition & 10 deletions src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,7 @@ public async ValueTask DisposeAsync()

await StopWatchingLogsAsync();

if (_resourceSubscriptionTask is { } resourceSubscriptionTask)
{
try
{
await resourceSubscriptionTask;
}
catch (OperationCanceledException)
{
}
}
await TaskHelpers.WaitIgnoreCancelAsync(_resourceSubscriptionTask);

if (_logViewer is { } logViewer)
{
Expand Down
17 changes: 2 additions & 15 deletions src/Aspire.Dashboard/Model/DashboardClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using Aspire.Dashboard.Utils;
using Aspire.V1;
using Grpc.Core;
using Grpc.Net.Client;
Expand Down Expand Up @@ -385,21 +386,7 @@ public async ValueTask DisposeAsync()

_channel?.Dispose();

try
{
if (_connection is { IsCanceled: false })
{
await _connection.ConfigureAwait(false);
}
}
catch (OperationCanceledException)
{
// Ignore cancellation
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error from connection task.");
}
await TaskHelpers.WaitIgnoreCancelAsync(_connection, _logger, "Unexpected error from connection task.").ConfigureAwait(false);
}
}
}
12 changes: 2 additions & 10 deletions src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Aspire.Dashboard.Otlp.Model;
using Aspire.Dashboard.Utils;

namespace Aspire.Dashboard.Model;

Expand Down Expand Up @@ -161,15 +162,6 @@ public async ValueTask DisposeAsync()
_watchContainersTokenSource.Cancel();
_watchContainersTokenSource.Dispose();

if (_watchTask is not null)
{
try
{
await _watchTask.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
await TaskHelpers.WaitIgnoreCancelAsync(_watchTask).ConfigureAwait(false);
}
}
1 change: 1 addition & 0 deletions src/Aspire.Hosting/Aspire.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Compile Include="$(SharedDir)KnownFormats.cs" Link="Utils\KnownFormats.cs" />
<Compile Include="$(SharedDir)CircularBuffer.cs" Link="CircularBuffer.cs" />
<Compile Include="$(SharedDir)ValueStopwatch\*.cs" LinkBase="Utils" />
<Compile Include="$(SharedDir)TaskHelpers.cs" Link="Utils\TaskHelpers.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 3 additions & 14 deletions src/Aspire.Hosting/Dcp/DcpHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO.Pipelines;
using System.Net.Sockets;
using System.Text;
using Aspire.Dashboard.Utils;
using Aspire.Hosting.Dcp.Process;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -83,20 +84,8 @@ public async Task StopAsync(CancellationToken cancellationToken = default)
}

_shutdownCts.Cancel();
if (_logProcessorTask is { } task)
{
try
{
await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in logging socket processor.");
}
}

await TaskHelpers.WaitIgnoreCancelAsync(_logProcessorTask, _logger, "Error in logging socket processor.").ConfigureAwait(false);
}

public async ValueTask DisposeAsync()
Expand Down
41 changes: 41 additions & 0 deletions src/Shared/TaskHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Logging;

namespace Aspire.Dashboard.Utils;

internal static class TaskHelpers
{
public static Task WaitIgnoreCancelAsync(Task? task)
{
return WaitIgnoreCancelCoreAsync(task, logger: null, logMessage: null);
}

public static Task WaitIgnoreCancelAsync(Task? task, ILogger logger, string logMessage)
{
return WaitIgnoreCancelCoreAsync(task, logger, logMessage);
}

private static async Task WaitIgnoreCancelCoreAsync(Task? task, ILogger? logger = null, string? logMessage = null)
{
if (task is null || task.IsCompletedSuccessfully || task.IsCanceled)
{
return;
}

try
{
await task.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Ignore errors from canceled tasks.
}
catch (Exception ex) when (logger is not null)
{
logger.LogError(ex, logMessage ?? "Unexpected error.");
throw;
}
}
}