Bug 17878

Summary: Bug in awaiters (unnecessary Post)
Product: [Mono] Class Libraries Reporter: Grigory (Playtika) <GrigoryP>
Component: mscorlibAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mohitk, mono-bugs+mono
Priority: ---    
Version: 3.2.x   
Target Milestone: Untriaged   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: New repro

Description Grigory (Playtika) 2014-02-19 10:16:55 UTC
// Correct output is 123 (.NET)
// Wrong output is 132 (Mono)
// There should be no Context.Post if context in the end of called async method is same as the context in caller method

private async void Form1_Load(object sender, EventArgs e)
{
    await Wait();

    Console.Write(2);
}

private async Task Wait()
{
    await Task.Delay(50);

    BeginInvoke((Action) (() => Console.Write(3)));

    Console.Write(1);
}
Comment 1 Marek Safar 2014-02-19 12:55:47 UTC
I don't have simple way to test it as Winforms does not initialise for me but Mono 3.2.7 should have this issue fixed. Please reopen the bug report if it does not resolve your issue.
Comment 2 Grigory (Playtika) 2014-04-22 09:01:05 UTC
Still reproduces on 3.4.0
Comment 3 Marek Safar 2014-04-22 10:38:33 UTC
I think your code can produce both 123 and 132 or even 312 it depends on how fast is BeginInvoke.
Comment 4 Grigory (Playtika) 2014-04-23 06:09:15 UTC
Erm...
This is how SyncContexts work. It is a message loop with sequential execution of delegates. There is no non-determinism in single loop iteration.
Comment 5 Grigory (Playtika) 2014-04-23 06:13:48 UTC
Or it can be that BeginInvoke inlines execution of Delegate and does it in current iteration instead of posting it to next one.

I'll check this right now.
Comment 6 Grigory (Playtika) 2014-04-23 06:36:22 UTC
Created attachment 6637 [details]
New repro

Here is a repro without WindowsForms/BeginInvoke.

A simple console app with custom message loop. 
Result is the same.
Comment 7 Marek Safar 2014-04-25 12:04:09 UTC
Fixed in master

Thanks for the test case!
Comment 8 Mohit Kheterpal 2014-06-03 07:53:32 UTC
I have checked this issue on Windows and after running attached project in comment 6, I am getting output 123.

Hence closing this issue.

Environment info :
=== Xamarin Studio ===

Version 5.1 (build 302)
Installation UUID: 3b924754-5196-440a-b42b-05ebb3a3082e
Runtime:
	Microsoft .NET 4.0.30319.18408
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.25

=== Xamarin.Android ===

Version: 4.13.0 (Enterprise Edition)
Android SDK: E:\AndroidSDK
	Supported Android versions:
		1.6   (API level 4)
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: C:\Program Files\Java\jdk1.6.0_31
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)

=== Build Information ===

Release ID: 501000302
Git revision: 37edec4110ed93f02581f1331d2ff5b38f9a3aa9
Build date: 2014-05-30 12:45:30-04
Xamarin addins: b4105598b2afb41dac2f91a523ce94dbb3f1efdf

=== Operating System ===

Windows 6.1.7601.65536