Skip to content

[src] Clean/consolidate runtime initialization code between platforms.#26131

Merged
rolfbjarne merged 18 commits into
mainfrom
dev/rolf/synchronize-initialize
Jul 22, 2026
Merged

[src] Clean/consolidate runtime initialization code between platforms.#26131
rolfbjarne merged 18 commits into
mainfrom
dev/rolf/synchronize-initialize

Conversation

@rolfbjarne

@rolfbjarne rolfbjarne commented Jul 16, 2026

Copy link
Copy Markdown
Member

This is a significant code unification between macOS and the other platforms. Xamarin.Mac supported being loaded into a separate app, which meant additional supporting code. macOS for .NET does not support being loaded into a separate app (just like the other platforms), which means all the additional supporting code can be deleted, and we can share much more code between platforms.

  • Add Runtime.EnsureUIThread (), and call this method from [NS|UI]Application.EnsureUIThread. Also move supporting code to the Runtime class.
  • Remove NSApplication.EnsureInitialized - this was necessary to ensure xamarin_initialize was called when Xamarin.Mac was hosted inside another application (such as Visual Studio for Mac). .NET for macOS no longer supports being hosted inside another application, so this code can be removed.
  • Remove Runtime.RegisterAssemblies () - it's no longer needed because .NET for macOS doesn't support being hosted inside another app.
  • Don't set MONO_CFG_DIR anymore - this is old code, and shouldn't be needed now. If anybody does, then they can set this variable themselves.
  • Remove NSApplication.ResetHandle () - it's no longer needed because .NET for macOS doesn't support being hosted inside another app.
  • Add NSApplication.Initialize () - this mirrors UIApplication.Initialize ()
  • Remove NSApplication.InitDrawBridge () - this is not applicable for .NET for macOS (not fully removed until XAMCORE_5_0)
  • Remove NSApplication.Init () - this is not needed anymore in .NET for macOS (not fully removed until XAMCORE_5_0)
  • Make the various [NS|UI]Application.Main methods return 'int' in XAMCORE_5_0.
  • Misc other dead code fixes.
  • Misc xml documentation updates.

rolfbjarne and others added 2 commits July 16, 2026 15:23
* Add Runtime.EnsureUIThread (), and call this method from [NS|UI]Application.EnsureUIThread. Also move supporting code to the Runtime class.
* Remove NSApplication.EnsureInitialized - this was necessary to ensure `xamarin_initialize` was called when Xamarin.Mac was hosted inside another application (such as Visual Studio for Mac). .NET for macOS no longer supports being hosted inside another application, so this code can be removed.
* Remove Runtime.RegisterAssemblies () - it's no longer needed because .NET for macOS doesn't support being hosted inside another app.
* Don't set MONO_CFG_DIR anymore - this is *old* code, and shouldn't be needed now. If anybody does, then they can set this variable themselves.
* Remove NSApplication.ResetHandle () - it's no longer needed because .NET for macOS doesn't support being hosted inside another app.
* Add NSApplication.Initialize () - this mirrors UIApplication.Initialize ()
* Remove NSApplication.InitDrawBridge () - this is not applicable for .NET for macOS (not fully removed until XAMCORE_5_0)
* Remove NSApplication.Init () - this is not needed anymore in .NET for macOS (not fully removed until XAMCORE_5_0)
* Make the various [NS|UI]Application.Main methods return 'int' in XAMCORE_5_0.
* Misc other dead code fixes.
* Misc xml documentation updates.
@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.

rolfbjarne and others added 2 commits July 16, 2026 20:14
The consolidated Runtime.EnsureUIThread returned early when
CheckForIllegalCrossThreadCalls was true, which is exactly when the check
should run. This meant the UIKit/AppKit cross-thread access exceptions were
never thrown, breaking the monotouch-test DispatchTests on iOS/tvOS/Mac
Catalyst (they got a NullReferenceException instead of a
UIKitThreadAccessException).

Invert the condition so the check runs when CheckForIllegalCrossThreadCalls
is enabled.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The consolidation moved the AppKitSynchronizationContext setup out of
NSApplication.Init and into Runtime.InitializePlatform, which runs during
early native startup (xamarin_initialize).

Hosts and the test runner (MacRunner) start the main run loop by calling
NSApplication.Init followed by NSApplication.SharedApplication.Run,
*without* going through NSApplication.Main. On base, NSApplication.Init
established the synchronization context on that run-loop thread; after the
consolidation it no longer did, so the context was no longer guaranteed to
be current on the thread that actually runs the app.

This is the same pattern iOS already uses (UIApplication.Main calls
UIApplication.Initialize on the run-loop thread), and it fixes the
link-all macOS PrintPreview_NSGraphicsContextCurrentContext hang, where the
modal print operation could no longer be aborted via a continuation posted
to the (missing) AppKit synchronization context.

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

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR consolidates cross-platform runtime/UI-thread initialization by moving UI-thread enforcement logic into ObjCRuntime.Runtime, and by simplifying AppKit/UIKit initialization paths now that .NET for macOS is no longer hostable inside other applications.

Changes:

  • Centralized UI-thread enforcement in ObjCRuntime.Runtime.EnsureUIThread (), and routed UIApplication.EnsureUIThread / NSApplication.EnsureUIThread through it.
  • Introduced NSApplication.Initialize () (to mirror UIApplication.Initialize ()) and adjusted macOS runtime platform initialization to call it.
  • Updated UIApplication.Main / NSApplication.Main to return int under XAMCORE_5_0 and performed associated cleanup of older initialization code paths.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/UIKit/UIApplication.cs Routes EnsureUIThread to Runtime.EnsureUIThread and updates Main overloads to return int in XAMCORE_5_0.
src/ObjCRuntime/Runtime.mac.cs Calls NSApplication.Initialize () during platform initialization.
src/ObjCRuntime/Runtime.iOS.cs Adds Mac Catalyst product/assembly naming constants and keeps UIApplication.Initialize () as the platform initializer.
src/ObjCRuntime/Runtime.cs Stores main thread during runtime init and introduces centralized EnsureUIThread ().
src/AppKit/NSApplication.cs Adds Initialize (), simplifies Init/Main, and routes EnsureUIThread to Runtime.EnsureUIThread.

Comment thread src/AppKit/NSApplication.cs Outdated
Comment thread src/ObjCRuntime/Runtime.iOS.cs Outdated
Comment thread src/ObjCRuntime/Runtime.cs Outdated
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne marked this pull request as ready for review July 20, 2026 12:34
@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner July 20, 2026 12:34
dalexsoto
dalexsoto previously approved these changes Jul 20, 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.

@dalexsoto

Copy link
Copy Markdown
Member

/azp run xamarin-macios-sim-pr-tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@dalexsoto

Copy link
Copy Markdown
Member

/azp run xamarin-macios-sim-pr-tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@dalexsoto

Copy link
Copy Markdown
Member

/azp run xamarin-macios-sim-pr-tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne enabled auto-merge (squash) July 21, 2026 16:07
@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

✅ 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: 067c0f3660d7f7b37946067d327650bd298bf524 [PR build]

@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

🚀 [CI Build #067c0f3] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 203 tests passed 🎉

Tests counts

✅ assembly-processing: All 1 tests passed. [attempt 2] 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. [attempt 2] 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. [attempt 2] Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. [attempt 3] 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 (iOS): All 19 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 18 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ monotouch (macOS): All 19 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 19 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: 067c0f3660d7f7b37946067d327650bd298bf524 [PR build]

@rolfbjarne
rolfbjarne merged commit 71b9510 into main Jul 22, 2026
56 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/synchronize-initialize branch July 22, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants