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

Bug QueryLobbiesAsync() freezes Game

Discussion in 'Lobby' started by ruwenwiederkehr, Apr 5, 2023.

  1. ruwenwiederkehr

    ruwenwiederkehr

    Joined:
    Feb 10, 2023
    Posts:
    5
    I am currently trying to make a Lobby Menu where i display all the open lobbies, but everytime i start my game the Unity Editor freezes.

    I don't get any Errors, Warnings, etc. and i can't open/close my UnityEditor anymore. I have to manually close it in the Task Manager

    When i debug my Code, it happens to freeze at this line:
    Code (CSharp):
    1. QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync(OptionsFactory.QueryLobbiesOptions());
    Optionsfactory.cs
    Code (CSharp):
    1. public static QueryLobbiesOptions QueryLobbiesOptions()
    2.     {
    3.         QueryLobbiesOptions queryLobbiesOptions = new QueryLobbiesOptions
    4.         {
    5.             Count = 10,
    6.             Filters = new List<QueryFilter>
    7.                 {
    8.                     new QueryFilter(QueryFilter.FieldOptions.AvailableSlots, "0", QueryFilter.OpOptions.GT)
    9.                 },
    10.             Order = new List<QueryOrder>
    11.                 {
    12.                     new QueryOrder(false, QueryOrder.FieldOptions.Created)
    13.                 }
    14.         };
    15.         return queryLobbiesOptions;
    16.     }
    I even tried this version of querying the lobbies

    Code (CSharp):
    1. List<Lobby> lobbyList = (await Lobbies.Instance.QueryLobbiesAsync(OptionsFactory.QueryLobbiesOptions())).Results;
    2.             foreach (var lobby in lobbyList)
    3.             {
    4.                 Debug.Log(lobby.Name);
    5.             }
    but that won't work either.

    Anyone have the same issue or an idea how to fix this issue?

    Please do not hesitate and reply if you need more information and details.
     
  2. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    There are a few scenarios where using an await in C# can cause a deadlock (see this article for some details). This should be unrelated to the Lobby service, and you could verify it by replacing the
    await QueryLobbies
    call with another async call like
    await AuthBlahBlah.LoginAsync
    and verifying that it still freezes.

    This is all assuming that that's actually whats happening. Can you provide a sample of the code that's around the QueryLobbiesAync stuff? Maybe a link to a Github Gist or something if it's a lot of code and we can work from there.
     
  3. ruwenwiederkehr

    ruwenwiederkehr

    Joined:
    Feb 10, 2023
    Posts:
    5
    I don't think that the async call is the problem, since I have not only this await call. Every other (example: JoinLobbyAsync()) async method is runned correctly.

    Here is the full function where the code snippet is located:

    LobbyManager.cs

    Code (CSharp):
    1. public async Task<List<Lobby>> ListLobbies()
    2.     {
    3.         try
    4.         {
    5.             // shutdown here
    6.             QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync(OptionsFactory.QueryLobbiesOptions());
    7.  
    8.             List<Lobby> lobbyList = (await Lobbies.Instance.QueryLobbiesAsync(OptionsFactory.QueryLobbiesOptions())).Results;
    9.             foreach (var lobby in lobbyList)
    10.             {
    11.                 Debug.Log(lobby.Name);
    12.             }
    13.         }
    14.         catch (LobbyServiceException e)
    15.         {
    16.             Debug.Log(e);
    17.             return null;
    18.         }
    19.         return null;
    20.     }
    LobbyListManagerUI.cs
    Code (CSharp):
    1.  
    2.     public void ShowLobbyList()
    3.     {
    4.         try
    5.         {
    6.             var queryResponse = lobbyManager.ListLobbies().Result;
    7.  
    8.             // Create lobby list items for each lobby
    9.             foreach (Lobby lobby in queryResponse)
    10.             {
    11.                 GameObject listItem = Instantiate(lobbyListItemPrefab, lobbyListContent);
    12.                 listItem.GetComponentInChildren<Text>().text = lobby.Name;
    13.  
    14.                 Button joinBtn = listItem.GetComponentInChildren<Button>();
    15.                 joinBtn.onClick.AddListener(() => JoinLobby(lobby.Id));
    16.             }
    17.  
    18.             // Enable the lobby list UI
    19.             lobbyListUI.SetActive(true);
    20.         }
    21.         catch (LobbyServiceException e)
    22.         {
    23.             Debug.Log(e);
    24.         }
    25.     }
     
  4. ruwenwiederkehr

    ruwenwiederkehr

    Joined:
    Feb 10, 2023
    Posts:
    5
  5. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    Sorry, I missed your response. Glad you figured it out, the cause of the deadlock was line 6 of the ShowLobbyList function. The .Result is a blocking call which blocks the UI thread instul the ListLobbys async function completes. But ListLobbies needs to run on the UI thread, so as soon as you call await it will deadlock.