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 at path already opened with different sync user" upon reopening with identical configuration #6461

Closed
craigdrayton opened this issue Feb 3, 2024 · 5 comments
Labels

Comments

@craigdrayton
Copy link

craigdrayton commented Feb 3, 2024

How frequently does the bug occur?

Always

Description

I have a React Native app with Realm and Device Sync configured as per the starter docs.

I am trying to use Expo TaskManager to run a background task where location data is written to Realm.

The background task runs in global context, outside of React, so I cannot access use hooks to access the Realm context.

So I attempt to connect to the Realm with Realm.open(), using identical configuration and the app.currentUser (anonymous auth) but I get this error:

TaskManager: Task "LOCATION_TRACK" failed: [Error: Realm at path '/data/data/[redacted]/files/mongodb-realm/[redacted]/[redacted ID]/flx_sync_default.realm' already opened with different sync user.]

I understood from this comment that I should be able to open the Realm twice successfully without error:
#6273 (comment)

Stacktrace & log output

TaskManager: Task "LOCATION_TRACK" failed: [Error: Realm at path '/data/data/[redacted]/files/mongodb-realm/[redacted]/[redacted ID]/flx_sync_default.realm' already opened with different sync user.]

Can you reproduce the bug?

Always

Reproduction Steps

Create an expo app with Realm and Device Sync configured as per the starter docs.

Realm works correctly inside React using hooks.

Attempt to open the Realm outside the React context, using the same config, and get the error.

const app = new Realm.App({ id: ATLAS_APP_ID });

    const realm = await Realm.open({
      schema: [LocationLog],
      sync: {
        flexible: true,
        user: app.currentUser,
      },
    });

Version

^12.6.0

What services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

Windows

Build environment

No response

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Feb 5, 2024

@craigdrayton From reading the documentation of Expo TaskManager it is unclear if a task is a thread or a process. The error message you see indicates that it is a process, since Realm currently doesn't support multi-process access for synced Realm (work in process but it will take some time before it is released).

You might consider using the same trick as our Electron users apply: One process handles device sync, and other processes open the Realm with the sync configuration set to true (yes, literally sync: true,).

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Feb 5, 2024
@craigdrayton
Copy link
Author

Thanks for your reply @kneth, really appreciate the help.

When I try the following in the background task:

const realm = await new Realm.open({
    schema: [LocationLog],
    sync: true,
  });

I get: Expected 'sync' on realm configuration to be an object, got a boolean.

If I pass the following config to RealmProvider in React Native:

  const realmConfig = {
    schema: [LocationLog],
    sync: true,
    closeOnUnmount: false,
  };

I get: Expected 'partitionValue' on realm sync configuration to be an integer, string, ObjectId, UUID, or null, got undefined.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Feb 5, 2024
@craigdrayton
Copy link
Author

craigdrayton commented Feb 6, 2024

Downgrading to Realm 11.10.2 and @realm/react 0.5.2 resolves the validation errors (perhaps the sync: true workaround broke with version 12).

I haven't been able to get the sync: true workaround to work copying the pattern from the Electron example.

But it looks like I have a different workaround working: using a different realm path for the background tasks from the foreground React realm. Items added via the background realm are synchronized to the foreground realm.This works in Realm 11 but not Realm 12.

@kneth
Copy link
Contributor

kneth commented Feb 6, 2024

perhaps the sync: true workaround broke with version 12

Yes, for v12, you can use openSyncedRealmLocally (set it to true - and no sync configuration).

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Needs-Attention Reporter has responded. Review comment. labels Feb 7, 2024
@nirinchev
Copy link
Member

Closing this as stale. Feel free to reopen if you have more questions/Kenneth's suggestion didn't work.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2024
@sync-by-unito sync-by-unito bot reopened this Mar 20, 2024
@sync-by-unito sync-by-unito bot closed this as completed Mar 20, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants