Skip to content

Conversation

@taublast
Copy link

@taublast taublast commented May 25, 2025

This PR removes the following directive:

#if NET5_0_OR_GREATER
[module: System.Runtime.CompilerServices.SkipLocalsInit]
#endif

to resolve native crashes on mobile platforms (specifically catched on iOS and Android when using .NET 9 in MAUI app).

🧯 The Problem

In high-throughput UI scenarios (e.g. extremely fast list scrolling in MAUI), calls to:

Markdown.Parse(string, MarkdownPipeline)

began randomly triggering crashes. After isolating the issue, removing [SkipLocalsInit] fully resolved the problem.
Removing it ensures cross-platform safety and improves compatibility mobile/AOT targets.

Issue describing the case: #873

"This attribute is unsafe, because it may reveal uninitialized memory to the application in certain instances (for example, reading from uninitialized stack-allocated memory). If applied to a method directly, the attribute applies to that method and all its nested functions, including lambdas and local functions. If applied to a type or module, it applies to all methods nested inside. This attribute is intentionally not permitted on assemblies. To apply the attribute to multiple type declarations, use it at the module level instead."
docs.microsoft.com

🚀 Impact
Fixes crashes in mobile apps using Markdig. Improves confidence for developers using Markdig in MAUI, Unity, Blazor Hybrid, and other non-Windows environments.

Please let me know if you'd prefer the attribute be conditionally compiled or applied to only select methods, would happy to adjust.

@MihaZupan
Copy link
Collaborator

MihaZupan commented May 25, 2025

Removing the attribute feels like it's hiding the underlying problem here.
Do you have a reliable repro?

I don't see how the use in Markdig would lead to crashes, it's possible there's a codegen issue involved if this only reproes on Maui.

If it were a Markdig issue we should be fixing any code paths that aren't accounting for the attribute instead of removing it IMO.

@taublast
Copy link
Author

Yes, you are totally right. Removing this makes it works as referenced project on Debug. As soon as i compiled the custom nuget on Release the crash came back again. Tried to change StackallocThreshold no luck..
Still investigating, something related to GC hitting over spans in the middle of the Markdig processing.
Will pull out a repro in the upcoming days.
Will close the PR and would share more info inside the replated issue.

@taublast taublast closed this May 25, 2025
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.

2 participants