Greetings everyone , i am working on a android multiplayer horror game named blackout and i use distributed authority as my network topology and these are my packages and editor versions,
i used the delay and await_with_timeout function because the lobby creation sometimes stuck on the creation due to wire reconnection. ( this is only for android because of an ad playing before lobby creation, so irrelevant to editor and yes this happens in editor and android build as well but randomly works sometimes).
this same code works absolutely fine a week back but now it gives me this error.
i don’t know whether its the problem in my end or at unity’s.
any help will be appreciated.
Never do that anywhere for service or network related code. Waiting a fixed time is never guaranteed to “fix” anything. There’s always an appropriate event you must listen to since whatever the reason for this or any other Task.Delay may not happen within that hardcoded time. It would also only serve to add artificial delays for users in most cases, ie you’d force everyone to wait 2s even when the task completes instantaneously.
And never use that Delay to match any UI animations or something - service calls can and should comfortably run during any animation, since it would likely take them to complete 1-2s. Those 2s would be awfully long for a UI transition anyway, the sweet spot (and usually mandated by HID guidelines anyway) is between 0.2 to 0.3s.
Exception logging must always be done first, because whatever gets Invoked may in itself throw an exception or delete/destroy the instance where this code runs on.
Moreover, you must not run “regular” code in catch (or be very deliberate about it) since you’re effectively continuing other calls from exception handling, which means any further exceptions will not get caught. A try/catch inside a catch is to be avoided.
It’s best to keep a bool “failed” and then check that afterwards and do any cleanup. This also avoids repeating the same call three times in this code snippet.
Remove that. This is a flawed attempt at creating a custom timeout. Use a cancellationtoken instead, or just rely on the default timeout (recommended).
Your version will not stop the session creation, it will continue in the background. This will cause problems, possibly the one you experience, when trying to create the next session. For instance what if after 20s the Lobby does get created?
In other words. Don’t make service calls interruptable (rather: fired, but then ignored) where they don’t support cancelation. If the default timeout is too long, it may be configurable. If it’s not, you got to live with it. Under normal situations this will never occur. You rely on Unity servers being up 99.99% of all times, while checking a user’s Internet connection right before issuing essential service calls. That would then leave only timeouts due to a rare Unity service failure or the user’s Internet disconnecting just during that Lobby creation.
Thank you for the quick reply, based on this i have refactored my code here’s how it looks now
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>
{
//some vars
},
}.WithDistributedAuthorityNetwork();
try
{
currentSession = await MultiplayerService.Instance.CreateSessionAsync(options);
}
catch (Exception e)
{
Debug.Log("Cant Create a Sesion " + e);
onLobbyCreationFailed?.Invoke(this, EventArgs.Empty);
}
if (currentSession != null)
{
Debug.Log(currentSession.Code);
await setPlayerProps(wantVC);
registerLobbyEvents();
}
}
public async Task leaveSession()
{
if (currentSession == null) { Debug.Log("No Sessions found"); return; }
unregisterEvents();
try
{
await currentSession.LeaveAsync();
}
catch (Exception e)
{
Debug.Log("Cant Leave the session " + e);
}
onLeavingLobby?.Invoke(this, EventArgs.Empty);
onLobbyLeft();
}
I have attached both the hosting and leaving with this improvement the code still return the same error
[Multiplayer]: No join code returned from lobby.
but i found a important thing about this issue. the lobby has a join code but its not returning it properly for some reason and i am not trying to access it inbetween the creation process.
as you could see from the above image it actually has a join code .my guess is something internally is trying to access this for some reason ( i am no expert in this so please correct me if i am wrong)
and hence it fails .
during testing i noticed a pattern where the first 2 times of the creation and the third time always fails and ends up in this error.
Moreover during saving the player properties it shows this
with this i assume multiplayer services created a session that the player is not a part of and i wonder why is that?
and just for testing i created a blank unity project and tried the same and it randomly fails there as well.
i am starting to question my network connection but it seems to work fine.
is there any chance that unity servers are down in my region?
Hiya @Angie777, thanks for the feedback. This looks like a failure to receive a Relay joincode in the pub/sub data sent via Lobby, which is the underlying connection mechanism to join a Distributed Authority session. We’re working on improving the allocations flow this sprint. If you want to DM me more details on your project I can have a look through logs to see what is going on and if we need to add something we can look into it.
Hey , the new project that i used to replicate this error have simple create and leave function like this
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
using System.Diagnostics;
using Unity.Services.Multiplayer;
public class lobby_Manager : MonoBehaviour
{
void Start()
{
UnityServices.InitializeAsync();
AuthenticationService.Instance.SignedIn += () => { UnityEngine.Debug.Log("Signed in!"); };
AuthenticationService.Instance.SignInAnonymouslyAsync();
}
[ContextMenu("Summa")]
public void createLobby()
{
SessionOptions options = new SessionOptions();
options.MaxPlayers = 4;
options.IsPrivate = true;
options.WithDistributedAuthorityNetwork();
MultiplayerService.Instance.CreateOrJoinSessionAsync("MyLobby", options).ContinueWith(task =>
{
if (task.IsCompletedSuccessfully)
{
UnityEngine.Debug.Log("Lobby created successfully!");
session = task.Result;
// You can now use the session object to manage your lobby
}
else
{
UnityEngine.Debug.LogError("Failed to create lobby: " + task.Exception);
}
});
}
ISession session;
[ContextMenu("summa1")]
public void leaveLobby()
{
session.LeaveAsync();
}
}
this always shows up whenever a leaveasync is called, i am not sure whether this is the problem or not but one more thing that points to the cleanup being the problem is that this error only happens after 2 times of lobby creation.
and more over why session.leaveAsync() leaves the session but still stores the session locally eventhough the player is no longer the member of the session and whenever the error occurs the session is actually created but the one who creates cant join the session for some reason.
i confirmed this by creating a public session (which fails with this error ) and have another device join that session and it perfectly joins on that device and then i use the lobby code to join from the actual created device then it joins.
and to make sure this happens because of cleanup i tried creating session back to back with little interval and every time this error occurs.
So my final guessing is the problem is in the cleanup of the session. ( i am no expert in this so please correct me if i am wrong )
Hello,
Any word on this? I have a similar problem. When trying to start a session using CreateSessionAsync, I will occasionally get the same error discussed here ([Multiplayer]: No join code returned from lobby), although it is inconsistent. It happens maybe 15% of the time or so when starting sessions, and doesn’t seem to correlate with anything else in my code. I perform all other startup activities the same way and will usually have no problem starting my lobby, and then randomly will see that error on occasion.
One thing I have noticed is that the Await function takes longer to run when the error occurs. This makes me think that it is a timeout issue. Any help would be great - the inconsistency of this error makes it hard to debug and figure out what is going on under the hood.