Unity lobby creation works in editor but silently fails in android build with no exception

I am working on a horror co-op multiplayer game named “Blackout-Multiplayer Horror” and i am using distributed authority as network topology.
When creating a lobby using multiplayer services a lobby is created successfully in editor but on an android build it just silently fails without even throwing an exception.

Sample Code :

SessionOptions options = new SessionOptions()
{
    SessionProperties = new Dictionary<string, SessionProperty>
    {}.WithDistributedAuthorityNetwork();

playerName = project_Networkmanager.instance.getPlayerName();
try
{
    Debug.Log("Creation Started");
    currentSession = await MultiplayerService.Instance.CreateSessionAsync(options); --> stucks here
    Debug.Log("Creation Ended");-->this line is never reached sometimes

    registerLobbyEvents();

    onLobbyCreationCompleted?.Invoke(this, EventArgs.Empty);
}
catch (Exception e)
{
    onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
    Debug.Log("Cant Create a Sesion " + e);
}

Note : every lobby function is wrapped in a try catch block and everything is a async Task
i handled all the exceptions but its not even reaching the catch block and not failing hence just stuck at the create session line.

i used android logcat to check whats happening but the qqos is not even starting and it just print “gamestateloading : true/false”

and when i restart the game i works fine and qqos starts correctly and then i restart again i works and then again i restart it fails the pattern is so random, then i tried in different devices they also have the same issue.But the created lobby is shown in the public query for lobbies and i was able to join in it but the lobby creator is not there its just me alone in the lobby ,i dont know what causing this problem if anyone in unity seeing this post please work on this issue.

Note : i asked gpt about this and it says this is a common issue in android but i cant find any post related to this issue and all it gives me is delay the process like using a coroutine or repeatedly spam the create session until it created.

Hope someone fixes this!

Hi @Angie777,

Could you provide the Unity editor, NGO, and multiplayer services versions you are using? This will help us narrow down what the issue could be.

Editor - 6.3
Ngo - 2.8.1
Multiplayer Services - 2.0.0

as mentioned this issue is so random i asked gpt and it said some “Task” may get lost in the main thread if the app lost focus or paused but this issue happens even if we actively interact with the game.

Hope this helps.

Thank you for that @Angie777.

It looks like you are using the more current versions so we can rule that out.
To help expedite resolving this issue could you submit this issue via the editor and include your project so we can try to replicate the issue on our end to determine where the failure is occurring?

Once you have submitted, you should get a ticket number for the issue filed. Once you have that, could you provide that here so I can can get it assigned to our group faster?

Thank you for your patience.

Sorry for this late reply,
Just stuck with issue for a long time and my project is around 25 gigs ,i cant send the entire one but heres the script i now use to avoid it from stucking at creation.

 public async Task createSession(string lobbyName, bool onlyFriends,bool wantVC)
 {
     SessionOptions options = new SessionOptions()
     {
         //change this after game completion
         IsPrivate = onlyFriends,
         MaxPlayers = maxPlayers,
         SessionProperties = new Dictionary<string, SessionProperty>
         {
             {strings.gameStarted.ToString(),new SessionProperty(strings.False.ToString(),index:PropertyIndex.String1) },
             {strings.map.ToString(),new SessionProperty(null) },
             {strings.lobbyname.ToString(),new SessionProperty(lobbyName) },
             {strings.wantVC.ToString(),new SessionProperty(wantVC.ToString()) }
         }
     }.WithDistributedAuthorityNetwork();

     playerName = project_Networkmanager.instance.getPlayerName();
     try
     {
         onLobbyCreation?.Invoke(this, EventArgs.Empty); 
         await Task.Yield();
         currentSession = await AwaitWithTimeout(MultiplayerService.Instance.CreateSessionAsync(options),30000);
         //setting player props
         await setPlayerProps(wantVC);

         registerLobbyEvents();

         //await currentSession.RefreshAsync();

         onLobbyCreationCompleted?.Invoke(this, EventArgs.Empty);
     }
     catch (TimeoutException e)
     {
         Debug.LogError("CreateSession timeout: " + e);
         onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
     }
     catch (Exception e)
     {
         onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
         Debug.Log("Cant Create a Sesion " + e);
     }
 }
 public static async Task<T> AwaitWithTimeout<T>(Task<T> task, int timeoutMs)
 {
     var timeoutTask = Task.Delay(timeoutMs);

     var completed = await Task.WhenAny(task, timeoutTask);

     if (completed == timeoutTask)
         throw new TimeoutException("UGS task timed out");

     return await task; 
 }

I just created a timeout task that get triggered after 30 secs i do know its annoying to wait 30 secs for just a lobby creation but that the optimal solution that i have for now

and for side note this fail happens more on wifi than mobile internet i dont know whether the two or related or not and i call the lobby creation after an add has been played like this,

ads_Manager.instance.onRewarded += createLobby;

 private void createLobby(object sender, EventArgs e)
 {
     _ = Lobby_Manager.instance.createSession(lobbyName.text, onlyFriends.isOn, wantVCToggle.isOn);
 }

this event fires when the rewarded ad completes and all the ads are working properly but when the ad finishes up until this event is triggered.

onLobbyCreation?.Invoke(this, EventArgs.Empty); 

but the actual creatsession is never called or the qqos services is never started.

I apologize for not sending the entire project ,my game just released and there is a hell of a bug that need to be fixed ,if you have any better solutions please let me know.

Thanks in advance.

Finally found the core of this issue.
It’s a unity internal exception that does not surface to the catch block.
Here’s the error

1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2314 Unity Unobserved task exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Connection failed: Socket closed during connection attempt: WebsocketUndefined.) ---> Unity.Services.Wire.Internal.ConnectionFailedException: Connection failed: Socket closed during connection attempt: WebsocketUndefined.
1982 2314 Unity   at Unity.Services.Wire.Internal.Client.ConnectAsync () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2314 Unity   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback (System.T
1982 2165 Unity CreateSession timeout: System.TimeoutException: UGS task timed out
1982 2165 Unity   at Lobby_Manager.AwaitWithTimeout[T] (System.Threading.Tasks.Task`1[TResult] task, System.Int32 timeoutMs) [0x00000] in <00000000000000000000000000000000>:0 
1982 2165 Unity   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
1982 2165 Unity   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
1982 2165 Unity   at UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
1982 2165 Unity   at UnityEngine.UnitySynchronizationContext.Exec () [0x00000] in <00000000000000000000000000000000>:0 
1982 2165 Unity --- End of stack trace from previous location where exception was thrown ---
1982 2165 Unity 
1982 2165 Unity   at Lobby_Manager.createSession (System.String lobbyName, System.Boolean onlyFriends, System.Boolean wantVC) [0

This is the entire error log from logcat and it says its a websocket error for some reason even though i have a stable internet connection.

Here’s my code

 public async Task createSession(string lobbyName, bool onlyFriends,bool wantVC)
 {
     SessionOptions options = new SessionOptions()
     {
         //change this after game completion
         IsPrivate = onlyFriends,
         MaxPlayers = maxPlayers,
         SessionProperties = new Dictionary<string, SessionProperty>
         {
             {strings.gameStarted.ToString(),new SessionProperty(strings.False.ToString(),index:PropertyIndex.String1) },
             {strings.map.ToString(),new SessionProperty(null) },
             {strings.lobbyname.ToString(),new SessionProperty(lobbyName) },
             {strings.wantVC.ToString(),new SessionProperty(wantVC.ToString()) }
         }
     }.WithDistributedAuthorityNetwork();

     playerName = project_Networkmanager.instance.getPlayerName();
     try
     {
         onLobbyCreation?.Invoke(this, EventArgs.Empty);

         Debug.Log("Creating session");

         currentSession = await AwaitWithTimeout(MultiplayerService.Instance.CreateSessionAsync(options),30000);

         Debug.Log("Creating session successfully");


         //setting player propsadwad
         await setPlayerProps(wantVC);

         registerLobbyEvents();

         //await currentSession.RefreshAsync();

         onLobbyCreationCompleted?.Invoke(this, EventArgs.Empty);
     }
     catch (OperationCanceledException e)
     {
         Debug.Log(e);
         onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
     }
     catch (TimeoutException e)
     {
         Debug.LogError("CreateSession timeout: " + e);
         onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
     }
     catch (Exception e)
     {
         onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
         Debug.Log("Cant Create a Sesion " + e);
     }
 }

this is called from an event which get triggered whenever an ad is closed or if they purchased ads then the event is called with null params like this

private void createLobby(object sender, EventArgs e)
{
    Debug.Log("Working");
    createLobby();
}
public async void createLobby()
{
    try
    {

        await Lobby_Manager.instance.createSession(lobbyName.text, onlyFriends.isOn, wantVCToggle.isOn);
    }
    catch (Exception e)
    {
        onLobbyCreationFailed(null, null);
        Debug.Log(e);
    }
}

I don’t know whether this is my code issue or a unity bug that suppress this error and i found this using this line of code.

TaskScheduler.UnobservedTaskException += (s, e) =>
{
    Debug.LogError("Unobserved task exception: " + e.Exception);
};

This behaviour is so random and it’s breaking a lot of my players game. yes my game is on playstore and players are not happy about it.
Hope this info helps and thanks in advance.

CASE IN-134860
this is the report number.
I just replicated the scenario in small scale where a session is created after an ad being player.

On further debugging i find that its the unity wire services that keep getting failed over and over again.

This is because i am calling create session when a rewarded ad is closed and during that time period the entire unity activity is paused and hence after resuming the activity the wire just lost its websocket.

I tried adding a delay to it but it does not make it any better.
I tried signing out and re authenticating that also does not worked.

Is there a way to restart the services without quitting the app?

Hope this info help and rooting for this issue to be fixed. :slight_smile:

Thank you for doing this. :+1:
I will get QA to assign this to our group and we will start looking into the root cause of why this is happening.