Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question HttpClient hangs forever

Discussion in 'Scripting' started by Tobias-J, May 16, 2024.

  1. Tobias-J

    Tobias-J

    Joined:
    Apr 11, 2013
    Posts:
    8
    Hello,

    My project have suddenly started hanging when I run in the editor.

    I have disabled all scripts except the one that fetches the data:


    Code (CSharp):
    1. using System;
    2. using System.Net.Http;
    3. using System.Threading.Tasks;
    4. using UnityEngine;
    5.  
    6. public class DataFetcher : MonoBehaviour
    7. {
    8.     private static HttpClient httpClient = new();
    9.     private static Uri wineUri = new Uri("https://ipecho.net/plain");//  
    10.  
    11.     void Start()
    12.     {
    13.         Debug.Log("fetcher starting");
    14.         try
    15.         {
    16.             string responseBody = FetchData().Result;
    17.  
    18.             Debug.Log(responseBody);
    19.         }
    20.         catch (HttpRequestException e)
    21.         {
    22.             Debug.Log("\nException Caught!");
    23.             Debug.Log("Message :" + e.Message);
    24.         }
    25.     }
    26.  
    27.     static async Task<string> FetchData()
    28.     {
    29.         return await httpClient.GetStringAsync(wineUri);
    30.     }
    31.  
    32. }
    I do not ever see the debug log "fetcher starting".
    When I comment out the call to FetchData or replace responseBody with a string like "test", everything proceeds and the scene loads as expected.

    As soon as I involve the HttpClient, Unity hangs forever. I get no errors and have to force-quit.

    Can you spot anything wrong here, or suggest how to proceed with debugging this?

    Edit to add:
    To make sure it isn't something funky elsewhere in my project, I created a new 2D core project, added an empty GO and then the script above. Unity still hangs. Using 2023.3.11f1.

    Can anyone confirm this behaviour, please?
     
    Last edited: May 16, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,963
    You aren't awaiting the result. Mark the Start method as async and add await before FetchData().Result.
     
    Tobias-J and bugfinders like this.
  3. Tobias-J

    Tobias-J

    Joined:
    Apr 11, 2013
    Posts:
    8
    Silly me.
    I extracted that code from a month-old class that did a little of this and that, then suddenly stopped working even if I hadn't changed a thing for weeks.

    Your suggestions did the trick, but I'm still clueless as to why it stopped working in the first place.
    meh, I guess I'll learn to live with it. I a few years.

    Thanks!

    PS: I'd love to mark your reply as the answer or the thread as resolved, but I simply can't seem to find a way to do so.
     
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    847
    At the top of the page, click
    Thread Tools > Edit Title

    Click the part that says “Question” and change it to “Resolved”
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,963
    The compiler or IDE should actually warn you about "this call is not awaited". Rider does that quite prominently, highlighting the statement in yellow.