Trim STAThreadAttribute in single-threaded (browser/wasi) Mono apps#130603
Trim STAThreadAttribute in single-threaded (browser/wasi) Mono apps#130603pavelsavara with Copilot wants to merge 4 commits into
Conversation
Co-authored-by: pavelsavara <271576+pavelsavara@users.noreply.github.com>
|
How are we ending up needing these attribute in the first place? When I make a new console app (on Windows even!), and PublishTrimmed the app, this attribute trims just fine. Does Blazor place the attribute on Main for some reason? Or roots some other code that needs it? I think this is the core part that should be addressed. |
Great question, thanks
@copilot removing root cause is better alternative. Investigate if this type rooting is still necessary on other Mono/mobile platforms. If not, remove it from If it's still need, make it only explicitly used for those (MT) Mono platforms. |
…tributes change Co-authored-by: pavelsavara <271576+pavelsavara@users.noreply.github.com>
Good catch — the attribute trims fine on CoreCLR because nothing roots it. On Mono it's the descriptor that keeps it alive: Note This comment was generated by GitHub Copilot.
Done in the latest commit. I confirmed the root is still needed on multithreaded Mono platforms (desktop/iOS/Android launch the entry point via So rather than removing the root outright, I gated it in Note This comment was generated by GitHub Copilot. |
|
/ba-g CI failure is #130618 |
There was a problem hiding this comment.
Pull request overview
This PR attempts to reduce trimming roots for Mono single-threaded WebAssembly targets by making System.STAThreadAttribute no longer unconditionally rooted in Mono’s ILLink descriptors, instead gating its retention on RuntimeFeature.IsMultithreadingSupported.
Changes:
- Removes the unconditional
System.STAThreadAttributeroot from the mainSystem.Private.CoreLibdescriptor list. - Re-adds
System.STAThreadAttributeas a feature-gated root conditioned onSystem.Runtime.CompilerServices.RuntimeFeature.IsMultithreadingSupported == true.
Co-authored-by: pavelsavara <271576+pavelsavara@users.noreply.github.com>
| <!-- STAThreadAttribute is read by the native runtime (object.c: prepare_thread_to_exec_main) | ||
| when launching an entry point via mono_runtime_run_main to set the apartment state. | ||
| On single-threaded runtimes this attribute can be trimmed: apartment state is meaningless | ||
| there and the native lookup tolerates the type being absent. --> | ||
| <assembly fullname="System.Private.CoreLib" feature="System.Runtime.CompilerServices.RuntimeFeature.IsMultithreadingSupported" featurevalue="true" featuredefault="true"> | ||
| <type fullname="System.STAThreadAttribute" /> | ||
| </assembly> |
There was a problem hiding this comment.
If the runtime tolerates it now, why keep it?
main PR
Description
System.STAThreadAttributewas unnecessarily retained in trimmed single-threaded Mono apps (browser/WASI).This PR now addresses both sides of the behavior:
src/mono/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.xmlkeepsSystem.STAThreadAttributerooted only whenSystem.Runtime.CompilerServices.RuntimeFeature.IsMultithreadingSupportedistrue.src/mono/mono/metadata/object.cwas updated to tolerate the attribute type being absent by using a try-load path when checking for[STAThread]on entrypoint methods.This preserves existing behavior on multithreaded Mono platforms while allowing single-threaded browser/WASI apps to trim the attribute safely.
Customer Impact
Without this fix, trimmed single-threaded Mono apps can retain unnecessary metadata, and WASI startup could fail when the attribute is trimmed.
Regression
No known product regression is introduced. The native startup path now handles missing
System.STAThreadAttributegracefully instead of assuming it is always present.Testing
./build.sh mono+libs./build.sh mono.runtime -c Release./build.sh -os wasi -subset mono+libs -c DebugRisk
Low to medium. The change is narrowly scoped to STA attribute lookup in Mono startup and linker rooting configuration. Multithreaded platforms continue to root the type; single-threaded platforms can trim it, and runtime lookup now safely handles absence.
Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.