Bug 17878 - Bug in awaiters (unnecessary Post)
Summary: Bug in awaiters (unnecessary Post)
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.2.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2014-02-19 10:16 UTC by Grigory (Playtika)
Modified: 2014-06-03 07:53 UTC (History)
3 users (show)

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

New repro (4.09 KB, application/octet-stream)
2014-04-23 06:36 UTC, Grigory (Playtika)

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:

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();


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

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

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
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
	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