In a C# method, `_ = WriteAuditAsync();` discards a Task that later throws; what happens to that exception on .NET 10, and how should the call be fixed?
answer
- the exception lives in the task
- the discard silences a warning only
- finalizer, not the throw site
- give every task an owner
basics
~20 sThe exception is stored in the discarded task and nothing reports it. On .NET 10 the process keeps running; TaskScheduler.UnobservedTaskException fires only if the faulted task is later finalized. Await the call, or hand the task to an owner that observes it.
solid answer
~40 sAn async method captures every exception it throws, including one thrown before its first `await`, into the `Task` it returns; nothing is thrown at the call site. `_ = WriteAuditAsync();` throws that task away, and the discard only silences compiler warning CS4014, which a bare unawaited call raises. When the task faults, no code sees it. On .NET 10 the runtime does not crash the process: when the garbage collector finalizes the faulted task, it raises `TaskScheduler.UnobservedTaskException`, which may be much later or never. That event is a diagnostics hook, not error handling. Fix it by awaiting the call when its outcome matters; when it truly must run in the background, give the task an owner that attaches a fault-logging continuation and awaits outstanding work at shutdown.
code
csharp · 20 linesTaskScheduler.UnobservedTaskException += (_, e) =>
{
Console.WriteLine($"unobserved: {e.Exception.InnerException?.Message}");
e.SetObserved();
};
StartAudit(); // the returned Task is dropped
await Task.Delay(200); // the audit has faulted by now
GC.Collect();
GC.WaitForPendingFinalizers(); // the faulted task's finalizer raises the event
Console.WriteLine("process still running");
static void StartAudit() => _ = WriteAuditAsync();
static async Task WriteAuditAsync()
{
await Task.Delay(50);
throw new InvalidOperationException("audit store unavailable");
}go deeper
Recall that an un-awaited task hides its exception from the caller, and that the compiler's CS4014 warning is pointing at exactly that.
Explain how an async method stores every exception in its task, why the discard only silences CS4014, and when UnobservedTaskException is raised.
Trace a missing audit record or silent failure back to a dropped task, add a global handler for detection, and fix the call site by awaiting or tracking it.
Set a team rule for when background work may run untracked and what owning, logging and draining it at shutdown must look like.
## Where the exception goes When a C# `async` method throws, the compiler-generated code catches the exception and stores it in the method's `Task`, which moves to the `Faulted` state. This applies to every statement in the method body, **including code that runs before the first `await`**. Nothing is thrown to the caller at the call site; the caller learns about the failure only by awaiting the task or by reading its `Exception` property. So in ```csharp public void Save(Order order) { repository.Add(order); _ = WriteAuditAsync(order); // returned Task discarded } ``` an exception inside `WriteAuditAsync` ends up in a task object that no variable refers to. No catch block anywhere can see it. ## What the compiler says A bare call such as `WriteAuditAsync(order);` whose `Task` is ignored draws warning **CS4014**: "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call." Assigning to the discard `_` makes the intent explicit and removes the warning. It does **not** observe the task, catch its exception or wait for it; it only tells the compiler the omission is deliberate. ## What the runtime does The runtime keeps the stored exceptions in an internal holder with a finalizer. If a faulted task becomes unreachable without anyone observing its exception, then at some later garbage collection: 1. the holder's finalizer runs; 2. it wraps the exceptions in an `AggregateException`; 3. it raises the static event `TaskScheduler.UnobservedTaskException`, whose `UnobservedTaskExceptionEventArgs` lets a handler log it and call `SetObserved()`. On .NET 10 that is all. Microsoft's guide to task exception handling states that in modern .NET unobserved exceptions no longer crash the process by default: the runtime reports them through the event and continues. Older material that warns of the process being torn down describes an earlier escalation policy, and the doc comment on the event in the runtime source still uses that wording. Several properties make the event a poor safety net: - it fires on the **finalizer thread**, long after the request that caused it; - it fires only if a garbage collection actually collects the task, which may never happen before the process exits; - it carries no context about which operation or user was involved. Contrast `async void`: an exception escaping an `async void` method is rethrown on the captured context or the thread pool and normally does crash the process. The two failure modes are opposite: one silent, one fatal. ## Fixing the call | Situation | Fix | |---|---| | the caller needs to know it succeeded | make the caller `async` and `await WriteAuditAsync(order)` | | several independent calls | start them, then `await Task.WhenAll(...)` | | truly background, optional work | give the task an **owner**: a tracker that attaches a fault-logging continuation and awaits all outstanding tasks at shutdown | | long-running background loops | a hosting abstraction that owns their lifetime, covered with application hosting | Microsoft's guidance on keeping async methods alive lists the three risks of untracked fire-and-forget: the exception is never observed, the process can shut down before the work finishes, and the work can outlive the object or scope that should control it. It allows untracked fire-and-forget only when the work is optional, the failure is safe to ignore and the work finishes well within the process lifetime. ## Finding dropped tasks in review Dropped tasks rarely announce themselves, so look for their shapes: - `_ =` assignments of calls that return `Task` or `ValueTask`; - `#pragma warning disable CS4014` or a project-wide suppression of that warning; - `ContinueWith` chains whose resulting task is itself discarded; - background work started from constructors or property setters, where `await` is impossible. ## A diagnostic, not a strategy Subscribing to `TaskScheduler.UnobservedTaskException` early in startup and logging every occurrence is still worthwhile: it finds discarded tasks that slipped through review. It must not replace awaiting. Treat each log entry as a bug report against the code that dropped the task. ## Interview angle The answer interviewers want has three parts: - the exception is captured in the task and is invisible at the call site; - the discard silences CS4014 and nothing else; - on current .NET the process survives and the failure surfaces late, if at all, through `UnobservedTaskException`, so the fix is ownership: await it or track it.
- An async method validates its argument with `ArgumentNullException.ThrowIfNull` before its first `await`. Does the caller get the exception at the call site?No. In an `async` method even the code before the first `await` is inside the generated state machine, so the exception is stored in the returned task. A caller that discards the task never sees it. To throw eagerly, split the method into a non-async wrapper that validates and an async core that does the work.
- Is a global `TaskScheduler.UnobservedTaskException` handler enough to make fire-and-forget safe?No. It fires on the finalizer thread only when a garbage collection collects the faulted task, possibly long after the failure or never, and it has no request context. Use it to log and find dropped tasks, then fix each one by awaiting it or giving it an owner.
- An exception in a discarded Task crashes the process on .NET 10.
- Assigning the task to _ makes the compiler observe its exception.
- UnobservedTaskException fires as soon as the task faults.
- Code before the first await throws straight to the caller of an async method.
- Handling UnobservedTaskException is a sound replacement for awaiting.