runtime: AndroidStripILAfterAOT causing "InvalidProgramException: Invalid IL code in Xunit.Assert:True(bool)"

Android application type

.NET Android (net7.0-android, net8.0-android, etc.)

Affected platform version

VS 2022 17.8.5

Description

When setting AndroidStripILAfterAOT=true in a test app, some tests are failing the first time we run them, but then they’ll pass if we run them again. The error is 100% reproducible when starting the app again. Here is the error: InvalidProgramException: Invalid IL code in Xunit.Assert:True(bool): method body is empty. No issue in Debug. No issue when removing AndroidStripILAfterAOT=true. This is similar to dotnet/runtime#104156, but no BroadcastReceiver is involved here.

Steps to Reproduce

  1. Open repro in visual studio.
  2. Run app in Release on android emulator.
  3. Run all tests: one of the test fails.
  4. Run all tests again: both tests succeed.

Did you find any workaround?

Remove AndroidStripILAfterAOT=true.

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 31 (16 by maintainers)

Most upvoted comments

@tipa yea that looks related.


Our next steps here are to wait for this fix to flow to the Android and ios SDKs and to get some performance benchmarks. If there is negligible performance impact on non-racy code we will try to do a net8.0 backport.

I’ve just tested net9-pre7. The fix works on my repro project. Thx.

The fix to this issue is on track to be shipped as part of .NET9 Preview7. Additionally, I’ve opened an PR to backport this fix to .NET8.

Fun news - I believe I can get the same failure on iOS too https://github.com/lambdageek/maui-aot-race

The underlying issue is how mono_aot_get_method races with load_aot_module - one thread could attempt to read MonoImage:aot_module while the other thread is still loading the aot module.

@tranb3r Do you have an iOS app which the same kind of testing, meaning a few tests within one assembly? If so, are they running fine?

@fanyang-mono I’ve added the net8.0-ios target to the repro. I do not have any issue with it : tests are running fine. But, is there actually an equivalent for AndroidStripILAfterAOT on iOS ?

@fanyang-mono Did you manage to access the repo and rebuild the test app?

Yes, working on it.

@tranb3r I was caught up by some other projects. Will find some time to look into this this week. Sorry about the delay.

@tranb3r I was able to reproduce with the apk file that you provided. Will look into it.