From b95cc40640738f0c26b80b5ec96b4301858916cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Cig=C3=A1nek?= Date: Mon, 14 May 2018 12:22:20 +0200 Subject: [PATCH] Set current SynchronizationContext before the game loop starts This fixes the problem that `SynchronizationContext.Current` would be null during the call to `_EnterTree`, `_Ready` and the first call to `_Process` thus the task continuations would be scheduled outside the main thread, which is unexpected and might lead to crashes. With this change, task continuations are scheduled always on the main thread and so async/await can be used without any explicit synchronization, which is what is expected. Fixes #18849 (cherry picked from commit f25240cfe6a4b7f030b2dfd5db1365b4de44301d) --- modules/mono/glue/cs_files/GodotTaskScheduler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/mono/glue/cs_files/GodotTaskScheduler.cs b/modules/mono/glue/cs_files/GodotTaskScheduler.cs index 6bf25a89d2f..3d23ec10f1c 100644 --- a/modules/mono/glue/cs_files/GodotTaskScheduler.cs +++ b/modules/mono/glue/cs_files/GodotTaskScheduler.cs @@ -14,6 +14,7 @@ namespace Godot public GodotTaskScheduler() { Context = new GodotSynchronizationContext(); + SynchronizationContext.SetSynchronizationContext(Context); } protected sealed override void QueueTask(Task task) @@ -57,7 +58,6 @@ namespace Godot public void Activate() { - SynchronizationContext.SetSynchronizationContext(Context); ExecuteQueuedTasks(); Context.ExecutePendingContinuations(); }