Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: InvalidOperationException defining Schema configuration property #2701

Closed
XDarinor opened this issue Nov 1, 2021 · 1 comment · Fixed by #2702
Closed

[Bug]: InvalidOperationException defining Schema configuration property #2701

XDarinor opened this issue Nov 1, 2021 · 1 comment · Fixed by #2702
Assignees

Comments

@XDarinor
Copy link

XDarinor commented Nov 1, 2021

What happened?

Randomly, while settings Schema property of RealmConfiguration using new Type[] { } array value throws an InvalidOperationException with the message:

System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.

NOTE: I create a realm instance everytime I need to read or write a value in my app. My application is multi-threaded, and notify the UI of the changes over the entities is not a requirement. The read and the writes must be auto-contained.

There is a working thread that constantly read bluetooth objects and read the DB every 500 ms to identify them.

Repro steps

Launch the app, create a RealmConfiguration instance and apply a schema using the Schema property.

Version

10.6.0 dotnet

What SDK flavour are you using?

Local Database only

What type of application is this?

Xamarin

Client OS and version

Android 7, Xamarin.Android 11.4.0.5

Code snippets

Realm instance initialization method:

realmConfiguration = new RealmConfiguration(DefaultRealmFilename);               
realmConfiguration.SchemaVersion = CurrentSchemaVersion;
realmConfiguration.MigrationCallback = this.MigrationCallback;

  // realmConfiguration.ObjectClasses = new Type[] { }  seems deprecated. 
  // use Schema instead.

  try
  {
      realmConfiguration.Schema = new Type[]
      {
      typeof(TableA),
      typeof(TableB),
      };
  }

// this code is never reached, the exception throws on the schema part.

 try
  {
      this.realm = Realm.GetInstance(this.realmConfiguration);
#if HEAVY_VERBOSE_MODE
      if (SynchronizationContext.Current != null)
      {
          Logger.Debug("SyncContext is not null.");
      }
      else
          Logger.Debug("SyncContext is null.");
#endif
  }
  catch(RealmMigrationNeededException realmMigrationException)
  {
      Logger.Warning("A migration is needed on current app version.");
      Logger.Warning(realmMigrationException.ToString());
      migrationException = realmMigrationException;
      requireMigration = true;                
  }

For every write or read, the realm object is instantiated using:

IStorageProviderContext storageProviderContext = null;

try
{
    if (StorageManager.Current != null)
        storageProviderContext = SmartlockStorageManager.Current.CreateProviderInstance();
}
catch(Exception exc)
{
    Logger.Error(exc);
}            

try
{                
    using (storageProviderContext)
    {
        if (storageProviderContext != null)
        {
            objectsCount = storageProviderContext.KnownObjects.Count;
            locatonsCount = storageProviderContext.Locations.Count;
        }
    }
}
catch (Exception exc)
{
    Logger.Error(exc);
}

Stacktrace of the exception/crash you're getting

at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00105] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:437 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:888 
  at Realms.Schema.ObjectSchema.FromType (System.Type type) [0x0000b] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Schema\ObjectSchema.cs:126 
  at Realms.Schema.RealmSchema+Builder.Add (System.Type type) [0x0000b] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Schema\RealmSchema.cs:341 
  at Realms.Schema.RealmSchema+Builder..ctor (System.Collections.Generic.IEnumerable`1[T] types) [0x00021] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Schema\RealmSchema.cs:273 
  at Realms.Realm.GetInstance (Realms.RealmConfigurationBase config) [0x0000a] in D:\a\realm-dotnet\realm-dotnet\11-01 00:50:26.989 V/mono-stdout(30354):   at Realms.Schema.RealmSchema.op_Implicit (System.Collections.Generic.HashSet`1[T] objects) [0x00000] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Schema\RealmSchema.cs:245 
  at Realms.Schema.RealmSchema+<>c.<.cctor>b__25_0 () [0x00090] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Schema\RealmSchema.cs:66 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x0001c] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:327 
--- End of stack trace from previous location where exception was thrown ---
  at System.Lazy`1[T].CreateValue () [0x0007e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:435 
  at System.Lazy`1[T].get_Value () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Lazy.cs:509 
  at Realms.Schema.RealmSchema.get_Default () [0x00000] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Schema\RealmSchema.cs:99 
  at Realms.RealmConfigurationBase.GetSchema () [0x0001d] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Configurations\RealmConfigurationBase.cs:241 
  at Realms.RealmConfiguration.CreateRealm () [0x00000] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Configurations\RealmConfiguration.cs:126 
  at Realms.Realm.GetInstance (Realms.RealmConfigurationBase config) [0x0000a] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Realm.cs:83

Relevant log output

No response

@nirinchev
Copy link
Member

Hey, thanks for the report - this definitely looks like a bug and I have very strong feeling I know what's causing it on our end. We'll take a look first thing on Monday and come up with a fix.

@nirinchev nirinchev self-assigned this Nov 1, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants