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

Bug AddressableAssetSettingsDefaultObject.Settings sometimes null

Discussion in 'Addressables' started by Peter77, Sep 4, 2020.

Thread Status:
Not open for further replies.
  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    We get random null reference exception caused by the following line when building our game on the build server:
    foreach (var group in AddressableAssetSettingsDefaultObject.Settings.groups)


    The surrounding code looks like:
    Code (CSharp):
    1. if (!UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.SettingsExists)
    2. {
    3.    Debug.LogWarning("Addressable Settings don't exist, creating new ones.");
    4.  
    5.    UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.Settings =
    6.        UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.GetSettings(true);
    7. }
    8.  
    9. if (UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.Settings == null)
    10. {
    11.    Debug.LogWarning("Addressable Settings is null.");
    12. }
    13. else if (UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.Settings.groups == null)
    14. {
    15.    Debug.LogWarning("Addressable Settings.groups is null.");
    16. }
    17.  
    18. foreach (var group in UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.Settings.groups)
    19. {
    20. }
    21.  
    22.  
    AddressableAssetSettingsDefaultObject.SettingsExists
    returns true (I don't see the warning in the log), but
    Settings
    is null. Just triggering the build again magically causes this exception to disappear.

    Why is
    Settings
    sometimes
    null
    and how can I fix/workaround this problem?

    Using Unity 2018.4.26f1 and Addressables 1.13.1
     
    Last edited: Sep 4, 2020
    _geo__ and CasualT_Bossfight like this.
  2. Shaunyowns

    Shaunyowns

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey @Peter77, got a message back from the Addressables team, can you send in a bug report for them?

    Thank you!
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    A bug-report isn't going to help, since the reproduce chance is bad. It occurs randomly. Sometimes several times in a row, sometimes not at all for multiple days. It not only occurs on our build server, but also locally.

    I just ran in the issue locally, occurred several times in a row, so I was able to attach a debugger. But after debugging, the issue went away magically.

    However, as you can see below, the "Settings" reference is null, yet "SettingsExists" is true. Please ask your team to take a look at it. It's probably some of the if statements in the Settings property that is causing it.

    upload_2020-11-23_8-41-44.png
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    bump, see my post above.
     
  5. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,795
    Gotcha. I'll flag with the team again. I think they are keen on at least trying to reproduce. I know you're not confident it would be helpful, but I think they'd appreciate the bug report just in case. :)
     
  6. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    736
    I have a thought about what might be happening:
    The editor application is either refreshing the AssetDatabase or is in a script compiling state. The Settings property has a check
    Code (CSharp):
    1. if (s_DefaultSettingsObject == null && !EditorApplication.isUpdating && !EditorApplication.isCompiling)
    which, when this evaluates to true, we load the settings file. If it hasn't previously been loaded and is in one of those other two states it'll return you null. The SettingsExists just checks the AssetDatabase that a file is there or not.

    This would explain why it only happens randomly. Though, looking at your surrounding code I would have expected the if statement above to detect the Settings as null. Ah, it may be that because the cached s_DefaultSettingsObject is getting lost as part of a domain reload (which could be caused by script compilation or something) and then if scripts are compiling when the Settings is requested it comes back as null.

    I'm not sure what, if anything, we can do about this. Does any of this sound like it might be what's happening on your build server?
     
    Peter77 likes this.
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    Hey @DavidUnity3d thank you for the reply.

    I added more debug log's and it turns out
    EditorApplication.isUpdating
    is
    true
    . The code that sometimes causes
    AddressableAssetSettingsDefaultObject.Settings
    to return
    null
    basically looks like this:
    Code (CSharp):
    1. AssetDatabase.StartAssetEditing();
    2. try
    3. {
    4.     EditorUtility.SaveFolderPanel("Save level to...", "Assets/Scenes", "");
    5.  
    6.     // https://forum.unity.com/threads/addressableassetsettingsdefaultobject-settings-groups-sometimes-null.964103/
    7.     if (AddressableAssetSettingsDefaultObject.Settings == null)
    8.     {
    9.         Dbg.Error(null, "AddressableAssetSettings could not be loaded. isUpdating: {0}, isCompiling: {1}", EditorApplication.isUpdating, EditorApplication.isCompiling);
    10.         return;
    11.     }
    12.  
    13.     // Generate assets
    14. }
    15. finally
    16. {
    17.     AssetDatabase.StopAssetEditing();
    18. }
    19.  
    The description of EditorApplication.isUpdating reads like StartAssetEditing/StopAssetEditing could be related to it? But I don't understand why it only occurs randomly when that's the case.

    I changed the code (see below) and will continue to observe if the error keeps coming up:
    Code (CSharp):
    1. EditorUtility.SaveFolderPanel("Save level to...", "Assets/Scenes", "");
    2.  
    3. // https://forum.unity.com/threads/addressableassetsettingsdefaultobject-settings-groups-sometimes-null.964103/
    4. if (AddressableAssetSettingsDefaultObject.Settings == null)
    5. {
    6.    Dbg.Error(null, "AddressableAssetSettings could not be loaded. isUpdating: {0}, isCompiling: {1}", EditorApplication.isUpdating, EditorApplication.isCompiling);
    7.    return;
    8. }
    9.  
    10. AssetDatabase.StartAssetEditing();
    11. try
    12. {
    13.    // Generate assets
    14. }
    15. finally
    16. {
    17.    AssetDatabase.StopAssetEditing();
    18. }
     
  8. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    299
    I was able to reproduce this on MacOS with a simple project setup with Addressables and the following build script:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.AddressableAssets;
    3. using UnityEditor.AddressableAssets.Settings;
    4. using UnityEngine;
    5.  
    6. public static class AutoBuilder
    7. {
    8.     [MenuItem("Test/Build")]
    9.     private static void BuildStuff()
    10.     {
    11.         RecompileScripts();
    12.         BuildAddressables();
    13.     }
    14.  
    15.     private static void RecompileScripts()
    16.     {
    17.         var guids = AssetDatabase.FindAssets("t:script", new[] {"Assets/Scripts"});
    18.  
    19.         if (guids.Length > 0)
    20.         {
    21.             var path = AssetDatabase.GUIDToAssetPath(guids[0]);
    22.             AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    23.         }
    24.     }
    25.  
    26.     private static void BuildAddressables()
    27.     {
    28.         Debug.Log("");
    29.         Debug.Log($"STATUS:  isUpdating: {EditorApplication.isUpdating}   isCompiling: {EditorApplication.isUpdating}  settingsExist: {AddressableAssetSettingsDefaultObject.SettingsExists}  isNull: {AddressableAssetSettingsDefaultObject.Settings == null}");
    30.         AddressableAssetSettingsDefaultObject.Settings.ActivePlayerDataBuilderIndex = AddressableAssetSettingsDefaultObject.Settings.DataBuilders.Count - 1;
    31.         AddressableAssetSettings.CleanPlayerContent();
    32.         AddressableAssetSettings.BuildPlayerContent();
    33.     }
    34. }
    Executing batch mode from the command line via:
    Code (JavaScript):
    1. #!/bin/bash
    2.  
    3. UNITY="/Applications/Unity/2019.4.17f1/Unity.app/Contents/MacOS/Unity"
    4.  
    5. $UNITY -batchMode -executeMethod AutoBuilder.BuildStuff -logFile -
    I submitted a bug report on this with my sample project for 2019.4.17f1.

    I suspect it is some sort of timing issue as it is dumping:
     
    Last edited: Jan 12, 2021
    Peter77 likes this.
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    Can you post the bug-report case number, so Unity staff participating in this thread can quickly find it?
     
  10. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    299
    I was waiting on this, but found it in my junk mail: Case 1305730
     
    Peter77 likes this.
  11. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    736
    I just checked out case 1305730 and it's still being processed by our incoming QA so it hasn't quite made it to our team yet.

    It definitely feels like some kind of race condition with AssetDatabase and our request.

    Not that this is how it should work, but a potential workaround could be loading the scriptable object yourself using AddressableAssetSettingsDefaultObject.DefaultAssetPath. The settings object is just a scriptable object so you should be able to load it using AssetDatabase.LoadAssetAtPath<AddressableAssetSettings>(...) path.

    Maybe that will help you in the meantime while your ticket gets processed and makes it to our team.
     
    CasualT_Bossfight likes this.
  12. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    299
    I was able to load the settings file via your suggestion. However, it now throws this error in the log and still does not build the bundles:
    Code (CSharp):
    1. Addressable Asset Settings does not exist.  EditorApplication.isCompiling was true.
    2. UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
    3. UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    4. UnityEngine.Logger:Log(LogType, Object)
    5. UnityEngine.Debug:LogError(Object)
    6. UnityEditor.AddressableAssets.Settings.AddressableAssetSettings:BuildPlayerContent() (at Library/PackageCache/com.unity.addressables@1.16.15/Editor/Settings/AddressableAssetSettings.cs:1878)
    7. AutoBuilder:BuildAddressables() (at Assets/Scripts/Editor/AutoBuilder.cs:45)
    8. AutoBuilder:BuildStuff() (at Assets/Scripts/Editor/AutoBuilder.cs:12)
     
  13. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    736
    Sorry, I don't think I mentioned this but you may have to set the AddressableAssetSettingsDefaultObject.Settings to that object that you loaded yourself. There's a good chance you'll run in the same issue if there's another domain reload or if, for whatever reason, that internal reference to the settings object is lost.

    If that doesn't work we'll just have to take a look at the bug when it comes in. I'm drawing a blank on other things to suggest.

    I just checked that case again and it's still being processed by our incoming QA? Seems odd for it to take this long. I'll see if I can't nudge this along.
     
  14. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    299
    Just got an email update saying it's been entered into your system. Looks like Jira IN-949
     
  15. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    This problem still occurs on our build server randomly. I run this code before we do anything else:
    Code (CSharp):
    1. // Another desperate attempt to workaround Addressables returning null
    2. // https://forum.unity.com/threads/addressableassetsettingsdefaultobject-settings-groups-sometimes-null.964103/
    3. // The idea with this workaround is that we access Addressables before any lock occurs (LockReloadAssemblies, StartAssetEditing, etc)
    4. if (UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.Settings == null)
    5.     Dbg.Warning(null, $"Addressable Settings is null.\nisUpdating: {EditorApplication.isUpdating}   isCompiling: {EditorApplication.isUpdating}  settingsExist: {UnityEditor.AddressableAssets.AddressableAssetSettingsDefaultObject.SettingsExists}");
    6.  
    ... yet it sometimes returns null and will then later on cause a NullReferenceException. The output of above code is this:
    Code (CSharp):
    1. Addressable Settings is null.
    2. isUpdating: False   isCompiling: False  settingsExist: True
     
  16. KBaxtrom-LevelEx

    KBaxtrom-LevelEx

    Joined:
    Feb 5, 2018
    Posts:
    28
    Is there any update on this? We are hitting this issue as well but only on a Windows bulid
     
  17. joe_nk

    joe_nk

    Joined:
    Jan 6, 2017
    Posts:
    67
    I'm hitting this issue too, on Addressables version 1.16.16. We recompile scripts, then attempt to access the
    AddressableAssetSettingsDefaultObject.Settings, which is of course null.
     
  18. RG_Keith

    RG_Keith

    Joined:
    Mar 14, 2017
    Posts:
    35
    We get this too on our build server - it's very likely to happen when doing a full clean build (which involves deleting the entire Library directory) - but that's the only time it happens. We're on AssetDatabase v2. Interestingly, it'll usually happen once for each platform (PC, Mac, XB1, PS4, Switch), but then once it has failed for each platform, each subsequent build is fine.
     
    lanpartygamesstudio likes this.
  19. lanpartygamesstudio

    lanpartygamesstudio

    Joined:
    Jul 3, 2019
    Posts:
    3
    Also experiencing this issue in Unity 2021.1.14f1 (Addressables 1.16.19)
     
  20. agilgamedev

    agilgamedev

    Joined:
    Aug 20, 2021
    Posts:
    1
  21. d_sherlock

    d_sherlock

    Joined:
    Apr 3, 2020
    Posts:
    5
    Any news in this thread? How to fixed this problem?
     
Thread Status:
Not open for further replies.