Bug 38599

Summary: Regression: SynchronizationContext.Current returns wrong value in delegate run by SynchronizationContext.Send.
Product: [Mono] Class Libraries Reporter: Tom Hindle <mono>
Component: Windows.FormsAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: 4.2.0 (C6)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test case showing regression.
Another example showing bug using System.Timers.Timer with SynchronizingObject set.

Description Tom Hindle 2016-02-09 22:24:43 UTC
Created attachment 14973 [details]
Test case showing regression.

See attached test program:

SynchronizationContext uiSyncContext = ....;
uiSyncContext.Send((state) =>
{
  // This should be true.
  // SynchronizationContext.Current == uiSyncContext
  // In mono 4.2.1 SynchronizationContext.Current returns null.
};

The following is output of test program running on various systems.
Note for mono 4.2.1 the last line doesn't return the expected result.

Output on .net:

Runing on UI thread context = 'System.Windows.Forms.WindowsFormsSynchronizationContext' expected: WindowsFormsSynchronizationContext
Thread running context = '' expected: (null or NOT WindowsFormsSynchronizationContext)
Running on UI thread context = 'System.Windows.Forms.WindowsFormsSynchronizationContext' expected: WindowsFormsSynchronizationContext

Output on mono 3.2.8:

Runing on UI thread context = 'System.Windows.Forms.WindowsFormsSynchronizationContext' expected: WindowsFormsSynchronizationContext
Thread running context = '' expected: (null or NOT WindowsFormsSynchronizationContext)
Running on UI thread context = 'System.Windows.Forms.WindowsFormsSynchronizationContext' expected: WindowsFormsSynchronizationContext


Output on mono 4.2.1

Runing on UI thread context = 'System.Windows.Forms.WindowsFormsSynchronizationContext' expected: WindowsFormsSynchronizationContext
Thread running context = '' expected: (null or NOT WindowsFormsSynchronizationContext)
Running on UI thread context = '' expected: WindowsFormsSynchronizationContext
Comment 1 Tom Hindle 2016-02-10 18:11:21 UTC
Created attachment 14988 [details]
Another example showing bug using System.Timers.Timer with SynchronizingObject set.

f
Comment 2 Marek Safar 2016-02-10 19:30:57 UTC
Fixed in master and Mono 4.3.2
Comment 3 Tom Hindle 2016-02-22 15:36:19 UTC
commit for fix was d5a93b13bf47508173e512178f32abd1e8411b98.