Bug 35530 - [Mono 4.2 Regression] SynchronizationContext.Current == null on UI Thread
Summary: [Mono 4.2 Regression] SynchronizationContext.Current == null on UI Thread
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-11-04 06:57 UTC by David Karlaš
Modified: 2017-09-06 17:39 UTC (History)
5 users (show)

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:

Description David Karlaš 2015-11-04 06:57:53 UTC
Here is small repro:

In case of Mono 4.2(+)(after referencesource import of TPL) it will output:
>"Got here"
>"SynchronizationContext.Current is null"

But in case of Mono 4.0(-) it will output:
>"Got here"

SynchronizationContext.Current should never be null on UI Thread.

Here is also question and answer on MSDN where answer says TPL team is aware of bug, but it's not clear if it was ever fixed(notice this was 2011).

Don't skip this link just because it says WCF, it's not related to WCF :).

Notice that I can't reproduce this on any version of .Net on Windows.
Comment 1 Alexis Christoforides 2016-03-21 23:05:23 UTC
I've looked into this and it looks like the SynchronizationContext is indeed properly "flowed", even if never surfaced in the Current property. 

This sample (https://gist.github.com/alexischr/7f0b71abbc452b828d15) demonstrates that TPL uses the correct SyncContext for continuations ( boo() will run within the context even when it's reported null).

The referencesource ExecutionContext code is very complex and I haven't figured out yet how to surface SyncCtx without breaking it.
Comment 2 Ludovic Henry 2017-09-06 17:39:07 UTC
I tried to reproduce with Mono (2017-06/6425f06) but I didn't get the "SynchronizationContext.Current is null" message, so marking as fixed.