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

Question How to Catch Addressables AsyncOperation download errors

Discussion in 'Addressables' started by petera1980, Jun 16, 2020.

  1. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    Hello,
    I mark whole scene as addressable and I use this code to download and load scene from server.
    Scene is about 30mb.

    Code (CSharp):
    1. AsyncOperationHandle ao = Addressables.LoadSceneAsync(scene1, UnityEngine.SceneManagement.LoadSceneMode.Single);
    2.  
    As say user's mobile have lost the internet connection during download or at the beginning.

    In this case Addressable throw the exception " Scene 'Assets/Scenes/Level2.unity' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded."

    This means that game freezes and need to be restarted again. I can't show any message to user that an error has occurred.

    The questions is how to handle Addressable downloading errors ?

    "Completed+=" event don't work because error occurs before it fired so I can not check AsyncOperationHandle Status.

    I can check internet connection before user start downloading but I need better way was to catch Addressable exceptions when they are throws

    Please advice
    Thank you
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,795
    Let me flag this for the team to have a look. Which version of Addressables are you using?
     
  3. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    I use 1.8.4 and Unity 2019.4
     
  4. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,795
  5. petera1980

    petera1980

    Joined:
    Jan 29, 2016
    Posts:
    55
    ok I will report it
     
  6. Spabbage

    Spabbage

    Joined:
    Feb 15, 2015
    Posts:
    37
    Is there any known workaround to find out if a request has failed in this way?

    P.S. If not, is there a bug report to track?

    Thanks :)
     
  7. wanted748

    wanted748

    Joined:
    Mar 11, 2018
    Posts:
    11
    Any updates on this please?
     
  8. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,795
    @petera1980 Where did QA land with your bug report?
     
  9. toto007

    toto007

    Joined:
    Jul 18, 2014
    Posts:
    33
    I've same problem. I can't handler error downloading/loading with Addressables.LoadSceneAsync. Have you some suggestion on solution?
     
  10. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    The problem still persists in the Addressables 1.17.17.
    The ".Completed" handler simply not called if the asset bundle containing the scene is failed to load (in any case: any HTTP error, or no Internet connection). The only two cases when the handler is called are: if the loading succeeded and if the resource key is invalid.

    That's SO ANNOYING that Addressables has so many bugs and is so unstable.
     
    Last edited: Apr 16, 2021
  11. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    I've spent all the day long chasing this bug and have found the root of the problem. I've sent a bug report to Unity Technologies and for those who need the fix urgently here is the explanation and the solution.

    1. What is the problem:

    In the behaviour of SceneManager.LoadSceneAsync() is broken. If a Scene requested to load is not available, the method will return null instead of AsyncOperationHandle.

    This crashes Addressables.LoadSceneAsync(), specifically - SceneProvider.cs in method void IUpdateReceiver.Update(float unscaledDeltaTime) because the value of m_Inst.m_Operation happens to be null. Which leads to handle.Completed handler never called and app being stuck (there is no way to avoid this or handle the exception from outside). The only solution as of now is to patch SceneProvider.cs of Addressables manually to handle null result there.

    Moreover, the documentation here clearly says that SceneManager.LoadSceneAsync() doesn't return null because the example given there assumes that the result of SceneManager.LoadSceneAsync() is never null:
    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

    Even more, it is said in docs that invalid scene name will cause an Exception to be thrown. None happens. Just an error is logged to the console.

    2. What can be done.
    We can't change the SceneManager.LoadSceneAsync(), but we can fix SceneProvider.cs a little bit to make it handle the null result. Thee are two options:
    - change the original file;
    - create a fixed copy of SceneProvider.cs and register it in Addressables settings.

    We have to fix two methods:

    2.1. void IUpdateReceiver.Update(float unscaledDeltaTime):
    Code (CSharp):
    1.             void IUpdateReceiver.Update(float unscaledDeltaTime)
    2.             {
    3.                 if (m_Inst.m_Operation.isDone || (!m_Inst.m_Operation.allowSceneActivation && m_Inst.m_Operation.progress == .9f))
    4.                 {
    5.                     m_ResourceManager.RemoveUpdateReciever(this);
    6.                     Complete(m_Inst, true, null);
    7.                 }
    8.             }
    9.  
    Change it this way:
    Code (CSharp):
    1.             void IUpdateReceiver.Update(float unscaledDeltaTime)
    2.             {
    3.                 // fix start
    4.                 if (m_Inst.m_Operation == null)
    5.                 {
    6.                     m_ResourceManager.RemoveUpdateReciever(this);
    7.                     Complete(m_Inst, false, "The AsyncOperationHandle is null for Scene loading.");
    8.                     return;
    9.                 }
    10.                 // fix end
    11.  
    12.                 if (m_Inst.m_Operation.isDone || (!m_Inst.m_Operation.allowSceneActivation && m_Inst.m_Operation.progress == .9f))
    13.                 {
    14.                     m_ResourceManager.RemoveUpdateReciever(this);
    15.                     Complete(m_Inst, true, null);
    16.                 }
    17.             }
    18.  
    2.2. internal SceneInstance InternalLoadScene(IResourceLocation location, bool loadingFromBundle, LoadSceneMode loadMode, bool activateOnLoad, int priority)

    Code (CSharp):
    1.             internal SceneInstance InternalLoadScene(IResourceLocation location, bool loadingFromBundle, LoadSceneMode loadMode, bool activateOnLoad, int priority)
    2.             {
    3.                 var internalId = m_ResourceManager.TransformInternalId(location);
    4.                 var op = InternalLoad(internalId, loadingFromBundle, loadMode);
    5.  
    6.                 op.allowSceneActivation = activateOnLoad;
    7.                 op.priority = priority;
    8.  
    9.                 return new SceneInstance() { m_Operation = op, Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1) };
    10.             }
    11.  
    The fixed code:
    Code (CSharp):
    1.             internal SceneInstance InternalLoadScene(IResourceLocation location, bool loadingFromBundle, LoadSceneMode loadMode, bool activateOnLoad, int priority)
    2.             {
    3.                 var internalId = m_ResourceManager.TransformInternalId(location);
    4.                 var op = InternalLoad(internalId, loadingFromBundle, loadMode);
    5.  
    6.                 if (op != null)
    7.                 {
    8.                     op.allowSceneActivation = activateOnLoad;
    9.                     op.priority = priority;
    10.                 }
    11.                 return new SceneInstance() { m_Operation = op, Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1) };
    12.             }
    13.  
    I tested everything with these versions:
    Unity 2019.4.10f1
    Addressables 1.17.17
     
    Syooian and Steimond1 like this.
  12. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    138
    Steimond1 and anisimovdev like this.
  13. NNSkelly

    NNSkelly

    Joined:
    Nov 16, 2015
    Posts:
    35
    tangential problem involving DownloadDependenciesAsync it looks like if the underlying web operations fail, the AsyncOperationHandle.Status is AsyncOperationStatus.Failed upon completion (after either an await or in a completion handler). If LoadSceneAsync behaves similarly, then checking for that will at least give you an indicator. What you do from there if Unity refuses to load your assets because they e.g. take more than 3 seconds to download and curl gives up... is probably on you.
     
  14. ekodl

    ekodl

    Joined:
    Dec 18, 2018
    Posts:
    1
    For anyone that stumbles across this from google like I did - you can kinda handle this using the ResourceManager.ExceptionHandler. You'll still see the NullReferenceException happen in the logs, but it'll at least allow you to manage the fact that you can't get your addressable scene via the Exception that is a parameter in your CustomExceptionHandler.
     
    DavidZobrist likes this.
  15. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    201
    @ekodl thanks. that worked

    just modified it to catch the specific "could not connect to remote bundle" error as a normal log and all others as warnings


    Code (CSharp):
    1. void CustomExceptionHandler(AsyncOperationHandle handle, Exception exception)
    2. {
    3.     if (exception.HResult == -2146233088)
    4.     {
    5.         Debug.Log("AA: Connection failed to load remote bundles ");
    6.     }
    7.     else
    8.     {
    9.         Debug.LogWarning("Unhandled Error + " +exception.Message);
    10.     }
    11. }

    Its more readable for us like this
     
    ekodl likes this.
  16. pixelminer

    pixelminer

    Joined:
    Jul 24, 2011
    Posts:
    26
    Just ran in to this my self in 1.18.19. Has this issue been addressed in 1.19?
     
  17. pixelminer

    pixelminer

    Joined:
    Jul 24, 2011
    Posts:
    26
    Never mind. This issue seems to be fixed in 1.19.18.
     
  18. pillakirsten

    pillakirsten

    Unity Technologies

    Joined:
    May 22, 2019
    Posts:
    346
    Hi all the bug report mentioned in the thread is here: https://issuetracker.unity3d.com/is...ference-dot-loadsceneasync-with-invalid-scene

    It seems like the issue tracker de-synced with the closed internal ticket. Here's the missing resolution notes:

    Because this is an async operation, by the time an exception is hit, it likely cannot be passed up to the calling code. As such, we never attempt to pass it up, but instead catch the exception ourselves and log it. So two notes.
    1. Exceptions are indeed caught, they're just also logged, which could make them appear as "not caught"
    2. If a user wishes to actively process exceptions, they should do so via the ResourceManager.ExceptionHandler as outlined here https://docs.unity3d.com/Packages/c...agement.ResourceManager.ExceptionHandler.html

    There was also a recent fix in 1.19.4 "Fixed issue where Exceptions in UnityWebRequest.Send were not caught."
     
    alexandr13568 likes this.
  19. Igor_Time

    Igor_Time

    Joined:
    Dec 2, 2016
    Posts:
    21
    Hi @pillakirsten. I still quite confusing about how I have to handle Addressables download error.
    I'm trying to use ResourceManager.ExceptionHandler solution which you mentioned earlier, but the only thing I've got its a regular log message. I currently use a workaround, I subscribe on Application.logMessageReceived event and parse every single log message, trying find the message which starts from "Web request failed to load..." but it's avery bad solution

    I'm using Unity 2022.3.4f1 with Addressables 1.21.14. Also I use Unity's CCD solution

    Web request failed to load from cache. The cached AssetBundle will be cleared from the cache and re-downloaded. Retrying...
    ProtocolError : HTTP/1.1 404 Not Found
    ResponseCode : 404, Method : GET
    url : {here-is-my-url}


    And it's not only about scenes, I've got the same behaviour with other groups
     
  20. nguyenducthuanse

    nguyenducthuanse

    Joined:
    Jan 17, 2021
    Posts:
    6
    Same problem, Is there any new update?

    Our project is using Unity 2021.1.16 and Addressable 1.21.14 (upgraded by manijson from 1.19.19)
    Each we upload a new build version to the host, Addressable will try to load max 3 times with the error log same below:

    cacheVersion=1692583293864Dj9HFYwgyg:3 Web request failed, retrying (2/3)...
    ConnectionError : Request timeout
    ResponseCode : 500, Method : GET