Bug 39459

Summary: Corrupted async result when returning new struct directly
Product: [Mono] Compilers Reporter: Matthew Orlando <matthew>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+monodroid, mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

Description Matthew Orlando 2016-03-08 19:04:06 UTC
We have a project that retrieves a web page using HttpWebRequest's GetResponseAsync. Once we have the final redirected response, we create a simple PageResult struct containing the HTTP status code, final Uri, and the text of the response.

The code works fine on both iOS and Android, on devices and on emulators, EXCEPT when built for Android on OSX. The last part of the http response code looks like this:

    using (var stream = response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        return new PageResult
        {
            status = response.StatusCode,
            uri = response.ResponseUri,
            content = await reader.ReadToEndAsync().ConfigureAwait(false)
        };
    }

When built by XamarinStudio for Mac, the code awaiting the PageResult sees both status & uri zeroed out, but the content has the correct string. The exact same code, the exact same project building on either Xamarin Studio or Visual Studio on windows shows the expected status code & uri.

Now here's where it gets interesting, If I change the code to the following, everything works correctly on all platforms:

    using (var stream = response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        var result = new PageResult
        {
            status = response.StatusCode,
            uri = response.ResponseUri,
            content = await reader.ReadToEndAsync().ConfigureAwait(false)
        };
        return result;
    }

Maybe the "await" inside the struct initializer is causing problems as part of the return statement?

I have created a project on GitHub illustrating this issue: https://github.com/cogwheel/XamAndroidAwaitBug
Comment 1 Marek Safar 2016-03-10 17:11:59 UTC
Fixed in master and Mono 4.4
Comment 2 Matthew Orlando 2016-03-30 19:50:05 UTC
Is there any chance of getting this fix released to the stable branch? Apple is rejecting our app because we had to select ARMv7 only in order to avoid this bug. Enabling both ARMv7 and ARM64 breaks the app unless we disable optimizations.
Comment 3 Matthew Orlando 2016-03-30 22:18:10 UTC
Sorry, I'm wrong. it wasn't this issue, it was another I reported through private support. Please disregard previous comment.