{"action":"created","issue":{"url":"https://api.github.com/repos/AdamsLair/duality/issues/592","repository_url":"https://api.github.com/repos/AdamsLair/duality","labels_url":"https://api.github.com/repos/AdamsLair/duality/issues/592/labels{/name}","comments_url":"https://api.github.com/repos/AdamsLair/duality/issues/592/comments","events_url":"https://api.github.com/repos/AdamsLair/duality/issues/592/events","html_url":"https://github.com/AdamsLair/duality/issues/592","id":281518143,"number":592,"title":"Duality using a different SynchronizationContext inside the editor","user":{"login":"Barsonax","id":19387223,"avatar_url":"https://avatars2.githubusercontent.com/u/19387223?v=4","gravatar_id":"","url":"https://api.github.com/users/Barsonax","html_url":"https://github.com/Barsonax","followers_url":"https://api.github.com/users/Barsonax/followers","following_url":"https://api.github.com/users/Barsonax/following{/other_user}","gists_url":"https://api.github.com/users/Barsonax/gists{/gist_id}","starred_url":"https://api.github.com/users/Barsonax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Barsonax/subscriptions","organizations_url":"https://api.github.com/users/Barsonax/orgs","repos_url":"https://api.github.com/users/Barsonax/repos","events_url":"https://api.github.com/users/Barsonax/events{/privacy}","received_events_url":"https://api.github.com/users/Barsonax/received_events","type":"User","site_admin":false},"labels":[{"id":48735419,"url":"https://api.github.com/repos/AdamsLair/duality/labels/Core","name":"Core","color":"bfe5bf","default":false},{"id":48735426,"url":"https://api.github.com/repos/AdamsLair/duality/labels/Editor","name":"Editor","color":"bfe5bf","default":false},{"id":48735364,"url":"https://api.github.com/repos/AdamsLair/duality/labels/Task","name":"Task","color":"c7def8","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/AdamsLair/duality/milestones/2","html_url":"https://github.com/AdamsLair/duality/milestone/2","labels_url":"https://api.github.com/repos/AdamsLair/duality/milestones/2/labels","id":856226,"number":2,"title":"General","description":"Improving the existing functionality of Duality without creating whole new modules or systems.","creator":{"login":"AdamsLair","id":5013908,"avatar_url":"https://avatars3.githubusercontent.com/u/5013908?v=4","gravatar_id":"","url":"https://api.github.com/users/AdamsLair","html_url":"https://github.com/AdamsLair","followers_url":"https://api.github.com/users/AdamsLair/followers","following_url":"https://api.github.com/users/AdamsLair/following{/other_user}","gists_url":"https://api.github.com/users/AdamsLair/gists{/gist_id}","starred_url":"https://api.github.com/users/AdamsLair/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AdamsLair/subscriptions","organizations_url":"https://api.github.com/users/AdamsLair/orgs","repos_url":"https://api.github.com/users/AdamsLair/repos","events_url":"https://api.github.com/users/AdamsLair/events{/privacy}","received_events_url":"https://api.github.com/users/AdamsLair/received_events","type":"Organization","site_admin":false},"open_issues":66,"closed_issues":249,"state":"open","created_at":"2014-11-06T17:02:30Z","updated_at":"2017-12-12T20:05:49Z","due_on":null,"closed_at":null},"comments":0,"created_at":"2017-12-12T19:59:55Z","updated_at":"2017-12-12T20:09:34Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"### Summary\r\n\r\nCurrently inside the editor duality uses the [WindowsFormsSynchronizationContext](https://msdn.microsoft.com/en-us/library/system.windows.forms.windowsformssynchronizationcontext(v=vs.110).aspx). However outside the editor it uses no SynchronizationContext (which means this context is either null or equal to [SynchronizationContext](https://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext(v=vs.110).aspx). This can cause code to behave in a different way when running outside the editor and most likely it will break if this is the case.\r\n\r\nTo fix this inconsistency the following solutions could be applied:\r\n1. Make sure that duality uses no SynchronizationContext inside the editor. However since winforms depends on this this will probably mean you need to run winforms and duality in separate threads. I suspect many things will break if this is done.\r\n2. Make a custom DualitySynchronizationContext that will invoke the continuations in the OnUpdate loop. When using await this will make Tasks have the same behavior as my [PathRequest](https://github.com/Barsonax/Pathfindax/blob/develop/Source/Code/Pathfindax/PathfindEngine/PathRequest.cs) class in Pathfindax meaning continuations will execute on the mainthread. Make sure this context is only applied outside the editor as it will probably break winforms.\r\n3. Document this is a difference between running inside the editor and outside.\r\n\r\n### How to reproduce\r\n\r\n- Create a new component and implement ICmpInitializable\r\n- Put the following code in the OnInit method (the following code could also be done with await syntax):\r\n```csharp\r\nLog.Game.Write($\"Init thread: {Thread.CurrentThread.ManagedThreadId} Context: {SynchronizationContext.Current}\");\r\nTask.Run(() =>\r\n{\r\n    Log.Game.Write($\"Task run Thread: {Thread.CurrentThread.ManagedThreadId}\");\r\n}).ContinueWith(x =>\r\n{\r\n    Log.Game.Write($\"Task run continuation Thread: {Thread.CurrentThread.ManagedThreadId}\");\r\n}, TaskScheduler.FromCurrentSynchronizationContext());\r\n```\r\n- Run this code from inside the editor and from visual studio. Note that inside the editor the continuation is on the mainthread but in visual studio its on a different thread (a threadpool thread to be precise).\r\n\r\n### Workaround\r\n- While possible it can be quite alot of work to workaround this. In pathfindax I made a custom awaitable with its own custom logic so that the continuation is synced to the mainthread thus avoiding this whole issue. It stores the continuation in a callback which is then called in the OnBeforeUpdate of the plugin.\r\n\r\n### Analysis\r\nIf going for 1:\r\n- Duality wil run isolated from any UI no matter if its inside an editor or not.\r\n- Likely very complex to implement requiring many changes.\r\n\r\nIf going for 2:\r\n- Proper support for Tasks as the continuation will execute on the mainthread. Basically the same behavior when running a winforms or WPF application.\r\n- Still different SynchronizationContext inside the editor vs outside. However the behavior for the enduser 'should' be the same.\r\n\r\nIf going for 3:\r\n- Basically saying duality does not properly support Tasks as it can lead to inconsistent behavior.\r\n- Easy to implement.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/AdamsLair/duality/issues/comments/351178888","html_url":"https://github.com/AdamsLair/duality/issues/592#issuecomment-351178888","issue_url":"https://api.github.com/repos/AdamsLair/duality/issues/592","id":351178888,"user":{"login":"ilexp","id":14859411,"avatar_url":"https://avatars3.githubusercontent.com/u/14859411?v=4","gravatar_id":"","url":"https://api.github.com/users/ilexp","html_url":"https://github.com/ilexp","followers_url":"https://api.github.com/users/ilexp/followers","following_url":"https://api.github.com/users/ilexp/following{/other_user}","gists_url":"https://api.github.com/users/ilexp/gists{/gist_id}","starred_url":"https://api.github.com/users/ilexp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilexp/subscriptions","organizations_url":"https://api.github.com/users/ilexp/orgs","repos_url":"https://api.github.com/users/ilexp/repos","events_url":"https://api.github.com/users/ilexp/events{/privacy}","received_events_url":"https://api.github.com/users/ilexp/received_events","type":"User","site_admin":false},"created_at":"2017-12-12T20:09:34Z","updated_at":"2017-12-12T20:09:34Z","author_association":"OWNER","body":"**Note:** Some potentially related research regarding synchronization contexts can also be found in issue #295. Might be worth looking at as well, with regard to avoiding deadlock potential in manual synchronization adjustments."}}
{"actor":{"display_login":"ilexp"}}