Search Unity

UnityWebRequest - timeout

Discussion in 'Scripting' started by jarduz, Jul 10, 2018.

  1. jarduz

    jarduz

    Joined:
    Feb 12, 2015
    Posts:
    7
    Hi everyone,

    I'm trying to use UnityWebRequest and control errors that com with timeout with my rest services, but I can't make it work. Unity docuementation does not describe in a very good way how to use it (here). So this is my code:

    Script #1 - I try to catch the error caused by rq.timeout = 3; but it keeps on holding forever.

    Code (CSharp):
    1.  
    2. IEnumerator regNewDev () {
    3.    var rq = new UnityWebRequest("http://localhost/rest1", "POST");
    4.    rq.SetRequestHeader("Content-Type", "application/json");
    5.    rq.timeout = 3;
    6.    yield return rq.SendWebRequest();
    7.  
    8.    try {
    9.          debug.log (rq.downloadHandler.text);
    10.       } catch (Exception e) {
    11.       Debug.Log (e);
    12.    }
    13. }
    14.  
    Script #2 - I try to use rq.isNetworkError but it keeps on holding forever too.

    Code (CSharp):
    1.  
    2. IEnumerator regNewDev () {
    3.    var rq = new UnityWebRequest("http://localhost/rest1", "POST");
    4.    rq.SetRequestHeader("Content-Type", "application/json");
    5.    rq.timeout = 3;
    6.    yield return rq.SendWebRequest();
    7.  
    8.    if (rq.isHttpError || rq.isNetworkError) {
    9.       debug.log ("Error!!!");
    10.    } else {
    11.       debug.log (rq.downloadHandler.text);
    12.    }
    13. }
    14.  
    Could not find an example to fix this yet. Any sugestion?
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Does it output any debug of your script? Or do you get any error? Is it c# or is hit javascript? Looks like javascript
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    If that is an actual code that you run, you are sending an empty POST request. Is your server ready for that?
    You also don't assign any download handler, so you'll get NullReferenceException when trying to get text out of it.

    I recommend to use higher level API until you are comfortable, in your case use UnityWebRequest.Post().

    As for timeout, it is risky to set it to a very low value and there is also no guarantee of fast timeout either (though in Editor it should timeout quite fast).
     
  4. jarduz

    jarduz

    Joined:
    Feb 12, 2015
    Posts:
    7
    Well it is actually not the complete code I am using. This should be...

    Code (CSharp):
    1.  
    2. IEnumerator regNewDev () {
    3.    cred creC = new cred();
    4.    creC.n = nU.text;
    5.    creC.p = pU.text;
    6.    creC.i = getAndroidID();
    7.    string jsonC = JsonUtility.ToJson(creC);
    8.  
    9.    var rq = new UnityWebRequest("http://localhost/rest01", "POST");
    10.    byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonC);
    11.    rq.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
    12.    rq.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
    13.    rq.SetRequestHeader("Content-Type", "application/json");
    14.    rq.timeout = 3;
    15.    yield return rq.SendWebRequest();
    16.  
    17.    if (rq.isHttpError || rq.isNetworkError) {
    18.       Debug.Log("Error");
    19.    } else {
    20.       Debug.Log("Success!!!");
    21. }
    22.  
    As for setting up timeout = 3 is just for testing propouse. I want to be sure for this to fail, so then I will know how to control it on production.
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    OK, now it looks much better (except that you don't need to cast UploadHandler and DownloadHandler).
    So you don't get neither "Error" nor "Success"?
     
  6. jarduz

    jarduz

    Joined:
    Feb 12, 2015
    Posts:
    7
    Thnaks for the "cast" help.
    About "Error" nor "Success", I tried turning off the REST web service, and when I do that it only keeps on holding for a response, but never fails.

    So that's when the question appears, what is timeout for?, if this is not doing anithing. How can I control waiting response?
     
  7. jarduz

    jarduz

    Joined:
    Feb 12, 2015
    Posts:
    7
    I have another question thet could not find width web page. Is the variable timeout assuming values as seconds or milliseconds?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    From Unity documentation: "Sets UnityWebRequest to attempt to abort after the number of seconds in timeout have passed."
     
  9. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    I'm borrowing this thread for timeout related question..

    I'm am using UnityWebRequest for downloading large files. It works as expected, but I noticed that if I kill my network connection in the middle of a download (by disconnecting the network cable), the UnityWebRequest never returns any error. It just keeps trying to receive data forever. So, I tried applying 10 second timeout for all my requests. But it seems that timeout just kills the download after 10 seconds, so timeout is not suitable for my purpose.

    My question is, how could I detect that the connection to the server has been lost in the middle of a download with UnityWebRequest?

    @Aurimas-Cernius
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    How long have you been waiting? It should eventually return an error. The 10s timeout is very low, on a bad network connection it can fail even when download is actually happening. But it will kill the download anyway.
     
  11. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    I waited for like 5 minutes after plugging out the network cable, but UnityWebRequest returned no error. Shouldn't that be enough for a timeout? What is the internal value for UnityWebRequest to wait in case the connection gets lost?

    I ended up monitoring the downloadProgress variable with 0.25 second intervals and then calling Abort() if there is no progress for 120 consecutive checks (which means 30 second wait time). It works, but feel a bit hacky. It would be nice if the UnityWebRequest would automatically abort in case of lost connection.
     
    EirikWahl likes this.
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Actually, I've checked and it does not timeout by default. It will timeout if connection can't be established, but not when download is in progress. So you have to set the timeout property for it stop.
    Why timeout is not suitable for you?
     
  13. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    Because if I set the timeout property for the UWR, it just aborts the download process after the specified amount of seconds (since sending the request), even if the file is still downloading. What I would need is a timeout that would start in case the connection is lost in the middle of the download process and would then raise an timeout error if the connection does not recover during the specified time.

    Sure, I could set the timeout to like 1 hour, but even then there is a risk that it just aborts an otherwise successfull download process. The files I am downloading can be several gigabytes in size.

    @Aurimas-Cernius Can you confirm that UWR timeout is intended to work as "max total time this request can take and force abort when elapsed" -kind of thing, rather than "max time to wait for another byte to be received in the middle of download?" -kind?
     
    Last edited: Nov 7, 2018
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    In such case what you need is to periodically check the downloadedBytes property and abort on your own when no progress has been made for certain amount of time.
     
  15. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    No one answered this question, I have the same issue in Unity 2019.4.22f1.
     
    chexPK likes this.
  16. Nimrod_Harel

    Nimrod_Harel

    Joined:
    Oct 27, 2020
    Posts:
    1
    I'm also curious about that.

    Specially, we're encountering a case of using UnityAssetBundleRequest which prints a "failed to decompress data for the asset bundle" error but the callback is never triggered.
    We ended up setting up another timer to handle a case where we never get any result (error / success) and than handle re-downloading the bundle internally.

    Would be great if Unity can guarantee the on complete callback will always be called in case of direct failures.
     
    chexPK likes this.