Uploaded image for project: 'Realm .NET SDK'
  1. Realm .NET SDK
  2. RNET-227

Opening realm with IsDynamic=true results in hung file when exception building schema.

      <!--- Questions: If you have questions about how to use Realm, please ask on -->
      <!--- StackOverflow: http://stackoverflow.com/questions/ask?tags=realm -->
      <!--- We monitor the realm tag. -->

      <!--- Feature Request: Just fill in the first two sections below. -->

      <!--- Bugs: To help you as fast as possible with an issue please describe your issue -->
      <!--- and the steps you have taken to reproduce it in as much detail as possible. -->

      <Unable to render embedded object: File (--- Thanks for helping us help you) not found. -->

      Goals

      To be able to open an existing realm file in dynamic mode without the file being left open with no way to delete it in the event of an exception is thrown when building the schema dynamically.

      Essentially:

      try
      {
          var config = new RealmConfiguration("C:\path\to\realmfile") { IsDynamic = true };
          var instance = Realms.Realm.GetInstance(config);
      }
      catch (InvalidOperationException ex)
      {
          File.Delete("C:\path\to\realmfile");
      }
      

      Expected Results

      When the exception from ObjectSchema.cs Build() gets thrown:

      if (Count == 0)
      {
          throw new InvalidOperationException(
              "No RealmObjects. Has linker stripped them? See https://realm.io/docs/xamarin/latest/#linker-stripped-schema");
      }
      

      I should be able to delete the file "C:\path\to\realmfile"

      Actual Results

      Deleting results in exception due to the file still being open:

      System.IO.IOException: The process cannot access the file 'C:\path\to\realmfile' because it is being used by another process
      

      Steps to Reproduce

      Use any realm that has an object without properties (possible with older versions of realm), then try deleting after failing to get realm instance.

      Code Sample

      try
      {
      	var config = new RealmConfiguration("C:\path\to\realmfile") { IsDynamic = true };
      	var instance = Realms.Realm.GetInstance(config);
      }
      catch (InvalidOperationException ex)
      {
      	File.Delete("C:\path\to\realmfile");
      }
      

      It looks like the code that needs to handle closing the file is at

      RealmConfiguration.cs:

          internal override Realm CreateRealm(RealmSchema schema)
          {
      
              // ...
      
              var srHandle = new SharedRealmHandle(srPtr);
              if (IsDynamic && !schema.Any())
              {
                  // NOTE: exception here will leave realm file open--srHandle needs to close/dispose resources?
                  srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
              }
      
              return new Realm(srHandle, this, schema);
          }
      

      Version of Realm and Tooling

      Visual Studio Professional 2019
      16.5.4

      OS: Windows 10

      TargetFramework: netstandard2.0
      Nuget package version: 4.3.0

            Assignee:
            ferdinando.papale@mongodb.com Ferdinando Papale
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: