Bug 16587 - Task implementation does not run continuations on correct TaskScheduler
Summary: Task implementation does not run continuations on correct TaskScheduler
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.2.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-12-04 08:26 UTC by Øystein Krog
Modified: 2013-12-10 12:46 UTC (History)
3 users (show)

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


Attachments
repro project (94.54 KB, application/zip)
2013-12-04 08:26 UTC, Øystein Krog
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 Øystein Krog 2013-12-04 08:26:38 UTC
Mono 3.2.5.

The problem is essentially that when running in an async method and awaiting a task, the rest of the code in the method does not run on the "original" TaskScheduler/task context.

I've attached a repro-case, in which I use a custom single-thread TaskScheduler to illustrate the problem.

The code/contexts never have SynchronizationContext set, so as far as I understand it should resume on TaskScheduler.Current, which it does not.

This is a really serious bug in the task implementation IMO, as it leads to subtle problems in Task/async-heavy applications.

I found this while bug-hunting for a Task-related hard-crash bug in TaskAwaiter when running on an ios device, the repro case triggers when running under plain mono os x (console app).
It does not matter if the repro case is compiled by mono or original .net (under windows).
Comment 1 Øystein Krog 2013-12-04 08:26:56 UTC
Created attachment 5599 [details]
repro project
Comment 2 Øystein Krog 2013-12-04 08:27:50 UTC
Output when running on windows/.net:

Scheduler A: 41149443
Scheduler B: 39785641
Current Thread ID     Expected Thread ID      Current Scheduler       Expected Scheduler
11                     11                       41149443               41149443
12                     12                       39785641               39785641
11                     11                       41149443               41149443
Comment 3 Øystein Krog 2013-12-04 08:28:35 UTC
Output when running under mono:

Scheduler A: 671611029
Scheduler B: 425590757
Current Thread ID     Expected Thread ID      Current Scheduler       Expected Scheduler
3                     3                       671611029               671611029
4                     4                       425590757               425590757
4                     3                       259417723               671611029
Comment 4 Øystein Krog 2013-12-04 08:31:51 UTC
Mono JIT compiler version 3.2.5 ((no/964e8f0 Thu Nov 14 14:48:50 EST 2013)
Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       altstack
        Notification:  kqueue
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          yes(3.3svn-mono)
        GC:            sgen
Comment 5 Øystein Krog 2013-12-04 09:25:21 UTC
Tested with latest master, same problem.

Mono Runtime Engine version 3.2.7 (master/d2485bf Wed Dec  4 14:56:14 CET 2013)
Copyright (C) 2002-2013 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       altstack
        Notification:  kqueue
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen
Comment 6 Marek Safar 2013-12-05 03:52:32 UTC
Fixed in master
Comment 7 Joshua Barker 2013-12-05 23:52:06 UTC
Any idea when this will be pushed out?
Comment 8 Marek Safar 2013-12-06 04:05:33 UTC
It should go out with Mono 3.2.6
Comment 9 Øystein Krog 2013-12-10 12:46:59 UTC
Thanks for the fix, looking forward to testing this against our codebase.