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

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


Attachments

Description David Karlaš 2015-11-04 06:57:53 UTC
Here is small repro:
https://gist.github.com/DavidKarlas/c914ef6d6dcb0e3bb9db

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 :).
https://social.msdn.microsoft.com/Forums/vstudio/en-US/629d5524-c8db-466f-bc27-0ced11b441ba/taskcontinuewith-from-wcf-client-call-has-no-synchronizationcontext?forum=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 5.4.0.135 (2017-06/6425f06) but I didn't get the "SynchronizationContext.Current is null" message, so marking as fixed.

Note You need to log in before you can comment on or make changes to this bug.