Avoid unobserved tasks for synchronous delegate exceptions - #9071
Avoid unobserved tasks for synchronous delegate exceptions#9071pablomartinezbernardo wants to merge 1 commit into
Conversation
4050bbf to
d0be9a3
Compare
BenchmarksBenchmark execution time: 2026-08-18 12:04:56 Comparing candidate commit d0be9a3 in PR branch Found 0 performance improvements and 2 performance regressions! Performance is the same for 70 metrics, 0 unstable metrics, 65 known flaky benchmarks, 61 flaky benchmarks without significant changes.
|
Summary of changes
Prevent delegate instrumentation from creating an orphaned faulted task when a Task-returning delegate throws synchronously.
Reason for change
Handler exceptions in AWS Lambda resurface as UnobservedTaskException after upgrading 3.45.0 -> 3.51.1
#9069
AWS Lambda executes customer handlers through a generated synchronous action. For async handlers, AWS calls GetAwaiter().GetResult(), which can throw synchronously when the customer’s task is faulted. AWS invokes this action before creating the outer task with Task.FromResult.
In this case, the wrapper propagates the synchronous exception, but the finally block also creates an asynchronous continuation with a null original task. The continuation rethrows the same exception, becoming a faulted task that cannot be returned because the original synchronous exception is already leaving the wrapper.
That task is never observed and eventually raises
TaskScheduler.UnobservedTaskExceptionduring garbage collection.The PR that likely triggered this error is the one adding support for
Amazon.Lambda.RuntimeSupport2.x #8797Implementation details
Only rethrow an exception from the continuation when the wrapped delegate actually returned a task.
Test coverage
Added a non-parallel regression test. The test fails against the previous implementation and passes with this change.