Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: System.Random and other dotnet classes throw an exception on iOS #73110

Open
atlasapplications opened this issue Feb 11, 2023 · 1 comment

Comments

@atlasapplications
Copy link

Godot version

v3.5.1.stable.mono.official.6fed1ffa3

System information

iOS 16.3

Issue description

This is similar to previous issues such as:
#38810
#40633
#40757

They were supposedly fixed by:
#49248

However, some of these classes are still not working. This seems to be contained only to the iOS platform when the app is submitted to App Store Connect, aka, not a simulator build. When simply instancing Random rand = new Random(); it throws an exception. This also seems to be connected to why this method- System.Text.Json.JsonSerializer.Serialize(); which comes from the System.Text.Json NuGet package also throws an exception because it gives this stack trace:

System.TypeInitializationException: The type initializer for 'System.Numerics.Hashing.HashHelpers' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Random' threw an exception. ---> System.EntryPointNotFoundException: SystemNative_GetNonCryptographicallySecureRandomBytes

Things I've tried:

  1. Using GD.RandRange() works, but only if I set Unhandled Exception Policy to LogError instead of TerminateApplication because it somehow still throws an exception while simultaneously returning the correct data. However, this doesn't solve the System.Text.Json.JsonSerializer() issue because it still seems to rely on System.Random specifically.
  2. I verified that System.Text.Json is compatible with net472 and net48 even though it is normally packaged with dotnet 6.0 and so on.
  3. I used a different source generator that does not use reflection because AOT on iOS prevents execution of dynamic code so the serialization part doesn't seem like the issue related to the System.Text.Json.

So that leads me to believe that it's probably not an issue with System.Text.Json. Perhaps symbols are being stripped when it's uploaded to App Store Connect, and when something that relies on native classes are called, an exception is thrown. Unfortunately, I'm not sure how to resolve something like that.

This symbol issue could be related to these:
#48701
#58454

Steps to reproduce

  1. Create a default mono enabled project.
  2. Use System.Random rand = new Random(); somewhere in a script.
  3. Wrap new Random() in a try catch and set Unhandled Exception Policy to LogError from project settings to more gracefully see issue.
  4. Export for iOS.
  5. Upload to App Store Connect.
  6. Download from TestFlight and run.

Alternatively, use the same steps but just use System.Text.Json.JsonSerializer.Serialize() after adding that as a NuGet package.
Make sure to change source generator from the default to metadata because of AOT restrictions on iOS.

Minimal reproduction project

N/A since described above.

@knightofiam
Copy link

Please see my comment on #58454. We're experiencing something similar, and I didn't want to duplicate what I wrote here, although this issue may be an even closer match.

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

No branches or pull requests

3 participants