realm / realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Apache License 2.0
773 stars 86 forks source link

Realm is closed but deleteRealm() errors out #1783

Open flawnn opened 3 months ago

flawnn commented 3 months ago

What happened?

When calling deleteRealm() after ensuring that the realm is closed (by realm.isClosed), I still get a RealmException:

RealmException: Cannot delete files of an open Realm: '.../6634c5a3b297ab517bc46ad0/default.realm' is still in use.. Error code: 2012.

I just found this tangentially related issue which mentioned waiting or pausing the syncsession - I just wonder why the isClosed property is true then.

I am closing the realm the following way:

  @override
  Future<void> closeRealm() async {
    final config = realm?.config;
    realm?.syncSession.pause();
    realm?.close();
    await atlasApp.currentUser?.logOut();

    await waitUntil(() => (realm?.isClosed ?? true) == true);

    Realm.deleteRealm(config!.path);
  }

Repro steps

  1. Close the realm and get the exception

Version

3.22.3

What Atlas Services are you using?

Atlas Device Sync

What type of application is this?

Flutter Application

Client OS and version

Android 14

Code snippets

No response

Stacktrace of the exception/crash you're getting

No response

Relevant log output

I/flutter (30426): │ RealmException: Cannot delete files of an open Realm: '.../6634c5a3b297ab517bc46ad0/default.realm' is still in use.. Error code: 2012.
I/flutter (30426): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
I/flutter (30426): │ #0   _raiseLastError.<anonymous closure> (package:realm_dart/src/handles/native/error_handling.dart:59:9)
I/flutter (30426): │ #1   using (package:ffi/src/arena.dart:124:31)
I/flutter (30426): │ #2   _raiseLastError (package:realm_dart/src/handles/native/error_handling.dart:48:3)
sync-by-unito[bot] commented 3 months ago

➤ PM Bot commented:

Jira ticket: RDART-1090

nirinchev commented 3 months ago

The closed property refers to that particular instance and doesn't take into account other components or processes that might be using the realm file at that time. As it was likely the case in the other issue you found, the sync component might hold on to the realm file for a while and prevent you from immediately deleting it. You can either stop the sync session or retry the deletion with some backoff.

flawnn commented 3 months ago

The closed property refers to that particular instance and doesn't take into account other components or processes that might be using the realm file at that time. As it was likely the case in the other issue you found, the sync component might hold on to the realm file for a while and prevent you from immediately deleting it. You can either stop the sync session or retry the deletion with some backoff.

How can I stop the sync session in Dart?

nirinchev commented 3 months ago

realm.syncSession.pause()

richard457 commented 2 months ago

this

 ProxyService.cron.isolateKill();
  final path = realm!.config.path;
  // You must close a realm before deleting it
  if (realm != null) {
    realm!.syncSession.pause();
    realm!.close();
  }

  // Delete the realm
  Realm.deleteRealm(path);

code is not working close is called but not closing the DB any suggestion? cc @nirinchev

Dararii commented 3 weeks ago

Same issue here with Local (non-sync) Realm version 20.0.0. Any idea about the workaround?