Monitor Wait frame freeze on disconnect

Options
Hi, y'all!

So I've tried to disconnect from the Photon server and have deep profiled what actually could've caused the frame freeze we're experiencing.

Now I see that there is a function called "Monitor.Monitor_wait()" which takes 1003ms (and thus the frame freeze)
I have enclosed the Profiler statistics for that current frame and hope you can help me with it.

Monitor-Wait-Issue.png

My question is if it's possible to fix this issue and if so, how exactly?

Looking forward to hearing from you!


- Skittluier

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited February 2021
    Options
    Hi @Skittluier,

    What PUN version is this?
    What unity version is this?
    If I understood correctly from the screenshot you are profiling a Windows build client?
    Is it built in "development mode"?
    What Scripting Backend and .NET version did you use?
  • Skittluier
    edited February 2021
    Options
    Hey @JohnTube ,

    Sorry for the missing info.

    Photon version: 2.25
    Photon library: 4.1.4.7
    Unity version: 2020.1.11f1
    Windows build client: That's correct!
    Dev mode: Yes.
    Scripting back-end: IL2CPP
    API compatibility level: .NET Standard 2.0
  • Tobias
    Options
    Thanks for the precise description and callstack.
    In that tree, SocketUdp.Disconnect() is the last piece of code that we wrote. We call socket.Close() and somewhere inside, this causes the freeze.

    I would guess we can work around this but imo, this should not happen in the first place.

    You could try one option yourself: There are multiple variations of the Photon3Unity3d.dll in the project. The one in PhotonLibs\ is usually used. Use the inspector to build a version with the folder PhotonLibs\netstandard2.0\ (and disable export of the one in PhotonLibs\).
  • Tobias wrote: »
    Thanks for the precise description and callstack.
    In that tree, SocketUdp.Disconnect() is the last piece of code that we wrote. We call socket.Close() and somewhere inside, this causes the freeze.

    I would guess we can work around this but imo, this should not happen in the first place.

    You could try one option yourself: There are multiple variations of the Photon3Unity3d.dll in the project. The one in PhotonLibs\ is usually used. Use the inspector to build a version with the folder PhotonLibs\netstandard2.0\ (and disable export of the one in PhotonLibs\).

    Hi @Tobias !

    Thanks for the swift response. The PUN Support is super fast. :D

    Anyway, I will come back on this and will tell you how it went!

    Thanks again.
  • Tobias
    Options
    Are you connecting to the cloud or a local server? If it's the latter, try the Cloud or running the client and server on different machines.
  • Tobias
    Options
    With the Photon Cloud, I can not reproduce this currently in 2020.1.12f1 and PUN 2.27.
    Is this in the Editor or only in builds (with IL2CPP)?
  • Skittluier
    edited February 2021
    Options
    Tobias wrote: »
    With the Photon Cloud, I can not reproduce this currently in 2020.1.12f1 and PUN 2.27.
    Is this in the Editor or only in builds (with IL2CPP)?

    We are connecting to the Photon Cloud and it only happens to builds with IL2CPP.

    We also do not experience this issue in the Editor, which is quite odd.
  • Tobias
    Tobias admin
    edited February 2021
    Options
    I built with IL2CPP for Win64 bit and still can't reproduce this. It may be an issue with the Unity version that happens to be fixed in the next.

    Edit:
    Or of course, I did not trigger the exact same situation. In my test client, I am connected, send and receive events and can press a "Disconnect" button anytime.
  • Tobias wrote: »
    I built with IL2CPP for Win64 bit and still can't reproduce this. It may be an issue with the Unity version that happens to be fixed in the next.

    Edit:
    Or of course, I did not trigger the exact same situation. In my test client, I am connected, send and receive events and can press a "Disconnect" button anytime.

    Right, we will try updating to the next version of Unity next week and will tell you the results.

    Thanks for the effort @Tobias !
  • Skittluier
    edited February 2021
    Options
    @Tobias

    I forgot to mention that this issue only occurs when there are instabilities between the client and the Master Server.

    So when I'm blocking the IP Address 92.38.154.80 (< EU region), connect and eventually disconnect, the lag spike occurs.

    I hope this provides sufficient information to reproduce the issue.

    EDIT: Right now trying to reproduce this same method in an empty project. Will tag you again (and also send you the project, if you want) if I have an update.
  • Skittluier
    edited February 2021
    Options
    Hi @Tobias

    It seems that I've had time left in my week and I've checked if it also occurs in a new project with Photon in it. And it does.

    performance-issue.png

    I would like to send you the empty Unity project with my stuff in it. This also includes the profiler data. (Assets/Photon Disconnect performance issue.data)
    Click here to download the project.

    Next to that, you need to have a piece of software to simulate network instabilities for certain IP addresses. In this case, I've used Clumsy, which can be downloaded here: https://jagt.github.io/clumsy/download

    Use the following configuration to simulate the EU servers' instability:

    clumsy.png

    Hope to hear from you soon!
  • Tobias
    Options
    So, this need an IL2CPP build, which connects to an external server.
    Then, simulate network loss and at (about) the same time, call Disconnect?

    I will try to repro this these days but can't promise anything yet. I guess Unity's IL2CPP Socket implementation might get stuck while sending. It is a blocking operation which normally finishes quick enough to not matter.

    You could try to use the Async Sockets API. Simply apply the SocketUdpAsync like this before you connect:
    PhotonNetwork.NetworkingClient.LoadBalancingPeer.SocketImplementationConfig[ConnectionProtocol.Udp] = typeof(SocketUdpAsync);
    

    Thanks for all the info and the project. Please leave this up, so I can take a look.
  • Skittluier
    Options
    @Tobias

    We've just tested this line of code and as far as we know, it's working perfectly! We don't have any frame freezes anymore.

    Thanks a lot for your support!
  • Tobias
    Options
    Glad this helped.
    Can you confirm my summary of how to repro the case in PUN 2.25? I still need to take a look.
  • Skittluier
    Options
    Tobias wrote: »
    Glad this helped.
    Can you confirm my summary of how to repro the case in PUN 2.25? I still need to take a look.

    @Tobias
    When you disable your internet connectivity for the region master server you would like to connect to (In my case the EU server) and then disconnect, you will receive a giant lag spike.

    So to sum it up:

    1. Disable connectivity between you and the regional server. (EU)
    2. Connect with the Name Server -> Master Server
    3. Disconnect while it's connecting with the Master Server.


    Hope this helps you! If you have any questions, then you of course can respond to us.
  • Tobias
    Options
    I can reproduce the issue and confirm: This is an internal issue in Unity's Socket implementation, which should possibly get reported as bug. I will see if I find the time for this.

    The C# async socktet implementation doesn't have the issue and neither has our own native sockets plugin. I will think about including these in the future.