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 instance closed during background (custom) sync causing "Cannot access realm that has been closed" error on realm.write() #5731

Closed
Acetyld opened this issue Apr 13, 2023 · 4 comments
Labels

Comments

@Acetyld
Copy link

Acetyld commented Apr 13, 2023

How frequently does the bug occur?

Always

Description

We have a sync function that syncs our server with Realm, which can take a while. If the user sends the app to the background during this process, the sync continues for a few seconds before the app is suspended. During this time, the Realm instance is closed (i think?),_ causing the realm.write() call to fail and resulting in the "Cannot access realm that has been closed" error.
So i think the realm instance is closed when app goes to background and thats why we get this error.
Sentry is getting spammed by it.

In sentry we see it always happens after event:

app.lifecycle
{
state: background
}

Stacktrace & log output

app.lifecycle
{
state: background
}

After that: 

Error: Exception in HostFunction: Cannot access realm that has been closed.

Can you reproduce the bug?

Always

Reproduction Steps

Step 1.
Start the sync passing the realm
Imagine this takes like 5 minutes.

const { useRealm } = RealmContext;
const realm = useRealm();

//Fake for each loop to reproduce bug.
for (let i = 0; i < 100; i++) {
  updateSyncRealm({realm, schema: TasksSchema, data: {}, extraData: {}});
}


export const updateSyncRealm = ({ realm, schema, data, extraData }) => {
  realm.write(() => {
    for (const item of data) {
      realm.create(
        schema.schema.name,
        schema.add({ ...item, ...extraData }, realm),
        'modified',
      );
    }
  });
};

Step 2

Make the app go in background.
After this the realm.write will be triggered.

Step 3

Expect the following error to happen:
Cannot access realm that has been closed.

Version

"realm": "^11.7.0", "@realm/react": "^0.4.3",

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

"react-native": "0.71.4",

Build environment

Which debugger for React Native: ..
Flipper

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Apr 14, 2023

@Acetyld On iOS you are likely to take special measures to keep Realm working in the background. You can take a look at realm/realm-core#5731 (comment) to see how other users have solved it.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Apr 14, 2023
@Acetyld
Copy link
Author

Acetyld commented Apr 28, 2023

Yhea for us it is not happing in "real background" it happens directly when the app is only like 3/4 seconds in background, and it also happens on android.

Maybe its something due to realm.write being called rappidly bcs of sync, maybe i should add a realm if (!realm.isClosed()) {
but this seems cumbersome @kneth

@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 Apr 28, 2023
@kneth
Copy link
Contributor

kneth commented May 12, 2023

@Acetyld Sorry for the late reply. Is it possible for you to create a small app which can reproduce this?

@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 May 12, 2023
@sync-by-unito sync-by-unito bot closed this as completed Jun 27, 2023
@sync-by-unito
Copy link

sync-by-unito bot commented Jun 27, 2023

➤ takameyer commented:

Closed for inactivity. Please open again when you can provide a reproduction.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 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
@kneth @Acetyld and others