PUN disconnects in UWP build when application moves to background.

This has never happened in the editor and happens consistently in the build. Looking at the log file, the cause of disconnection is ServerTimeout and when I use PhotonNetwork.ReconnectAndRejoin(), I get game-breaking issues that can't be worked around (meaning that basically all of the object ownership gets messed up, and there are duplicate player objects and weirdest of all there are multiples of remote players as well, which should only be spawned once, by the master client, which, upon rejoining, you are not). I've also modified the source code of Photon too much to make it reinstallable (only changed some instantiation and scene loading code to work with the Addressables system, no connection code). I've also tried all the fixes suggested here, including some suggested for WebGL and mobile platforms. By this I mean that there is silent audio playing, I have all the needed UWP capabilities enabled and I have multiple Application.runInBackground = true; calls. I am also using int.MaxValue for PhotonNetwork.KeepAliveInBackground and RoomOptions.PlayerTtl, but it doesn't seem to change anything.

Room Settings:

Room creation + joining works fine, so this isn't too important I think, but still, it could be important.

RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = gameModes[sI].numberOfPlayers;
roomOptions.PlayerTtl = int.MaxValue;
roomOptions.CustomRoomProperties = new Hashtable
      {"gmn", gameModes[sI].gameModeName },
roomOptions.CustomRoomPropertiesForLobby = new string[] { "gmn" };
PhotonNetwork.CreateRoom(null, roomOptions);

Photon Network Settings:

Really just this:

PhotonNetwork.KeepAliveInBackground = int.MaxValue;

Disconnection Code:

public override void OnDisconnected(DisconnectCause cause) {
    if (AlertController.instance != null) AlertController.instance.Alert("DIsCoNnECteD");

public override void OnErrorInfo(ErrorInfo errorInfo) {
    if (AlertController.instance != null) AlertController.instance.Alert("ErorRe");

private void AttemptRejoin() {
    bool attempt = PhotonNetwork.ReconnectAndRejoin();
    Debug.Log("Rejoin Attempted at " + Time.time + " with result " + attempt);
    if (attempt) {
    } else Invoke("AttemptRejoin", rejoinTime);

private void OnApplicationPause(bool pause) {
    if (pause) {

And the log file shows "aaaaa" and then "ServerTimeout" and then when the application resumes focus it shows "Rejoin Attempted at {} with result True", and after a short delay it shows the game (with ownership of scene objects (like towers) messed up and there are two objects for every player, one of which is at (0, 0, 0), the other object is in the correct place (if it's remote), or if it's the local player then one object is at spawn (sometimes the wrong one but at least it's spawned). I am using UserIds instead of ActorNumbers for team allocation to deal with rejoins and disconnections (I know it's not the best way), so some of the rejoin is ok but it would be better if the game didn't disconnect when the application disconnects (although if somebody can tell me why there are player objects at (0, 0, 0) that'd be cool too).

Project Settings:

Unity 2021.3.10f1 LTS

PUN version: 2.41

PUN Settings: RunInBackground: true, No dev region set, No fixed region set

Any help is appreciated, I've been stuck trying to make it not disconnect in background and also trying to fix the reconnection, help with either of those issues would be helpful. If you need more info do tell me.


  • Couldn't find a solution, so I'm now porting my entire project over to Mirror Networking (it's gonna take a while)

  • Sorry for the late reply.

    I didn't test this myself but .. UWP might even be one of those platforms that don't allow network connections when the app isn't in focus. I might look into this these days.

    In that case, you would need a reconnect option, no matter the API you use.

    I am sure that the issues on reconnect can be sorted but it seems you are moving on, so .. not sure if it makes sense to chime in anymore.

    In any case: All the best.

  • RandomLetters
    edited October 2022

    Thank you, Tobias! I looked into it and it seems you are right (removed "photon" "pun2" and "unity" from my searches). I think I found a solution that seems viable (https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession?view=winrt-22621).

    I guess it didn't occur to me that windows would be like that. I'll do some more research and figure out how other games do it, but after spending so much time trying to make mirror work I am determined to have a viable system working with Mirror, PlayFab, and Epic Online Services (I'm actually really proud of the decent amount of stuff that is working correctly so far, which is everything except for the actual connection to a host but I think I am close to making that work as well).

    Thanks again for answering, your help is much appreciated!