Bug 39459 - Corrupted async result when returning new struct directly
Summary: Corrupted async result when returning new struct directly
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-03-08 19:04 UTC by Matthew Orlando
Modified: 2016-03-30 22:18 UTC (History)
2 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 GitHub or Developer Community 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 FIXED

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.