Bug 15527 - Invalid IL code when using Async on Android
Summary: Invalid IL code when using Async on Android
Status: RESOLVED NORESPONSE
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.8.x
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-10-20 19:13 UTC by Jakub Arnold
Modified: 2018-04-13 19:25 UTC (History)
10 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Jakub Arnold 2013-10-20 19:13:37 UTC
I'm getting `Invalid IL code in ParkingHunter.Android.SearchAddressActivity/<OnCreate>c__async3:MoveNext (): IL_0033: stfld     0x040000a3` error on my Android app. I'm using a shared library which works on iOS just fine

Async itself seems to work fine, as this doesn't crash

    var tcs = new TaskCompletionSource<int>();
    tcs.SetResult(50);
    SearchEditText.Text = (await tcs.Task).ToString();

but using my library I get a System.InvalidProgramException

    var results = await SearchProvider.SearchForAddressAsync("foobar");

I'd like to stress out that I'm using the same exact code on iOS and Android, and it works on iOS.
Comment 1 Jakub Arnold 2013-10-20 19:41:39 UTC
I've also tried this on the alpha channel and got the same error :\
Comment 2 Jakub Arnold 2013-10-20 20:10:22 UTC
While trying to narrow this down even more, I found out the following. If I run the code in a click handler for a button, I'll get the `Invalid IL code` error.

            searchButton.Click += async (sender, e) =>
            {
                var results = await SearchProvider.SearchForAddressAsync("something");
                RunOnUiThread(() => ListAdapter.SetNewResults(results));
            };

But if I only run the code inside OnCreate it works fine. I have a feeling that I might be doing something wrong, but the exception is so low level that I have no idea why it's happening.
Comment 3 Jakub Arnold 2013-10-20 20:25:07 UTC
Sorry for spamming so many comments, but I think I've found a workaround, or actually two. It seems that the problem is that the event handler is marked async. If I just call another method from there which itself is async void it works fine

        searchButton.Click += (sender, e) =>
        {
            Derp();
        };

        public async void Derp()
        {
            var results = await SearchProvider.SearchForAddressAsync("something");
            ListAdapter.SetNewResults(results);
        }

or using ContinueWith directly in the click handler works as well

            searchButton.Click += (sender, e) =>
           {
               SearchProvider.SearchForAddressAsync("something").ContinueWith((task) =>
               {
                   RunOnUiThread(() => ListAdapter.SetNewResults(task.Result));
               });
           };

It seems that anything that makes the Click handler non-async does the trick. Works on Xamarin stable release as well.
Comment 4 Jonathan Pryor 2013-11-21 14:03:20 UTC
Are you able to try this on Windows? (It's possible that this is an mcs codegen bug; testing with CSC would be helpful, but would require that you rebuild the app + all referenced projects.)

What's your Mono version? `mono --version`

Would it be possible to attach a project which exhibits the problem? Comment #2 and Comment #3 are a start, but what's the base type, etc.
Comment 5 Sadik Ali 2013-12-11 10:54:58 UTC
I am not able to reproduce this issue,

Followed below steps to reproduce:

1. Downloaded sample app from link "https://github.com/conceptdev/xamarin-samples/tree/master/HttpClientPortable".
2. Debug android app.
3. Application deployed successfully.
4. Click on "HttpClient simple test" or "HttpClient secure test" button.
5. Getting response, 


Refer screen shot "http://screencast.com/t/q6sR927vhn"

checked with :
Lion 10.7.5
X.S 4.2.2(build 2)
X.iOS 7.0.5.2

Could you please recheck it ?
Comment 6 Brendan Zagaeski (Xamarin Team, assistant) 2014-03-12 14:25:24 UTC
@Sadik: This looks like a different bug (on Xamarin.iOS), maybe bug #16718?


@Jakub Arnold: I just filed a bug for a very similar-looking problem on bug #18324. In that case, upgrading to Mono 3.2.7 (now available on the Alpha channel) resolved the problem. If you still have a version of your project that shows the problem, you can test if upgrading to Mono 3.2.7 solves it.

Thanks in advance!
Comment 9 Jimmy [MSFT] 2017-08-25 22:57:50 UTC
Because we did not receive a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket with the requested information. Thanks!
Comment 11 Rexkans 2018-04-13 19:25:19 UTC Comment hidden (spam)