Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

UnityWebRequests on iOS sometimes get stuck indefinitely even with timeout set

Discussion in 'Scripting' started by nilsdr, Nov 26, 2020.

  1. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    319
    been a while but I'm pretty sure the invalidation callback never once fired, going to try again just to make sure.
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    319
    Ok, just ran our test case again to make sure, like so:

    Code (CSharp):
    1. @implementation UnityWebRequestDelegate
    2. - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
    3. {
    4.     NSLog(@"NETWORK ERROR: %@", error);
    5. }
    6. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(nonnull NSURLResponse *)response completionHandler:(nonnull void (^)(NSURLSessionResponseDisposition))completionHandler
    7. {
    8.     NSLog(@"NETWORK OK: %@", response.URL);
    9.     [self handleResponse: response task: dataTask];
    10.     completionHandler(NSURLSessionResponseAllow);
    11. }
    12.  
    I get a network ok for the first 20 or so requests, then it just stops working. I never see NETWORK ERROR in the logs, so didBecomeInvalidWithError is not called
     
    halinc and altkeymathieusavard like this.
  3. luke_avm

    luke_avm

    Joined:
    Mar 26, 2019
    Posts:
    10
    Saicopate, mae0510 and nilsdr like this.
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,768
    One possible workarounds to this issue is to add this line to UnityWebRequest.mm where the webOperationQueue is created:
    webOperationQueue.qualityOfService = NSQualityOfServiceUserInteractive;
     
    Saicopate, halinc and re-cheid like this.
  5. GoldARVR

    GoldARVR

    Joined:
    Oct 28, 2019
    Posts:
    6
    For everyone having this issue, I've replaced all my UnityWebRequest call by HttpClient call and it works. It was a lot of work and I had to learn a lot about HttpClient, but since there is no vision on the correction of this bug, I had to do it.

    And, well, HttpClient is really good. I don't know what are the pros of using UnityWebRequest since HttpClient can do the same if not better ?
     
  6. re-cheid

    re-cheid

    Joined:
    Apr 10, 2017
    Posts:
    27
    Thanks for the information.
    Is this just a temporary workaround, or is this the solution which will be integrated in a future release of Unity?

    //EDIT: I've tried this and it SEEMS TO FIX the problem! Thanks! Hope to see the fix in an official release soon, so we're able to use cloud builds again.
     
    Last edited: Jan 14, 2021
    halinc likes this.
  7. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    319
    We use some Unity specific functionality like assetbundle caching, switching to httpclient isnt viable.
     
  8. Carlogeno

    Carlogeno

    Joined:
    Dec 11, 2016
    Posts:
    8
    I've changed all plugins code with HttpClient :-( ... this is an time expensive workaround... not a final solution :-(
     
  9. halinc

    halinc

    Joined:
    Feb 24, 2019
    Posts:
    2
    seems to work for me as well, thank you! added it in the method UnityCreateWebRequestBackend:
    Code (CSharp):
    1. webOperationQueue = [[NSOperationQueue alloc] init];
    2. webOperationQueue.name = @"com.unity3d.WebOperationQueue";
    3. webOperationQueue.qualityOfService = NSQualityOfServiceUserInteractive;
    looking forward to see this in an official bugfix update
     
    jgmakes likes this.
  10. ANTONBORODA

    ANTONBORODA

    Joined:
    Nov 16, 2017
    Posts:
    13
    Looking at the documentation for qualityOfService property, this doesn't seem to be a correct fix, rather a workaround.

    I can be mistaken because I can't fully understand why the problem happens in the first place, but setting a higher priority to a queue doesn't seem like a "correct" fix to me, rather it's hiding the underlying problem. Again, I can be wrong.
     
    Last edited: Jan 15, 2021 at 8:01 AM
    apiotuch_unity and re-cheid like this.
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,768
    It seems that
    webOperationQueue.qualityOfService = NSQualityOfServiceUtility;
    is enough. I could not reproduce the issue after setting this quality of service.
     
  12. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    319
    So what is causing it? Why does a functionality unrelated to networking stop it from working?
     
  13. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    46
    We are currently testing more, however it seems like this fixed the issue for our team as well.
     
  14. re-cheid

    re-cheid

    Joined:
    Apr 10, 2017
    Posts:
    27
    Did anyone try this with iOS 14.2.1 (exclusive to iPhone 12 and iPhone 12 Pro)? Seems like one of our customers still has the problem

    //EDIT: not relevant, sorry, problem was somewhere else
     
    Last edited: Jan 19, 2021 at 11:29 AM
  15. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    46
    @re-cheid We updated everything to 14.3 (XR, MX Max, iPhone 7) and it works fine. Can the customer update their OS and see if there is still an issue?
     
  16. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,768
    iOS 14.3 does not solve this issue.
    It seems the cause is that webOperationQueue does not fully satisfy the expectations of NSURLSession. Passing nil as delegate queue when creating session does solve the issue for me (the NSURLSession creates it's own queue in such case). I think that's the way to go with fixing this.
     
    nilsdr likes this.
  17. re-cheid

    re-cheid

    Joined:
    Apr 10, 2017
    Posts:
    27
    False alarm! it was a different issue with his environment.
     
    nilsdr likes this.
  18. FOKSlab

    FOKSlab

    Joined:
    Jun 27, 2017
    Posts:
    10
    Just FYI I used HTTPClient from ClaytonIndustry (as advised before in this thread) to fix my http request issue on iPhone (iOS 14.3), and it does the job... but I still have the issue on iPad OS 14.3 (iPad Pro) (InvalidServer error after sending the request : message "unknown" is returned ). Any clue why this fails on iPad OS ?

    EDIT : I forgot to say I upgraded my project to Unity 2020.2
     
  19. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    46
    I meant our devices were all updated to 14.3 and using the webOperationQueue.qualityOfService = NSQualityOfServiceUserInteractive fix. We did not test with 14.2 or 14.2.1 because that would require us to downgrade the device OS which is more work than it is worth.
     
unityunity