NetworkManager.Singleton.StartClient() failed to connect sometimes

I am using Unity Relay to make a multiplayer game. Everything works fine except that sometimes the client can’t connect to the host even if there are no errors. I set NetworkManager’s log level to Developer to see what happened, but it only prints these when failed:

It didn’t print [Netcode] Connected (which should have printed when successful). This only happens like 5% of the time and can be “fixed” by reconnecting (which is still kinda annoying). I once opened up 3 instances simultaneously (1 host, 2 clients), and two of them (1 host, 1 client) connected, the last one didn’t. Here is the package version I am using:

Unity 2022.3.18f1
NGO 1.7.1
Relay 1.0.5

I will leave the code here for more reference.

EDIT:

After waiting for a while when the connection failed, it finally printed more things:

However, even reconnecting didn’t work this time. It cannot find the join code (based on this thread , I guess it is another bug).

9828324–1413252–RelayManager.cs (2.03 KB)

I think I found the solution. This may not be a bug, just the instability of the internet. Thanks to this post , I ended up subscribing to OnClientDisconnectCallback and switched back to the menu scene when it triggered. I may also adjust the Connect Timeout MS and Max Connect Attempts to make it switch back faster.