Bug 28828 - ExecutionContext not shared to System.Runtime.Remoting.Messaging.AsyncResult
Summary: ExecutionContext not shared to System.Runtime.Remoting.Messaging.AsyncResult
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.12.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-04-06 23:34 UTC by debugthings
Modified: 2015-07-14 17:51 UTC (History)
4 users (show)

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


Attachments
Zip of Repro Application (6.01 KB, application/octet-stream)
2015-04-10 21:45 UTC, debugthings
Details


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:
Status:
RESOLVED FIXED

Description debugthings 2015-04-06 23:34:14 UTC
When running tests in xUnit against DNX for hosting. There is an issue where the AsyncResult will not get the ExecutionContext of the calling thread. There are two constructors for AsyncResult in /mono/mcs/class/corlib/System.Runtime.Remoting.Messaging/AsyncResult.cs. 

The default constructor does nothing, but the other constructor takes in parameters to define Callback, State and Copy Context. In order to correct my issue I had to make the following change. This change is a hack, but should shed some light on to what is happening in the Mono CLR.

        internal AsyncResult ()
        {
                current = ExecutionContext.Capture (); // ADDED HACK TO TEST
        }

        internal AsyncResult (WaitCallback cb, object state, bool capture_context)
        {
                async_state = state;
                async_delegate = cb;
                current = ExecutionContext.Capture (); // ADDED HACK TO TEST
        }



This may be related to BUG 28793 
https://bugzilla.xamarin.com/show_bug.cgi?id=28793
Comment 1 debugthings 2015-04-10 21:45:03 UTC
Created attachment 10721 [details]
Zip of Repro Application

This simple application creates a number of async tasks, Threadpool workers and other threads. The code will set the CallContext message to "world" on the main thread and attempt to spawn numerous child threads with various delays and work.

In .NET full we can observe all threads output the value "world", while the Mono version does not.

If you put the threadpool at the top of the execution it will work fine because it has already set that thread's context and will act as if it is shared.
Comment 2 debugthings 2015-04-10 21:46:59 UTC
It should be noted that this repro has nothing to do with DNX and exists solely in the Mono framework.
Comment 3 Miguel de Icaza [MSFT] 2015-04-10 22:04:30 UTC
I can confirm that I can build this test case and observe the reported issues

With:

mcs ProgramStatic.cs
mono ProgramStatic.exe

Marek, this is a CallContext bug, does this ring a bell?
Comment 4 debugthings 2015-04-11 15:13:21 UTC
Just an update on this, I compiled Mono master and was able to set MONO_THREADPOOL="microsoft" and these tests worked. I'm pasting the output to compare against what's in repro.

jldgit@ubuntu:/repos/dnxrepro$ MONO_THREADPOOL="microsoft" dnx . run
Waiting 30 seconds...
CallContext value from inside a ThreadPool.QueueUserWorkItem(): world           Thread Id: 5
Yo dawg, I heard you liked threadpools... CallContext value: world              Thread Id: 5
New thread from inside theadpool's threadpool... CallContext value: world               Thread Id: 7
CallContext value from inside a Task.Run(): world               Thread Id: 9
Thread inside ThreadPool.QueueUserWorkItem() inside Task.Factory.StartNew()... CallContext value: world         Thread Id: 9
New thread from inside Task.Factory.StartNew()... CallContext value: world              Thread Id: B
CallContext value from inside a Task.Factory.StartNew: world            Thread Id: A
New thread from inside Task.Run()... CallContext value: world           Thread Id: C
Theadpool inside Task.Run()... CallContext value: world         Thread Id: E
Main thread CallContext: world          Thread Id: 1
Comment 5 Marius Ungureanu 2015-07-14 17:51:19 UTC
As of 1st July, mono threadpool is gone and the microsoft implementation is being used.

93bb234cea3676b97559e752dcf82a26915d5f32