Exception on Photon Client

Hi there, I've been using Photon TurnBased SDK for my project and it seems that Photon Client still receiving events even after a few seconds the app went to background, but not dispatched as LoadBalancingClient.Service() is not called. Then after resume the app's first call to LoadBalancingClient.Service() would cause the client to disconnect while processing these events received while in the background. The problem is that LoadBalancingClient.CurrentRoom is set to null after disconnect, so these queued events accessing CurrentRoom property (e.g EventCode.Join) would cause a NullReferenceException. Here is my stack-trace log:
NullReferenceException: Object reference not set to an instance of an object
at ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient.OnEvent(...PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:1900)  
at *.*.*.OnEvent(Unknown Source)
at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback(Unknown Source)
at ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands(Unknown Source)
at ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands(Unknown Source)
at ExitGames.Client.Photon.PhotonPeer.Service(Unknown Source)
at ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient.Service(...PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:634)
at *.*.*.Service(Unknown Source)
at *.*.*.Update(Unknown Source)

I couldn't find any good solution to fix this. Please help!

NB: Please excuse my bad English ;)


  • Tobias
    We don't dispatch events while in background by design. Or better: Unity Update() isn't called, so PUN doesn't dispatch them while trying to keep the connection.
    Usually, the events from inside a room should be skipped when you drop out due to a timeout. A timeout disconnects you and that clears all incoming queues.

    Maybe you over-interpreted this. Let's check the facts.

    When I look at line 1900 in LoadBalancingClient, the code has nothing to do with the current room.
    Can you copy and paste the code that's affected? I need to see what exactly happens in that line.
  • My bad, it turns out I have been using Photon TurnBased SDK all the time! Should I upgrade to to see if this problem still exists?
    Yeah, I know events shouldn't be dispatched while in background. But it seems that incoming queues doesn't get cleared after timeout disconnect or it was actually a bug in

    Here is the screen shot of my editor showing LoadBalancingClient.cs (
  • Tobias
    Yes, please update.
    All incoming queues get cleared when a timeout happens. It must be something else.
  • It still happens in and here is the stack trace:
    NullReferenceException: Object reference not set to an instance of an object
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/AssetStore/PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:1921)
    CoopSession+InternalLoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at ***)
    ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte[] inBuff)
    ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands ()
    ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands ()
    ExitGames.Client.Photon.PhotonPeer.Service ()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient.Service () (at Assets/AssetStore/PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:655)
    CoopSession.Service () (at ***)
    CoopManager.Update () (at ***)

    I don't know but I can't think of other possible case as it's really easy to reproduce the problem both in Unity Editor and smartphone devices. All events received in background still get dispatched on resume while being disconnected due to timeout.
  • UPDATE: this is the code for CoopSession+InternalLoadBalancingClient.OnEvent().
    		public override void OnEvent(EventData photonEvent)

    The first thing it does is to call base.OnEvent() which is LoadBalancingClient.OnEvent
  • Tobias
    I am clueless why you get an event after the Timeout and Disconnect.
    Can you share your project? Zip and upload it somewhere (Dropbox, e.g.) and mail us to: developer@exitgames.com.
  • Tobias
    Could you also please log something in CleanCachedValues()? The callstack of that method would be interesting to see how the disconnect got triggered. There are various reasons. Maybe only one has this behaviour.
  • Sorry for late reply. I can't send the project due to agreements with our clients. But I don't think you will need to see our project as it turned out that this also happen in your Memory Game sample project。Here is the stack trace:
    NullReferenceException: Object reference not set to an instance of an object
    MemoryGameClient.LoadBoardFromProperties (Boolean calledByEvent) (at Assets/Demo/MemoryGameClient.cs:315)
    MemoryGameClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Demo/MemoryGameClient.cs:158)
    ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte[] inBuff)
    ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands ()
    ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands ()
    ExitGames.Client.Photon.PhotonPeer.Service ()
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient.Service () (at Assets/LoadbalancingApi/LoadBalancingClient.cs:634)
    MemoryGui.Update () (at Assets/Demo/MemoryGui.cs:139)

    And here is the stack trace of CleanCachedValues() in our project:
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:CleanCachedValues() (at Assets/AssetStore/PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:1427)
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:OnStatusChanged(StatusCode) (at Assets/AssetStore/PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:1805)
    InternalLoadBalancingClient:OnStatusChanged(StatusCode) (at ...InternalLoadBalancingClient.cs:276)
    ExitGames.Client.Photon.LoadBalancing.LoadBalancingClient:Service() (at Assets/AssetStore/PhotonCloudTurnBased/LoadbalancingApi/LoadBalancingClient.cs:655)

    Please look into this matter as soon as possible. Thanks
  • JohnTube
    JohnTube ✭✭✭✭✭

    I think that your problem is linked to this topic. My solution was to implement a Property for the LoadBalancingClient field and in its getter always check if its null and create a new one if so. I know it's a dirty hack but it's better than nothing.
  • I don't know if it's the same or not but I don't think it ever happened to me that LoadBalancingClient object becomes null. The problem is that when client get disconnected, LoadBalancingClient's CurrentRoom properties is set to null by design but received events are still getting dispatched afterwards.
  • Still no answer?
  • Tobias
    I am sorry, I thought the error is no longer relevant. I tried to repoduce this and had some success once, but then couldn't do it another time.

    I think I understand the reason for the problem but couldn't decide if my solution would be OK for everyone or a breaking change with lots of follow up issues for others.
    The problem is in the lower levels of the API. We have a queue for some actions and that is not cleared when a timeout happens. I could clean it but risk missing some relevant info, too.

    You always call Service()? Never SendOutgoingCommands() directly?

    I had a hard time reproducing the issue, when I tried last. I don't know how the workflow would be, e.g. for the Memory Demo. Can you describe that in some more detail, so I have a good chance to get this error?