Skip to content

Conversation

@benwulfe
Copy link
Contributor

@benwulfe benwulfe commented Feb 1, 2017

Task.Run should run tasks on the threadpool but are instead run on SynchronizationContext.Current.

See: http://stackoverflow.com/questions/41959490/firebase-makes-tasks-execute-only-on-main-thread-causing-unity-to-freeze/41988539#41988539

Basically what's going on is that the TaskFactory defaults to the SynchronizationContext.Current scheduler, which is not correct as new tasks always should be run on the threadpool. TaskScheduler.FromCurrentSynchronizationContext() is instead intended to be used with ContinueWith as a mechanism for controlling the thread that the callback will run on. But adding that support is a new feature.

Supporting information:

MSDN Documentation that Task.Run should run on the threadpool:
https://www.google.com/url?q=https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.run(v%3Dvs.110).aspx&sa=D&usg=AFQjCNFx3qW3vSsmhpDONsRDBdynzbKHgA

source code for mono that defaults to threadpool:
https://www.google.com/url?q=https://github.com/mono/mono/blob/0bcbe39b148bb498742fc68416f8293ccd350fb6/mcs/class/referencesource/mscorlib/system/threading/Tasks/TaskFactory.cs%23L93&sa=D&usg=AFQjCNGLQ0gm5KlaitmEV2xr29BHutDKNQ

…nchronizationContext.Current.

See: http://stackoverflow.com/questions/41959490/firebase-makes-tasks-execute-only-on-main-thread-causing-unity-to-freeze/41988539#41988539

Basically what's going on is that the TaskFactory defaults to the SynchronizationContext.Current scheduler, which is not correct as new tasks always should be run on the threadpool.  TaskScheduler.FromCurrentSynchronizationContext() is instead intended to be used with ContinueWith as a mechanism for controlling the thread that the callback will run on.  But adding that support is a new feature.

Supporting information:

MSDN Documentation that Task.Run should run on the threadpool:
  https://www.google.com/url?q=https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.run(v%3Dvs.110).aspx&sa=D&usg=AFQjCNFx3qW3vSsmhpDONsRDBdynzbKHgA

source code for mono that defaults to threadpool:
https://www.google.com/url?q=https://github.com/mono/mono/blob/0bcbe39b148bb498742fc68416f8293ccd350fb6/mcs/class/referencesource/mscorlib/system/threading/Tasks/TaskFactory.cs%23L93&sa=D&usg=AFQjCNGLQ0gm5KlaitmEV2xr29BHutDKNQ
@richardjrossiii
Copy link
Contributor

It's been a REALLY long time since I dealt with this code, but IIRC (and maybe @hallucinogen can remember), we had it this way to support Unity's strange thread scheduling with it's WWW class, which could only be used from the main thread.

If all the relevant async APIs still work with this change (and you can show a quick demo of the APIs working, log output is more than enough), I'm more than happy to change this to be more accurate toward the standard .NET version.

@richardjrossiii richardjrossiii self-assigned this Feb 6, 2017
@benwulfe
Copy link
Contributor Author

benwulfe commented Feb 6, 2017 via email

@richardjrossiii
Copy link
Contributor

I'm referring to the Parse APIs here (ParseObject.fetch, etc.). As long as those still function, this is a solid change.

@benwulfe
Copy link
Contributor Author

It's been quite a while, but I've finally come back to this, got a local parse server working along with the unity sdk rebuilt and working in a test app.

All unit tests pass (192/192) with this change in place
Parse apis query and save still work as expected in a test unity3d project. ContinueWith still properly marshals

Really, this change should not be impactful in most scenarios. You would only see a change in behavior if SynchronizationContext.Current was set -- which it is not in most situations in unity3d.

Another interesting fact is that without SynchronizationContext being set, the ContinueWith callback will always be executed on a worker thread which you can see evidence of user issues such as this one here:
https://stackoverflow.com/questions/24441501/main-thread-issue-with-parse-com-queries
This is not related to this change.

Please let me know if there is any additional testing you'd like me to do.

@benwulfe
Copy link
Contributor Author

Note that this is an important thing to get in for Google Firebase (I work for Google) which uses the Task library built here.
The main problem is that since Parse compiles all source together into Parse.Unity.dll, a developer who wishes to use both Parse and Firebase (say for Firebase Cloud Messaging) must use the Parse version of Task. If this version in Parse is not fixed, then Firebase may not function properly.
Regards

@montymxb
Copy link
Contributor

@benwulfe glad to see you've managed to get back to this! I've been meaning to put some TLC into this project, but I'm going to be gone for a couple weeks coming up soon here. I don't think I'll have a chance to look at this until I'm back, but once I am I'll double check those tests and see how things look.

@benwulfe
Copy link
Contributor Author

ping!

@montymxb
Copy link
Contributor

@benwulfe whoops time seems to fly! I'll take some time to look over the tests tomorrow and get back to you in the afternoon, apologies.

Copy link
Contributor

@montymxb montymxb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright changes look good to me! @flovilmart this should be good to go. There will be more coming up before we do another release, but this is getting in that direction.

@codecov
Copy link

codecov bot commented Dec 2, 2017

Codecov Report

Merging #251 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master    #251   +/-   ##
======================================
  Coverage    67.2%   67.2%           
======================================
  Files         127     127           
  Lines       10139   10139           
  Branches     1455    1455           
======================================
  Hits         6814    6814           
  Misses       3127    3127           
  Partials      198     198

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6d50fab...663ee2c. Read the comment docs.

@montymxb
Copy link
Contributor

montymxb commented Dec 3, 2017

@benwulfe sorry for the delay! We're actively working on changes atm, expect to see some new stuff soon.

@montymxb montymxb merged commit 1c59dbf into parse-community:master Dec 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants