Search Unity

Every version of 1.18 gives me a different error when trying to use WaitForCompletion

Discussion in 'Addressables' started by renegadegd, Jun 13, 2021.

  1. renegadegd

    renegadegd

    Joined:
    Mar 23, 2014
    Posts:
    10
    I'm seeing a different error while trying to use WaitForCompletion in every version of 1.18 so far. The code I have works fine in 1.17.6-preview but updating to the latest seems to be causing some pains. Any ideas on why would be great help.

    This is the code i'm calling (It is being called inside another task, maybe that has something to do with it?)...
    Code (CSharp):
    1.     public static GameObject InstantiateGameObject(string assetName, Transform parent = null)
    2.     {
    3.         AsyncOperationHandle<GameObject> asyncOp = Addressables.InstantiateAsync(assetName, parent);
    4.  
    5.         asyncOp.WaitForCompletion();
    6.  
    7.         return asyncOp.Result;
    8.     }

    1.18.2 gives me this...
    Code (CSharp):
    1. InvalidOperationException: EnsureRunningOnMainThread can only be called from the main thread
    2. UnityEngine.Object.EnsureRunningOnMainThread () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:154)
    3. UnityEngine.Object.GetInstanceID () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:73)
    4. UnityEngine.Object.IsNativeObjectAlive (UnityEngine.Object o) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:182)
    5. UnityEngine.Object.CompareBaseObjects (UnityEngine.Object lhs, UnityEngine.Object rhs) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:140)
    6. UnityEngine.Object.op_Equality (UnityEngine.Object x, UnityEngine.Object y) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:428)
    7. UnityEngine.Object.Equals (System.Object other) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:122)
    8. System.Collections.Generic.ObjectEqualityComparer`1[T].Equals (T x, T y) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    9. System.Collections.Generic.Dictionary`2[TKey,TValue].Remove (TKey key) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    10. UnityEngine.AddressableAssets.AddressablesImpl.OnHandleDestroyed (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle handle) (at Library/PackageCache/com.unity.addressables@1.18.2/Runtime/AddressablesImpl.cs:712)
    11. DelegateList`1[T].Invoke (T res) (at Library/PackageCache/com.unity.addressables@1.18.2/Runtime/ResourceManager/Util/DelegateList.cs:69)
    12. UnityEngine.Debug:LogException(Exception)
    13. DelegateList`1:Invoke(AsyncOperationHandle) (at Library/PackageCache/com.unity.addressables@1.18.2/Runtime/ResourceManager/Util/DelegateList.cs:73)
    14. System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()
    1.18.4 gives me this...
    Code (CSharp):
    1. ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    2. Parameter name: index
    3. System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    4. System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <695d1cc93cca45069c528c15c9fdd749>:0)
    5. UnityEngine.ResourceManagement.ResourceManager.ExecuteDeferredCallbacks () (at Library/PackageCache/com.unity.addressables@1.18.4/Runtime/ResourceManager/ResourceManager.cs:1013)
    6. UnityEngine.ResourceManagement.ResourceManager.Update (System.Single unscaledDeltaTime) (at Library/PackageCache/com.unity.addressables@1.18.4/Runtime/ResourceManager/ResourceManager.cs:1036)
    7. MonoBehaviourCallbackHooks.Update () (at Library/PackageCache/com.unity.addressables@1.18.4/Runtime/ResourceManager/Util/MonoBehaviourCallbackHooks.cs:26)
    1.18.9 gives me this...
    Code (CSharp):
    1. Exception: Reentering the Update method is not allowed.  This can happen when calling WaitForCompletion on an operation while inside of a callback.
    2. UnityEngine.ResourceManagement.ResourceManager.Update (System.Single unscaledDeltaTime) (at Library/PackageCache/com.unity.addressables@1.18.9/Runtime/ResourceManager/ResourceManager.cs:1054)
    3. UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1[TObject].WaitForCompletion () (at Library/PackageCache/com.unity.addressables@1.18.9/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:173)
    4. AddressablesLoader.InstantiateGameObject (System.String assetName, UnityEngine.Transform parent, System.Action`1[T] callback) (at Assets/Scripts/Utils/Loaders/AddressablesLoader.cs:74)
    5. WorldUnitController.CreatePlayer (WorldPortProp spawnPort) (at Assets/Scripts/Screens/World/Unit/WorldUnitController.cs:194)
    6. WorldUnitController.SetUp (WorldPortProp playerSpawnPort) (at Assets/Scripts/Screens/World/Unit/WorldUnitController.cs:62)
    7. WorldScreen+<Load>d__12.MoveNext () (at Assets/Scripts/Screens/World/WorldScreen.cs:98)
    8. --- End of stack trace from previous location where exception was thrown ---
    9. System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
    10. System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    11. System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    12. System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    13. System.Runtime.CompilerServices.TaskAwaiter.GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0)
    14. ScreenBase+<Activate>d__22.MoveNext () (at Assets/Scripts/App/ScreenController/ScreenBase.cs:82)
    15. --- End of stack trace from previous location where exception was thrown ---
    16. System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
    17. System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    18. UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:153)
    19. UnityEngine.UnitySynchronizationContext:ExecuteTasks() (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)
     
    Last edited: Jun 13, 2021
  2. silyu

    silyu

    Joined:
    Jun 23, 2021
    Posts:
    1
    I meet the same problem.


    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <e1319b7195c343e79b385cd3aa43f5dc>:0)
    System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <e1319b7195c343e79b385cd3aa43f5dc>:0)
    System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <e1319b7195c343e79b385cd3aa43f5dc>:0)
    UnityEngine.ResourceManagement.ResourceManager.ExecuteDeferredCallbacks () (at Library/PackageCache/com.unity.addressables@1.18.4/Runtime/ResourceManager/ResourceManager.cs:1010)
    UnityEngine.ResourceManagement.ResourceManager.Update (System.Single unscaledDeltaTime) (at Library/PackageCache/com.unity.addressables@1.18.4/Runtime/ResourceManager/ResourceManager.cs:1036)
    MonoBehaviourCallbackHooks.Update () (at Library/PackageCache/com.unity.addressables@1.18.4/Runtime/ResourceManager/Util/MonoBehaviourCallbackHooks.cs:26)

     
  3. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
    I'll flag with the team for some guidance.
     
  4. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    So, the issues you saw on 18.2 and 18.4 were legitimate bugs on our end. The error you're seeing on 18.9 is actually our way of saying that the code is trying to do something invalid, in this case it's getting into a state where the ResouceManager is getting called twice in the same frame. It seems very possible that you calling that code from a task could lead to a situation that would put you in that invalid state. I'd double check you're not executing that code as part of any callback.

    If you look over everything and still feel like it's a bug on our end feel free to file a bug but please provide your repro project so we can debug the issue.

    Hope that helps!
     
  5. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    70
    It'd be very helpful if you could elaborate on this in the docs.
     
  6. KingKRoecks

    KingKRoecks

    Joined:
    Jul 28, 2013
    Posts:
    155
    Hey, I'm actually debugging this issue right now as well.
    I was playing around with writing some class to handle my loading.

    In my case, something in this that causes it to happen. If I don't call this IEnumerator, it doesn't happen.


    Code (CSharp):
    1. public class DefinitionAddressablesLoader
    2.     {
    3.        
    4.         public AsyncOperationHandle<List<string>> catalogUpdatesAsync;
    5.  
    6.         public IEnumerator LoadCatalog()
    7.         {
    8.             Debug.Log("DefinitionAddressablesLoader - LoadCatalog");
    9.            
    10.             DateTime startTime = DateTime.Now;
    11.  
    12.             catalogUpdatesAsync = Addressables.CheckForCatalogUpdates();
    13.  
    14.             while (catalogUpdatesAsync.IsValid() && !catalogUpdatesAsync.IsDone)
    15.             {
    16.                 yield return new WaitForSeconds(1);
    17.             }
    18.            
    19.             if (catalogUpdatesAsync.IsValid() && catalogUpdatesAsync.Status == AsyncOperationStatus.Succeeded)
    20.             {
    21.                 var updates = catalogUpdatesAsync.Result;
    22.                 if (updates.Count > 0)
    23.                 {
    24.                     var updateCatalogsAsync = Addressables.UpdateCatalogs(updates);
    25.                     while (!updateCatalogsAsync.IsDone)
    26.                     {
    27.                         yield return new WaitForSeconds(1);
    28.                     }
    29.  
    30.                     if (updateCatalogsAsync.Status == AsyncOperationStatus.Failed)
    31.                     {
    32.                         Debug.LogError("DefinitionAddressablesLoader - Failed to updateCatalogsAsync " + updateCatalogsAsync.OperationException);
    33.                         isError = true;
    34.                     }
    35.                 }
    36.             }
    37.             else
    38.             {
    39.                 Debug.LogError("DefinitionAddressablesLoader - Failed to catalogUpdatesAsync ");
    40.                 isError = true;
    41.             }
    42.  
    43.             Debug.Log($"DefinitionAddressablesLoader - LoadCatalog  after {(DateTime.Now - startTime).TotalSeconds} secs");
    44.         }
    45. }
    The problem with us trying to debug it, is that we don't actually get a callstack showing us what call is making that happen. I put in a bunch of "yield break;" to trace it to the spot. It seems related to the CheckForCatalogUpdates();

    So if you could make the exception happen at us calling the event that triggers the exception rather than in your event handler after the event has lost context, it will help us find out what our issue is. I don't have source code, so I'm jsut kind of guessing as to how your system is configured.
     
  7. renegadegd

    renegadegd

    Joined:
    Mar 23, 2014
    Posts:
    10
    Hey all, it's been a while but finally dedicated a little more time to this issue.

    I've been keeping my project on Unity 2020.3.11 with Addressables 1.17.6-preview as it seems to run fine on there. However if i upgrade either Unity to 2021.2 or Addressables to 1.18.4 or any higher version I receive the 'Reentering the Update method is not allowed' error.

    I have tried making a new project with one simplified script that replicates what I'm doing in my main project. Here I'm trying to switch scene while keeping this object alive with DontDestroyOnLoad then following up with loading a prefab. This is causing the error to show up.

    @davidla_unity Are you able to take a look or pass this onto someone who might be in the know? Wondering if I'm doing something fundamentally wrong here or if there is a bug.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.AddressableAssets;
    4. using UnityEngine.ResourceManagement.AsyncOperations;
    5. using UnityEngine.ResourceManagement.ResourceProviders;
    6.  
    7. public class TestLoad : MonoBehaviour
    8. {
    9.    private void Awake()
    10.    {
    11.       DontDestroyOnLoad(gameObject);
    12.    }
    13.  
    14.    private async void Start()
    15.    {
    16.       // async load scene
    17.       AsyncOperationHandle<SceneInstance> sceneAsyncOp = Addressables.LoadSceneAsync("SampleScene");
    18.       await sceneAsyncOp.Task;
    19.  
    20.       // Sync load cube
    21.       AsyncOperationHandle<GameObject> syncOp = Addressables.InstantiateAsync("CubePrefab", transform);
    22.       syncOp.WaitForCompletion();
    23.    }
    24. }
    25.  
     
    Last edited: Nov 21, 2021
  8. renegadegd

    renegadegd

    Joined:
    Mar 23, 2014
    Posts:
    10
    Well it's a Christmas miracle! Just tried again in 2021.2.7f with addressables 1.19.15 and it seems to be working.

    Looking at the change log for addressables 1.19.13, I think this must have been the fix.


    [*]Fixed issue where "Reentering the Update method is not allowed" exception occurs when calling WaitForCompletion during an async method.


    Thanks to the unity employee that got this working.
     
    larry2013z likes this.
  9. ringxtee

    ringxtee

    Joined:
    Dec 12, 2017
    Posts:
    2
    I met a situation is

    Code (CSharp):
    1. LoadAssetAsync(xxx, (op1) => {
    2.     LoadAssetAsync(xxx, (op2) => {
    3.         var op3 = LoadAssetAsync(xxx);
    4.         var result3 = op3.WaitForCompletion();
    5.     })
    6. })
    7.  
    8. var op4 = LoadAssetAsync(xxx);
    9. var result4 = op4.WaitForCompletion();
    It will report "Reentering the Update method is not allowed"
    caz LoadAssetAsync callback WaitForCompletion

    @davidla_unity
     
    Last edited: Jan 26, 2022
    sumio_kajiwara_rudel likes this.