Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UnityWebRequest fails after internet loss/regain

Discussion in 'Scripting' started by push-pop, Oct 9, 2020.

  1. push-pop

    push-pop

    Joined:
    Jun 29, 2017
    Posts:
    28
    Hi all,

    I'm using UWR to get urls for assets my app needs to download. In the QA process we've come across a very specific situation in which the app cannot recover and I'd be interested if anyone has some ideas...

    We are seeing the same behaviour on iOS and Android. Not sure if it happens in the editor (I can't test this particular case since I don't have access to the router I'm using.)

    My getURLS coroutine looks like this:
    Code (CSharp):
    1.     public static IEnumerator GetBundleURLs(Action<string, BundleURLCollection> callback)
    2.     {
    3.         var uri = Path.Combine(_config.Host, _config.Endpoint);
    4.         Debug.Log(uri);
    5.         using (var req = UnityWebRequest.Get(uri))
    6.         {
    7.             req.timeout = URL_REQUEST_TIMEOUT;
    8.             yield return req.SendWebRequest();
    9.  
    10.             if (req.isNetworkError || req.isHttpError)
    11.             {
    12.                 callback?.Invoke(req.error, null);
    13.                 yield break;
    14.             }
    15.             Debug.Log(req.downloadHandler.text);
    16.  
    17.             var urls = req.downloadHandler.text;
    18.             var bundleURLCollection = JsonUtility.FromJson<BundleURLCollection>(urls);
    19.  
    20.  
    21.             callback?.Invoke(null, bundleURLCollection);
    22.         }
    23.  
    24.         yield return null;
    25.     }
    Then the app goes on to take that response and download the appropriate bundles.


    So the repro goes like this:

    1) Start asset downloads as normal
    2) Before all downloads finish, unplug internet from router (leave router up, so phone is connected to router, but with no internet)
    3) Timeout on bundle download. (This calls UWR.Abort() on all remaining downloads.)
    4) Reconnect internet to router
    5) Try downloads again
    6) GetBundleURLs times out, even though the phone has internet.

    HERE"S THE WEIRD THING:

    If the phone gets some kinda interruption (a push notification from slack, or goes to background and returns) then the downloads work fine again. It seems like the "connected but no internet state" is getting stuck, and the UWR is not making it to the web for some reason.

    Can anyone think of any reason this might happen?

    Thanks!
     
  2. push-pop

    push-pop

    Joined:
    Jun 29, 2017
    Posts:
    28
    OK so as soon as I posted this we got a solution.

    For some reason setting UWR.useHttpContinue = false allowed us to recover from this. Still unsure/curious why. It seems like it could be related to some kind of caching based on these forum posts...

    https://forum.unity.com/threads/unitywebrequest-keeps-caching.519489/

    If anyone has any clues WHY this works, I would be curious to know for my own knowledge. The server is AWS lambda so possibly it is using that header? I certainly didn't write anything to consume it.

    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest-useHttpContinue.html