realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Realm 10 concurrent connection crash

Open willatrush opened this issue 4 years ago • 4 comments
trafficstars

Goals

Realm 10 to support concurrent connections

Expected Results

Opening and closing multiple connections should not create the crash "cannot access realm that has been closed."

Actual Results

The second connection throws "cannot access realm that has been closed.", it seems to be released prematurely.

Steps to Reproduce

Note that this problem didn't exist in Realm 6.

Our db layer is implemented as async functions, with each opening and closing connections within the function scope.

This error starts appearing after upgrading to Realm 10.

My guess is that Realm 10 caches and reuses the connection instance, and it is being released prematurely due to reference counting errors You can replicate this with the code attached:

Code Sample

const Realm = require("realm");

const UserSchema = {
  name: "User",
  primaryKey: "id",
  properties: {
    id: "string",
  },
};

it("should work", async () => {
  openConnection();
  // Crashes: cannot access realm that has been closed.
  await openConnection();
});

async function openConnection() {
  const realm = await Realm.open({
    schema: [UserSchema],
  });
  realm.objects("User");
  realm.close();
}

Version of Realm and Tooling

  • Realm js: 10.2.0
  • Node: 15.8.0 React native: 0.63.2 (seems to be Android only)

willatrush avatar Feb 16 '21 23:02 willatrush

Thanks for the report and test-case @willatrush.

We'll need to investigate this a bit further, especially if this is only happening on Android.

steffenagger avatar Feb 17 '21 11:02 steffenagger

I can confirm that this issue exists on iOS platform as well

maximzhemerenko avatar Feb 28 '21 12:02 maximzhemerenko

@steffenagger Hi, have you discovered anything about this issue? I am facing on the same issue and quick stuck at the moment. Any comments would be much appreciated!

Yujink0624 avatar Apr 07 '22 03:04 Yujink0624

@Yujink0624 @steffenagger is no longer with us, so I'll try to provide some context.

When opening a Realm of the same path twice (this is what the test above is doing) a shared instance is created underneath. Closing one of these instances, will close the other realm as well. One possible workaround is to provide two different path values in the configuration.

kraenhansen avatar Apr 07 '22 08:04 kraenhansen