Skip to content

Commit

Permalink
Merge pull request #18204 from frenzibyte/move-soft-handle-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy authored May 10, 2022
2 parents 2e774fb + b2a57c3 commit 587cc8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
22 changes: 0 additions & 22 deletions osu.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System;
using System.IO;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using osu.Desktop.LegacyIpc;
using osu.Framework;
using osu.Framework.Development;
Expand Down Expand Up @@ -63,8 +61,6 @@ public static void Main(string[] args)

using (DesktopGameHost host = Host.GetSuitableDesktopHost(gameName, new HostOptions { BindIPC = true }))
{
host.ExceptionThrown += handleException;

if (!host.IsPrimaryInstance)
{
if (args.Length > 0 && args[0].Contains('.')) // easy way to check for a file import in args
Expand Down Expand Up @@ -131,23 +127,5 @@ private static void setupSquirrel()
// tools.SetProcessAppUserModelId();
});
}

private static int allowableExceptions = DebugUtils.IsDebugBuild ? 0 : 1;

/// <summary>
/// Allow a maximum of one unhandled exception, per second of execution.
/// </summary>
/// <param name="arg"></param>
private static bool handleException(Exception arg)
{
bool continueExecution = Interlocked.Decrement(ref allowableExceptions) >= 0;

Logger.Log($"Unhandled exception has been {(continueExecution ? $"allowed with {allowableExceptions} more allowable exceptions" : "denied")} .");

// restore the stock of allowable exceptions after a short delay.
Task.Delay(1000).ContinueWith(_ => Interlocked.Increment(ref allowableExceptions));

return continueExecution;
}
}
}
35 changes: 35 additions & 0 deletions osu.Game/OsuGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
Expand Down Expand Up @@ -180,9 +181,21 @@ public virtual string Version
/// </summary>
protected DatabaseContextFactory EFContextFactory { get; private set; }

/// <summary>
/// Number of unhandled exceptions to allow before aborting execution.
/// </summary>
/// <remarks>
/// When an unhandled exception is encountered, an internal count will be decremented.
/// If the count hits zero, the game will crash.
/// Each second, the count is incremented until reaching the value specified.
/// </remarks>
protected virtual int UnhandledExceptionsBeforeCrash => DebugUtils.IsDebugBuild ? 0 : 1;

public OsuGameBase()
{
Name = @"osu!";

allowableExceptions = UnhandledExceptionsBeforeCrash;
}

[BackgroundDependencyLoader]
Expand Down Expand Up @@ -408,6 +421,8 @@ public override void SetHost(GameHost host)
LocalConfig ??= UseDevelopmentServer
? new DevelopmentOsuConfigManager(Storage)
: new OsuConfigManager(Storage);

host.ExceptionThrown += onExceptionThrown;
}

/// <summary>
Expand Down Expand Up @@ -505,6 +520,23 @@ private void onRulesetChanged(ValueChangedEvent<RulesetInfo> r)
AvailableMods.Value = dict;
}

private int allowableExceptions;

/// <summary>
/// Allows a maximum of one unhandled exception, per second of execution.
/// </summary>
private bool onExceptionThrown(Exception _)
{
bool continueExecution = Interlocked.Decrement(ref allowableExceptions) >= 0;

Logger.Log($"Unhandled exception has been {(continueExecution ? $"allowed with {allowableExceptions} more allowable exceptions" : "denied")} .");

// restore the stock of allowable exceptions after a short delay.
Task.Delay(1000).ContinueWith(_ => Interlocked.Increment(ref allowableExceptions));

return continueExecution;
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Expand All @@ -514,6 +546,9 @@ protected override void Dispose(bool isDisposing)
LocalConfig?.Dispose();

realm?.Dispose();

if (Host != null)
Host.ExceptionThrown -= onExceptionThrown;
}
}
}

0 comments on commit 587cc8c

Please sign in to comment.