Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JETom · Jan 20, 2021 at 02:07 PM · crashfreezeasynchttprequest

Unity freezes when adding Task to List

I have the follwing code :

 List<Task<Response>> tasks = new List<Task<Response>>();

 foreach (string url in urls)
 {
     Task<Response> task = AsyncRequest(url);
     tasks.Add(task);
 }
     
 Task.WaitAll(tasks.ToArray());

Where Response is a simple class with 2 members :

 public class Response {
     public bool isError;
     public string content;
 }

And the task is the following:

 public async Task<Response> AsyncRequest(string url) {

     Task<HttpResponseMessage> getTask = httpClient.GetAsync(url);
     HttpResponseMessage message;

     try
     {
         message = await getTask;
     }
     catch (Exception e)
     {
         Debug.LogFormat("GET request error for URL {0}: {1}", url, e);
         return null;
     }
     Response response = new Response
     {
         isError = !message.IsSuccessStatusCode,
         content = await message.Content.ReadAsStringAsync()
     };
     return response;
 }

When the code is executed, Unity freezes on the line tasks.Add(task).

In my original code, the task was stored with a key in a Dictionary<string, Task<Response>>, and it ended up the same way with a freeze of Unity.

If I create a list of tasks without result like List<Task> tasks, the freezing behaviour doesn't happen anymore when adding elements to the list but when the line Task.WaitAll(tasks) is reached.

Finally, I found out that using an array Task<Response>[] tasks does the same thing as above and freezes at Task.WaitAll(tasks) too.

My question is why does Unity crashes here ? What are the option I have to solve this issue ?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jan 20, 2021 at 09:58 PM 0
Share

You could use Task.WaitAll(tasks, 1000); to wait all tasks for a timeframe of 1000 milliseconds before continuing on. If it still freezes when this happens, errors may be occuring in the tasks, I guess, causing it to freeze. Try putting it into a try-catch statement and catching the exceptions, if there are any.

avatar image Bunny83 Llama_w_2Ls · Jan 21, 2021 at 02:25 AM 1
Share

Right, one of the reasons I don't really like the async / Task mechanism. Depending on the active and used scheduler the behaviour could vary drastically. Unity usually uses a syncronous scheduler similar to the one that drives the coroutines. Therefore the async functions do not run on a seperate thread but are scheduled on the main thread. Depending on what the tasks look like, if they can not "yield" / finish properly you could trap yourself in a deadlock. Without knowing what those async tasks actually do it's hard to tell what's exactly happening. Also note that "Task.WaitAll" is also a task that should be awaited. Since we don't see the encosing / calling code it's even harder to follow the flow of control. Custom schedulers and promises / continuations makes it quite hard to reason about the exact behaviour, especially in more complex scenarios.

That's why I'm still a fan of coroutines as they are 100% deter$$anonymous$$istic once you understand how they work. You can't get trapped in a blocking infinite loop unless you literally created one because you did not yield. Async / await is much more obscure.


Don't get me wrong async / await is a much nicer and much more flexible syntax / concept and certainly is the future. Though I simply don't like to use code I do not fully understand or can not follow or reason about. You know, hope ain't a tactic ^^.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by davorz32 · Apr 13 at 03:32 AM

Use await Task.WhenAll(taks) instead Task.WaitAll(taks)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Post Requests without Coroutines 2 Answers

Switch Levels with Async and still display progress 2 Answers

Editor Hangs after first play using plugin DLL 2 Answers

Unity randomly going unresponsive when importing script changes 0 Answers

Xbox UWP Game Crashing Upon Loading 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges