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

Realm is closed but deleteRealm() errors out #1783

Open
flawnn opened this issue Aug 15, 2024 · 6 comments
Open

Realm is closed but deleteRealm() errors out #1783

flawnn opened this issue Aug 15, 2024 · 6 comments

Comments

@flawnn
Copy link

flawnn commented Aug 15, 2024

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)
Copy link

sync-by-unito bot commented Aug 15, 2024

➤ PM Bot commented:

Jira ticket: RDART-1090

@nirinchev
Copy link
Member

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
Copy link
Author

flawnn commented Aug 26, 2024

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
Copy link
Member

realm.syncSession.pause()

@richard457
Copy link

richard457 commented Sep 9, 2024

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
Copy link

Dararii commented Oct 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants