Client sometimes never connects after creating a session

I use Distributed Authority. Below is the function that I use to create a session.

private void Awake()
{
  
  if (Instance != null && Instance != this)
  {
    Destroy(gameObject);
    return;
  }

  Instance = this;
  DontDestroyOnLoad(gameObject);

  networkManager.OnSessionOwnerPromoted += OnSessionOwnerPromoted;
  networkManager.OnClientDisconnectCallback += OnClientDisconnect;
  networkManager.OnClientConnectedCallback += OnClientConnected;
  networkManager.OnTransportFailure += OnTransportFailure;
  networkManager.OnClientStopped += OnClientStopped;
}

public async Task CreateLobbyAsync(bool isPrivate)
{
  OnCreateLobbyStarted?.Invoke(this, EventArgs.Empty);

  SessionOptions options = new SessionOptions
  {
    MaxPlayers = GameMechanics.MaxPlayerCount,
    IsLocked = false,
    IsPrivate = isPrivate,
    PlayerProperties = GetPlayerProperties()
  }.WithDistributedAuthorityNetwork();

  try
  {
    activeSession = await MultiplayerService.Instance.CreateSessionAsync(options);

    if (activeSession == null)
    {
      Debug.LogError("Failed to create session: returned null.");
      OnCreateLobbyFailed?.Invoke(this, EventArgs.Empty);
      return;
    }

    Debug.Log($"Session {activeSession.Id} created! Join code: {activeSession.Code} {activeSession.Host} {activeSession.PlayerCount}");
  }
  catch (Exception e)
  {
    Debug.LogError($"Session creation failed: {e}");
    OnCreateLobbyFailed?.Invoke(this, EventArgs.Empty);
  }
}

As you can see, it’s pretty generic. But sometimes, seemingly at random, the player client gets stuck at trying to connect after creating the session. Normally, when everything works fine, I receive events from OnClientConnected and OnSessionOwnerPromoted after creating the session. But sometimes, that never happens. I wait for couple of minutes and client disconnects after reaching maximum connection attemps. I’ve also tracked Transport events. NetworkEvent.Connect is also never fired from the Transport on NetworkManager when this bug happens. I’ve spent days but I couldn’t even consistently reproduce the issue. I tried different pc, wi-fi etc. but I’m pretty sure that the issue is not about my connection or firewall. This is also what my connection manager looks like. I’ve tried everything I can think of and still couldn’t the problem.

We call this a defective singleton. The duplicate nevertheless exists for one frame and can cause problems, especially if it has other scripts on it or is found by type or name in other scripts.

If the issue occurs any time but the first session this might be a potential source of issues.

More likely the problem is in the code calling the CreateLobbyAsync. Is it properly awaited? Is it potentially calling that method multiple times? Could there be an already-created Lobby from the last session? Especially if closing and recreating the Lobby happens within the same frame.

So the thing is I tried the minimal setup example Unity team themselves uploaded inside Netcode package and even it has the same issue. So I don’t know if the problem is even fixable. The issue is either with the internet service I use, the specific Unity version I am using or the Netcode package itself. Regardless. this is how I call create session just in case.

public async void OnClickCreateLobby()
{
    await CreateLobby(isPrivate);
}

public async Task CreateLobby(bool isPrivate)
{
    await SessionManager.Instance.CreateLobbyAsync(isPrivate);
}

Aren’t we supposed to use “Distributed Authority Transport” instead of Unity Transport if topology is DA?