Using ES3File in thread causes expection

Discussion and help for Easy Save 3
Post Reply
davidlim
Posts: 9
Joined: Fri May 31, 2019 11:27 pm

Using ES3File in thread causes expection

Post by davidlim »

Hello, I'm trying to create a new ES3File in a thread, I'm pointing to a File that has a single entry of a string so the data is thread safe but I'm getting the following error:

Code: Select all

GetAll can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
The stacktrace:

Code: Select all

  at (wrapper managed-to-native) UnityEditor.PackageManager.PackageInfo.GetAll()
  at UnityEditor.PackageManagerUtilityInternal.GetAllVisiblePackages (System.Boolean skipHiddenPackages) [0x0000e] in C:\buildslave\unity\build\Editor\Mono\PackageManagerUtilityInternal.cs:19 
  at UnityEditor.AssetDatabase+<FindEverywhere>c__Iterator2`1[T].MoveNext () [0x0008f] in C:\buildslave\unity\build\Editor\Mono\AssetDatabase\AssetDatabaseSearching.cs:109 
  at UnityEditor.AssetDatabase+<FindAllAssets>c__Iterator0.MoveNext () [0x00063] in C:\buildslave\unity\build\Editor\Mono\AssetDatabase\AssetDatabaseSearching.cs:37 
  at System.Linq.Enumerable+SelectEnumerableIterator`2[TSource,TResult].ToArray () [0x00030] in <b7efe7e6e548497fac3c4a6049a0a4b6>:0 
  at System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0001f] in <b7efe7e6e548497fac3c4a6049a0a4b6>:0 
  at UnityEditor.AssetDatabase.FindAssets (UnityEditor.SearchFilter searchFilter) [0x00029] in C:\buildslave\unity\build\Editor\Mono\AssetDatabase\AssetDatabaseSearching.cs:31 
  at UnityEditor.AssetDatabase.FindAssets (System.String filter, System.String[] searchInFolders) [0x00038] in C:\buildslave\unity\build\Editor\Mono\AssetDatabase\AssetDatabaseSearching.cs:26 
  at UnityEditor.AssetDatabase.FindAssets (System.String filter) [0x00003] in C:\buildslave\unity\build\Editor\Mono\AssetDatabase\AssetDatabaseSearching.cs:13 
  at ES3Settings.LoadDefaults () [0x00001] in G:\devl\ecs\Redrook\Assets\Plugins\Easy Save 3\Scripts\Settings\ES3Settings.cs:151 
  at ES3Settings.ApplyDefaults () [0x0000d] in G:\devl\ecs\Redrook\Assets\Plugins\Easy Save 3\Scripts\Settings\ES3Settings.cs:136 
  at ES3Settings..ctor () [0x0001a] in G:\devl\ecs\Redrook\Assets\Plugins\Easy Save 3\Scripts\Settings\ES3Settings.cs:74 
  at ES3Settings..ctor (System.String path) [0x00000] in G:\devl\ecs\Redrook\Assets\Plugins\Easy Save 3\Scripts\Settings\ES3Settings.cs:79 
  at ES3File..ctor (System.String filePath) [0x00000] in G:\devl\ecs\Redrook\Assets\Plugins\Easy Save 3\Scripts\ES3File.cs:21 
  at GridSaver.Loader+<>c.<LoadDataAsync>b__11_0 () [0x00001] in G:\devl\ecs\Redrook\Assets\Scripts\GridSaver\Loader.cs:47 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <23c160f925be47d7a4fd083a3a62c920>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <23c160f925be47d7a4fd083a3a62c920>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <23c160f925be47d7a4fd083a3a62c920>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <23c160f925be47d7a4fd083a3a62c920>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <23c160f925be47d7a4fd083a3a62c920>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <23c160f925be47d7a4fd083a3a62c920>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <23c160f925be47d7a4fd083a3a62c920>:0 
  at GridSaver.Loader+<LoadDataAsync>d__11.MoveNext () [0x00042] in G:\devl\ecs\Redrook\Assets\Scripts\GridSaver\Loader.cs:45 
I can only assume ES3File tries to update the editor gui for some reason ?

The Save method looks like this:

Code: Select all

            var es3File = new ES3File("david");
            es3File.Save<string>("key1", "testString");
            es3File.Sync();
The load is like this

Code: Select all

        private async Task LoadDataAsync(string sceneName, string[] tileMapsNames)
        {
            try
            {
                f = await Task.Run(() =>
                {
                    return new ES3File($"david");
                });
    
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Using ES3File in thread causes expection

Post by Joel »

Hi there,

It looks like Unity's AssetDatabase.FindAssets method is no longer thread-safe, though this is a very unusual error message so it could be an error at their end. We call this method only in the Editor.

I'll PM you a version which uses the runtime code instead, as I believe this is now supported on all newer versions of Unity.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
davidlim
Posts: 9
Joined: Fri May 31, 2019 11:27 pm

Re: Using ES3File in thread causes expection

Post by davidlim »

Great, looking forward to the PM so I can try this out. I'm using the 2019.3 Beta btw
Post Reply