Skip to content

[DO NOT MERGE] Instrument __MonoMac_NSAsyncActionDispatcher for issue #25861#25919

Draft
rolfbjarne wants to merge 7 commits into
mainfrom
dev/rolf/issue-25861-instrumentation
Draft

[DO NOT MERGE] Instrument __MonoMac_NSAsyncActionDispatcher for issue #25861#25919
rolfbjarne wants to merge 7 commits into
mainfrom
dev/rolf/issue-25861-instrumentation

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Throwaway diagnostic instrumentation to help reproduce and diagnose the GC/marshalling race in the flaky NSAsyncActionDispatcher failure.

The goal is to catch the failure on CI and learn:

  • Exactly where each __MonoMac_NSAsyncActionDispatcher is instantiated (creation stack trace), and whether it's always the same call site.
  • If/when the instance was disposed / garbage collected before the native callback ran.

How it works

  • New src/Foundation/NSAsyncDispatcherInstrumentation.cs: stores diagnostic data directly on the native Objective-C object via an associated reference (objc_setAssociatedObject), so it survives even after the managed instance is garbage collected.
  • NSAsyncDispatcher (in NSAction.cs) records the creation stack trace in its constructor, and logs lifecycle events (Apply, Dispose, finalize) as they happen.
  • MissingCtor (in Runtime.cs) reads this info from the still-alive native object and appends it to the marshalling exception message.

⚠️ This is throwaway instrumentation and must not be merged. It is only meant to gather data from CI.

Ref: #25861

🤖 Pull request created by Copilot

…25861

Add throwaway instrumentation to diagnose the GC/marshalling race in
#25861.

For every async dispatcher, record the creation stack trace and a log of
lifecycle events (Apply/Dispose/Finalize). This is stored directly on the
native Objective-C object via an associated reference, so it survives even
after the managed instance is garbage collected. When the marshalling
exception from the issue is raised (MissingCtor), this information is
appended to the error message.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne added the do-not-merge Do not merge this pull request label Jul 3, 2026
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

… objc associated objects

The previous approach stored the diagnostic info on the native object via
objc_setAssociatedObject with OBJC_ASSOCIATION_RETAIN. Reading it back with
objc_getAssociatedObject deterministically segfaulted in objc_msgSend_uncached
on the normal Apply path, crashing every test that dispatches an async action.

Replace the objc-associated-object mechanism with a static managed dictionary
keyed by the native handle (IntPtr). This still survives GC of the managed
dispatcher instance (the key is the native pointer, the value is a string, so
it does not keep the managed object alive), but performs no Objective-C calls
and therefore cannot crash.

Verified: tests/linker "dont link" MacCatalyst now passes (9/9) instead of
segfaulting at startup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #f227cd0] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: f227cd05fa355bb85d3a37587d07825e622f2251 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: f227cd05fa355bb85d3a37587d07825e622f2251 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #f227cd0] Build passed (Build packages) ✅

Pipeline on Agent
Hash: f227cd05fa355bb85d3a37587d07825e622f2251 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #f227cd0] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: f227cd05fa355bb85d3a37587d07825e622f2251 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #f227cd0] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 5 tests failed, 194 tests passed.

Failures

❌ monotouch tests (iOS)

1 tests failed, 17 tests passed.

Failed tests

  • monotouch-test/iOS - simulator/Release (compat inline Class.GetHandle): Failed

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

4 tests failed, 14 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Release (link sdk): Failed
  • monotouch-test/tvOS - simulator/Debug (LinkSdk): Failed
  • monotouch-test/tvOS - simulator/Debug (managed static registrar): Failed
  • monotouch-test/tvOS - simulator/Release (compat inline dlfcn): Failed

Html Report (VSDrops) Download

Successes

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 17 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 18 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: f227cd05fa355bb85d3a37587d07825e622f2251 [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot do-not-merge Do not merge this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants