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

Crash gracefully #12395

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions WalletWasabi.Fluent.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public static async Task<int> Main(string[] args)

var exitCode = await app.RunAsGuiAsync();

if (Services.TerminateService.GracefulCrashException is not null)
{
throw Services.TerminateService.GracefulCrashException;
}

if (exitCode == ExitCode.Ok && (Services.UpdateManager?.DoUpdateOnClose ?? false))
{
Services.UpdateManager.StartInstallingNewVersion();
Expand Down
14 changes: 13 additions & 1 deletion WalletWasabi/Services/Terminate/TerminateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public TerminateService(Func<Task> terminateApplicationAsync, Action terminateAp
_terminateApplicationAsync = terminateApplicationAsync;
_terminateApplication = terminateApplication;
IsSystemEventsSubscribed = false;
CancellationToken = TerminationCts.Token;
CancellationToken = TerminationCts.Token;
Instance = this;
}

public static TerminateService? Instance { get; private set; }
turbolay marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>Completion source that is completed once we receive a request to terminate the application in a graceful way.</summary>
/// <remarks>Currently, we handle CTRL+C this way. However, for example, an RPC command might use this API too.</remarks>
private TaskCompletionSource ForcefulTerminationRequested { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
Expand All @@ -42,6 +45,9 @@ public TerminateService(Func<Task> terminateApplicationAsync, Action terminateAp

private bool IsSystemEventsSubscribed { get; set; }

/// <summary>In case of an unrecoverable exception, SignalGracefulCrash will store here the exception to pass down to the CrashReporter.</summary>
public Exception? GracefulCrashException { get; private set; }

public void Activate()
{
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
Expand Down Expand Up @@ -103,6 +109,12 @@ private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e)
SignalForceTerminate();
}

public void SignalGracefulCrash(Exception ex)
{
GracefulCrashException = ex;
SignalForceTerminate();
}

public void SignalForceTerminate()
{
if (ForcefulTerminationRequested.TrySetResult())
Expand Down
2 changes: 2 additions & 0 deletions WalletWasabi/Wallets/WalletFilterProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using WalletWasabi.Extensions;
using WalletWasabi.Logging;
using WalletWasabi.Models;
using WalletWasabi.Services.Terminate;
using WalletWasabi.Stores;
using WalletWasabi.Wallets.FilterProcessor;

Expand Down Expand Up @@ -185,6 +186,7 @@
catch (Exception ex)
{
Logger.LogError(ex);
TerminateService.Instance?.SignalGracefulCrash(ex);

Check warning on line 189 in WalletWasabi/Wallets/WalletFilterProcessor.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (master)

❌ Getting worse: Complex Method

ExecuteAsync already has high cyclomatic complexity, and now it increases in Lines of Code from 87 to 88. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
throw;
}
finally
Expand Down