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]: Unable to open realm on background isolate #1433

Closed
geosebas opened this issue Nov 22, 2023 · 9 comments · Fixed by #1445 or #1470
Closed

[Bug]: Unable to open realm on background isolate #1433

geosebas opened this issue Nov 22, 2023 · 9 comments · Fixed by #1445 or #1470
Assignees

Comments

@geosebas
Copy link

geosebas commented Nov 22, 2023

What happened?

Code that worked and still work on realm 1.5.0 stop working since update. I downgraded for now, so it's not urgent.

I currently don't have time to create a sample and test sample with the new version, but since it work nicely on 1.5 and not with 1.6 and I didn't see anything on the changelog related to this, I assume the problem here is on your side.

Repro steps

Nearly a years ago I got the same problem (issue here #1055)

Version

Flutter 3.16.0

What Atlas Services are you using?

Both Atlas Device Sync and Atlas App Services

What type of application is this?

Flutter Application

Client OS and version

With realm 1.6.0 on both android and ios

Code snippets

No response

Stacktrace of the exception/crash you're getting

flutter: [ERROR] : PlayerSearchHelper.initData cannot be completed :
flutter: RealmException: Error opening realm at path /Users/geoffreysebastianelli/Library/Developer/CoreSimulator/Devices/3770FA2B-8574-473C-A9A7-ECDF0BD3F659/data/Containers/Data/Application/376EA245-397C-4FDC-9660-CB4A02CD5437/Documents/mongodb-realm/flex.638118ad44927095598ea5fa.realm. Error code: 2013 . Message: Realm at path '/Users/geoffreysebastianelli/Library/Developer/CoreSimulator/Devices/3770FA2B-8574-473C-A9A7-ECDF0BD3F659/data/Containers/Data/Application/376EA245-397C-4FDC-9660-CB4A02CD5437/Documents/mongodb-realm/flex.638118ad44927095598ea5fa.realm' already opened with different sync user.
flutter: #0      _RealmCore.throwLastError.<anonymous closure> (package:realm/src/native/realm_core.dart:156:7)
flutter: #1      using (package:ffi/src/arena.dart:124:31)
flutter: #2      _RealmCore.throwLastError (package:realm/src/native/realm_core.dart:150:5)
flutter: #3      _RealmLibraryEx.invokeGetPointer (package:realm/src/native/realm_core.dart:2993:17)
flutter: #4      _RealmCore.openRealm (package:realm/src/native/realm_core.dart:661:32)
flutter: #5      Realm._openRealm (package:realm/src/realm_class.dart:232:22)
flutter: #6      new Realm._ (package:realm/src/realm_class.dart:178:98)
flutter: #7      new Realm (package:realm/src/realm_class.dart:176:38)
flutter: #8      RealmService.initRealm (package:shapify/core/services/realm_service.dart:150:16)
flutter: #9      PlayerSearchHelper.initData (package:shapify/ui/shared/player_search_helper.dart:139:28)
flutter: #10     compute.<anonymous closure> (package:flutter/src/foundation/_isolates_io.dart:19:20)
flutter: #11     _RemoteRunner._run (dart:isolate:1090:47)
flutter: #12     _RemoteRunner._remoteExecute (dart:isolate:1084:12)
flutter: #13     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:17)
flutter: #14     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
flutter:

Relevant log output

No response

@nirinchev
Copy link
Member

Hey @geosebas, thanks for the report here. There were some changes related to how users are handled under the hood and it's possible we've hit a regression there. I'll try to come up with a repro case in a test based on the issue description, but if I fail to prepare one, I'll reach back to you for pointers.

@nirinchev
Copy link
Member

Hey @geosebas, I was able to reproduce this and it turns out it never really worked as it should have and if it did, it was by accident. The changes in #1445 don't fix it as much as introduce a different - and reliable - way of opening Realms on a background isolate. So instead of reconstructing the app on the background isolate, you'd need to first construct the App on the main thread and then call App.getById on the background isolate - once you do that, you can obtain a user from the background app instance and open the Realm as usual. The test there is a bit more convoluted than that, but the tl;dr is:

// On the main thread
final appId = 'my-app-abcde';
final app = App(AppConfiguration(appId));

// Later
await Isolate.spawn((List<Object> args) async {
  final app = App.getById(appId);
  final user = app.currentUser!;
  final realm = Realm(FlexibleSyncConfiguration(user));
  //  use realm as usual
});

@richard457
Copy link

richard457 commented Jan 23, 2024

As new release was published I was referring to your example Realm(FlexibleSyncConfiguration(user)); but I get the error The class 'FlexibleSyncConfiguration' doesn't have an unnamed constructor. should we open it as normal Configuration.flexibleSync( user, []) ?? in background isolate? ``` @nirinchev @nielsenko can you point me to documentation on how to use this?

@nirinchev
Copy link
Member

Hey @richard457, you're right - I was typing that directly on Github and seem to have mixed up some C# in there. You should indeed use Configuration.flexibleSync.

@Shreedhar73
Copy link

Shreedhar73 commented Feb 2, 2024

@nielsenko

I am uploading the video to api.video using theirs uploader plugin . video_uploader . They are using Workmanager to upload the video on background .

So when, I initialize the upload process, then terminate the app, (video continues to upload ) and then I try to reopen the app. I am getting ::

Realm at path '/data/data/com.yipl.onlyever/files/mongodb-realm/oe-phase1-tkmsy/653f43b64509e869245a3995/default.realm' already opened with different sync user.. Error code: 2013

Is there anyway I can handle this ?

@nirinchev
Copy link
Member

Can you create a new issue and share the code snippets you're using to open the Realm file?

@geosebas
Copy link
Author

Hello @nielsenko, hope you're well.
I hadn't worked since it worked well with previous version, but now it's time to work !

Everything is working well on my side, in fact too well ^^
I mean I didn't even need to do anything from 1.50 to 1.9.0.
I saw in #1445 so that we should receive a warning if we still use AppConfiguration and not App.getById in isolate but I didn't get anything in log. Is it normal ?
Since you said it, I still changed to App.getById but wanted to know if it's really useful ?

@nielsenko
Copy link
Contributor

@geosebas Unless you setup a listener on Realm.logger you won't see it. Something like:

final subscription = Realm.logger.onRecord.listen((event) {
  print('$event');
});

(of the top of my head)..

But yes, you should use App.getById on the background isolate.

@geosebas
Copy link
Author

Oh I didn't know about this ! Thanks for the help :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants