Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Remote config freezing issue

Discussion in 'Unity Remote Config' started by BrianND, Apr 3, 2024.

  1. BrianND

    BrianND

    Joined:
    May 14, 2015
    Posts:
    82
    This is the code in the unity remote-config-runtime
    Unity.Services.RemoteConfig.CheckForInternetConnection

    to check if you are connected online. If you have a bad connection (I simulated 100% packet loss) this code will block the main unity thread. I wonder if there is a better way to do this

    I only noticed this because my iOS app was crashing because of the launch watchdog.

    Does this download the entire unity website just to check the online status??

    Code (CSharp):
    1. public static bool CheckForInternetConnection()
    2. {
    3.   try
    4.   {
    5.     using (var client = new WebClient())
    6.       using (client.OpenRead("http://unity3d.com"))
    7.         return true;
    8.   }
    9.   catch
    10.   {
    11.     return false;
    12.   }
    13. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,896
    Not surprising.

    Effectively, you are simulating "Internet is available" but "there won't be any traffic going through". That is not a realistic state.

    Either Internet is completely unavailable, as in: no connection can be made. Or at least some packets need to be allowed to arrive. Try with 99% packet loss but even that is highly unrealistic.

    Check the Network Simulator for realistic rates of packet loss. I don't think it goes above 20-30% even for 2G connections.
     
  3. BrianND

    BrianND

    Joined:
    May 14, 2015
    Posts:
    82
    Sorry if I was unclear but the packet loss was just recreate a live bug.

    This has actually been happening to a tester on a production build with no simulation.

    I looked at this forum post and other people have been having performance/connections issues with this.
    https://forum.unity.com/threads/authentication-hang.1414722/

    One other post that looks like you replied to :)
    https://forum.unity.com/threads/do-...upport-a-proxy-software.1438993/#post-9026761

    I'm not too versed in WebClient but since that function reads the bytes from the Unity website it might cause some lag on bad connections since we are loading the Unity3d website just to check a connection. I was thinking that they could just use an async function of that.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,896
    OpenRead shouldn't download any bytes, since it just opens a stream. Same way you open a FileStream but this won't read data from disk unless you read from the stream.

    Apparently, the default timeout is 100s which I guess is too long for the watchdog. Someone ought to have thought about this and set a reasonable timeout before calling OpenRead. If you can modify that code I would give it a timeout of 5s at most.
     
  5. BrianND

    BrianND

    Joined:
    May 14, 2015
    Posts:
    82
    Yea I think the main issue is it can block the main unity thread. Lots of people will run this in Start or Awake and that will lock up loading. I just changed it to an openreadasync call with a cancellation task after 5 seconds. I wonder if you can also just read a header.

    If OpenRead doesn't read bytes I wonder why it's slower on bad connections

    https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.openread?view=net-8.0
    > Opens a readable stream for the data downloaded from a resource with the URI specified as a String.

    According to the docs it says open stream for the data "downloaded". So I wonder if it means that it downloads it first then opens the stream.