Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Asnyc/await freeze with nested tasks

Discussion in 'Editor & General Support' started by ComradeVanti, May 3, 2022.

  1. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    I've seen a few posts about Addressables freezing but noone seems to know what the cause is. I have narrowed my case down to the following minimal repro:

    Code (CSharp):
    1. private void Awake() =>
    2.   Debug.Log(DoWork());
    3.  
    4. private GameObject DoWork() =>
    5.   DoWorkAsync().Result;
    6.  
    7. private async Task<GameObject> DoWorkAsync() =>
    8.   await Addressables.LoadAssetAsync<GameObject>("Thing").Task;
    As you can see, I have an async load operation which I turn into a task and await. I then try to get the result of the resulting task. When I run, Unity will freeze on the "await".

    I have found a similar issue report which was marked as wontfix and mentioned that the issue seems that Unity cannot handle multiple waiting tasks at once or something similar, I'm not quite sure. Seems like my repro problem above is quite a common situation, so in my opinion it would be a little strange if this "is supposed" to not work.

    Anyway, I would love to hear if anyone has experienced this issue as well and maybe has found a solution.

    Cheers

    Specs: Unity 2020.3, IL2CPP, .netstandard 2.0, Addressables 1.18.19, Win10
     
    Last edited: May 3, 2022
  2. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    Also, I would love to post an issue about this if this is indeed a bug that should be fixed. If any Unity dev could stop by and confirm I will post the issue.
     
  3. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    Ok, seems this issue is unrelated to addressables. I have reproduced it with this setup now:

    Code (CSharp):
    1.  
    2. private void Awake() =>
    3.   Debug.Log(RunFirst());
    4.  
    5. private int RunFirst() =>
    6.   RunFirstAsync().Result;
    7.  
    8. private async Task<int> RunFirstAsync() =>
    9.    await RunSecondAsync();
    10.  
    11. private async Task<int> RunSecondAsync()
    12. {
    13.   await Task.Delay(1000);
    14.   return 1;
    15. }
    I have changed the title of the thread to be more general.

    Specs same as in top post.
     
  4. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    Found a StackOverflow post talking about this issue. Seems like this is related to synchronization-contexts and deadlocks. No easy solution, but at least an explanation. Also looks like its a somewhat Unity-independent issue.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Not really a solution to your problem (well, sort of is), but you should look at the UniTask package: https://github.com/Cysharp/UniTask

    It's provides a more Unity specific awaitable struct (allocation free!), and works by hooking directly into the Unity player loop. Might be worth a try to see if lets you do what you're trying to do.