mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
Commands and context-menu entries kick off async work as `_ = SomeAsync()`, discarding the task. When one faulted, the failure only surfaced via TaskScheduler.UnobservedTaskException after the GC finalized the dropped task -- late, on the finalizer thread, and disconnected from the gesture that caused it (a click would appear to do nothing, then a mysterious crash report arrived seconds later). Add a HandleExceptions() extension that observes such a task on the UI context and routes any fault to the existing GlobalExceptionHandler at the point of failure (treating cancellation as a normal outcome), and route the user-invoked command/entry sites through it. The global unhandled-exception net already existed; this just makes these failures prompt and attributable instead of deferred to GC. Assisted-by: Claude:claude-opus-4-8:Claude Codepull/3755/head
16 changed files with 76 additions and 21 deletions
@ -0,0 +1,55 @@ |
|||||||
|
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using ILSpy.AppEnv; |
||||||
|
|
||||||
|
namespace ILSpy |
||||||
|
{ |
||||||
|
public static class FireAndForgetExtensions |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Observes a fire-and-forget task started from a command or event handler. Awaits it on the
|
||||||
|
/// captured (UI) context and routes any failure to <see cref="GlobalExceptionHandler"/>
|
||||||
|
/// immediately, at the point of failure. Without this, a discarded faulted task
|
||||||
|
/// (<c>_ = SomethingAsync()</c>) only surfaces via
|
||||||
|
/// <see cref="TaskScheduler.UnobservedTaskException"/> after the GC finalizes it — late, on
|
||||||
|
/// the finalizer thread, and disconnected from the gesture that triggered it.
|
||||||
|
/// <see cref="OperationCanceledException"/> is treated as a normal cooperative cancel, not an
|
||||||
|
/// error.
|
||||||
|
/// </summary>
|
||||||
|
public static async void HandleExceptions(this Task task) |
||||||
|
{ |
||||||
|
ArgumentNullException.ThrowIfNull(task); |
||||||
|
try |
||||||
|
{ |
||||||
|
await task.ConfigureAwait(true); |
||||||
|
} |
||||||
|
catch (OperationCanceledException) |
||||||
|
{ |
||||||
|
// Cooperative cancellation is an expected outcome, not a failure.
|
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
GlobalExceptionHandler.Show(ex); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue