Repository navigation
Trim NativeAOT exception dispatch until a handler is added - #137331
Merged
MichalStrehovsky merged 7 commits intoNov 12, 2026
Merged
MichalStrehovsky merged 7 commits into
MichalStrehovsky merged 7 commits into
Conversation
Register managed exception callbacks from the existing AppDomain event add accessors. Keep the rooted runtime exports independent of AppContext's static initializer by storing their callback addresses in a cctor-free holder. Preserve unconditional crash-info capture. The event add accessor's Interlocked.CompareExchange provides the publication fence; callback dispatch retains acquire reads. The original datastore field and its readers remain unchanged. Standalone local HelloWorldSize probe, WASI NativeAOT Release, invariant globalization and size optimization: 722,933 -> 719,421 bytes, saving 3,512 bytes (0.49%) against the common base. Validated with clr.nativeaotlibs+libs.sfx and the HelloWorldSize build. HelloWorldSize and the existing Exceptions smoke test both exited 100. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
This was referenced Nov 11, 2026
Member
Author
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The dispatcher change skips crash-info capture on NativeAOT’s direct unhandled-exception path, causing a handler’s Environment.FailFast(message) call to omit the supplied exception from the crash record.
1 open issue
What changed in this PR
This PR makes NativeAOT’s managed exception-event dispatch trimmable when no handlers are registered, reducing application size while keeping runtime exports available.
Changes:
- Register dispatch callbacks through the existing
AppDomainevent add accessors. - Store callback pointers separately to avoid triggering
AppContextinitialization. - Move unconditional crash-info capture into the NativeAOT unhandled-exception export.
| File | Description |
|---|---|
src/libraries/System.Private.CoreLib/src/System/AppDomain.cs |
Registers NativeAOT callbacks when exception handlers are added. |
src/libraries/System.Private.CoreLib/src/System/AppContext.cs |
Removes NativeAOT export and crash-capture responsibilities from shared dispatch. |
src/coreclr/nativeaot/System.Private.CoreLib/src/System/AppContext.NativeAot.cs |
Adds isolated callback storage and conditional dispatch through runtime exports. |
Review effort: Balanced
On NativeAOT, keep OnUnhandledException as the capturing entry point used by both the runtime export and ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent. Rename only the subscriber dispatcher to OnUnhandledExceptionInternal and register that method as the callback. This preserves the original exception if a handler calls Environment.FailFast(message), without duplicating serialization or evaluating Exception.Message again on the normal runtime path. The public API caller and the other runtimes remain unchanged. Use !MONO for the shared first-chance registration condition. Validated with actual Wasmtime crash dumps: the API path lost the exception before the fix; both API raise and runtime throw retain its type and message afterward. The four existing RaiseEvent tests and Exceptions smoke test pass. WASI NativeAOT Release builds are clean and HelloWorldSize remains 719,421 bytes, preserving the 3,512-byte standalone saving. Addresses the review finding in dotnet#137331. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
Rename the subscriber-only dispatcher and its registered callback target to OnUnhandledExceptionCore, matching OnFirstChanceExceptionCore. Keep the capturing OnUnhandledException entry point and the CoreCLR/Mono method names unchanged. Validated with a WASI Release NativeAOT CoreLib build, all four existing RaiseEvent cases, and the Exceptions smoke test. Both test applications exited 100. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
jkotas
reviewed
Nov 11, 2026
jkotas
reviewed
Nov 11, 2026
This was referenced Nov 11, 2026
Replace the platform conditionals in AppDomain's event adders with optional partial hooks. Provide the first-chance implementation for CoreCLR and both implementations for NativeAOT; unsupported hooks are erased without adding Mono no-op implementations. Store NativeAOT callbacks as volatile managed function pointers. This makes release/acquire semantics explicit, removes integer casts and Volatile.Read calls, and avoids relying on the event adder's fence to describe callback publication. Validated WASI Release CoreLib builds for CoreCLR and NativeAOT, the four existing RaiseEvent cases, the Exceptions smoke test, and HelloWorldSize. All native test applications exited 100; HelloWorldSize remains 719,421 bytes and the AppContext initializer/dispatcher remain trimmed. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2e62c26c-6da8-442d-98ac-dcb988c2846a
jkotas
reviewed
Nov 12, 2026
jkotas
reviewed
Nov 12, 2026
Make AppDomain exception events simple forwarders and keep registration in AppContext. Use private backing events to preserve thread-safe subscription, update Mono VM field lookups, and remove redundant AppDomain partial files. Validated CoreCLR and NativeAOT CoreLib builds, the Mono runtime and libraries build, 73 passing Mono AppDomain tests (one skipped), and a passing process-level exception-event probe. CoreCLR and NativeAOT execution tests were not run. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2df08005-5848-4ba4-b1b5-e707ab480d0f
Select AppDomain.CurrentDomain in the shared first-chance exception dispatcher so NativeAOT and CoreCLR deliver the same sender. Remove the now-redundant sender argument from their entry points. Test validation deferred to CI as requested. Test changes are not included. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2df08005-5848-4ba4-b1b5-e707ab480d0f
MichalStrehovsky
requested review from
steveisok,
thaystg and
vitek-karas
as code owners
November 12, 2026 03:22
jkotas
approved these changes
Nov 12, 2026
Replace the first-chance exception adapter with a static local Callback and make the NativeAOT unhandled exception dispatcher private. Validated the Checked NativeAOT CoreLib build and the Exceptions smoke test (exit 100). Existing local test edits are not included. Co-authored-by: Copilot <[email protected]> Copilot-Session: 2df08005-5848-4ba4-b1b5-e707ab480d0f
jkotas
approved these changes
Nov 12, 2026
Member
Author
|
/ba-g helix crash in an unrelated test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Register managed exception callbacks from the existing AppDomain event add accessors. Keep the rooted runtime exports independent of AppContext's static initializer by storing their callback addresses in a cctor-free holder. Preserve unconditional crash-info capture.
The event add accessor's Interlocked.CompareExchange provides the publication fence; callback dispatch retains acquire reads. The original datastore field and its readers remain unchanged.
Standalone local HelloWorldSize probe, WASI NativeAOT Release, invariant globalization and size optimization: 722,933 -> 719,421 bytes, saving 3,512 bytes (0.49%) against the common base.
Validated with clr.nativeaotlibs+libs.sfx and the HelloWorldSize build. HelloWorldSize and the existing Exceptions smoke test both exited 100.