Realm-js: linkingObjects results in recursion in debug mode

Created on 26 Nov 2017  路  10Comments  路  Source: realm/realm-js

hi, in my app I use realm.objects to get some data, if I turn off the 'Debug JS Remotely' on iOS simulator, everything works fine, realm.objects will return my data. but if I turn on 'Debug Js Remotely', the 'get_property' will be called more than 25000 times until the error ' RangeError: Maximum call stack size exceeded' be thrown, see the screenshot below:
image

after added some debug codes, I found out It seems like the problem is about 'linkingObjects'. my schema looks like that:
class Goal {
static schema = {
name: 'Goal',
primaryKey: 'id',
properties: {
id: {type: 'int'},
title: 'string',
kpis: 'Kpi[]',
}
}
}

class Kpi {
static schema = {
name: 'Kpi',
primaryKey: 'id',
properties: {
id: {type: 'int'},
title: 'string',
goals: {type: 'linkingObjects', objectType: 'Goal', property: 'kpis'},
}
}
}
I go through thousands of 'get_property' requests, and found out that Goal and KPI will call each other recursively, goal.kpis->kpi.gols->goal.kpis->kpi.goals and on and on until 'Maximum call stack size exceeded'. If I turn off the debugger, the recursion is gone, or If I remove the linkingObjects 'goals' of KPI schema, the recursion is gone too.

sorry for my poor english, if It's not clear enough ,please let me know.

Version of Realm and Tooling

  • Realm JS SDK Version: 2.0.2
  • React Native: 0.50.1
  • Client OS & Version: macOS 10.13.1
  • Which debugger for React Native: React Native Debugger
O-Community P-2-Expected T-Bug

Most helpful comment

This also stops me using reactotron, reactotron sending stringify results to reactotron server, but my realm object contains such recursive relationships.

All 10 comments

Thank you for reporting and taking the time to debug. We need to write a test to reproduce it and fix it.

@YaoHuiJi I think that I am getting the same bug as you. In debug mode the main thread freeze and if I compile in Release my Redux actions aren't triggered anymore.

In both case the app doesn't respond properly.

@kneth Here are the two schema involved:

const User = {
  name: 'User',
  primaryKey: 'id',
  properties: {
    id: 'string',
    username: { type: 'string', indexed: true },
    name: 'string',
    avatar: 'string?',
    gender: 'string',
    birthday: 'string',
    latitude: 'double',
    longitude: 'double',
    relationship: { type: 'int', default: relationshipStatusConstants.NON_EXISTENT },
    interactions: { type: 'linkingObjects', objectType: 'Interaction', property: 'members' },
  },
};

const Interaction = {
  name: 'Interaction',
  primaryKey: 'id',
  properties: {
    id: 'string',
    type: 'string',
    weight: 'int',
    timestamp: 'int',
    members: { type: 'list', objectType: 'User' },
  },
};

Interaction has a collection of User via members and each User schema should point at their Interaction via interactions.

Version of Realm and Tooling

  • Realm JS SDK Version: 2.3.3
  • React Native: 0.54.1
  • Client OS & Version: macOS 10.13.3
  • Which debugger for React Native: Reactotron v1.15.0

@StevenMasini Can you describe the operations which causes your Redux actions not to trigger? Can https://github.com/lolatravel/realm-react-redux or https://stackoverflow.com/questions/40167038/using-realm-in-react-native-app-with-redux help you?

@kneth Thank for your answer. I don't know if realm-react-redux will solve the LinkingObject problem, but it is still a useful resource. We will try to implement it to solve other issues that we are facing with Realm and Redux.

For now, in order to move forward on my project, I just changed the interactions property in User, to a collection of id of Interaction.

After I am not sure what was blocking my Redux actions to trigger, but it seemed like an infinite loop.

@StevenMasini We are eager to hear how it goes when you are using realm-react-redux. It is not a Realm project but we are happy to be part of a community and therefore are we curious to learn how we fit in :smile:.

This bug has the tag "Reproduction Required":
Here you go:
https://github.com/TheNoim/realm-recursion

It would be nice if this gets fixed.

The default way to serialise an object to JSON will print all properties. Since a realm object with a link also has a backwards link (the linkingObject property) the JSON.stringify() (and probably the react debugger) will hit a recursive loop when printing a realm object. We could potentially provide a version of toJSON() on Realm objects which only prints the forward links but I'm not sure if it would be annoying for debugging not being able to see the linkingObjects property - it would be better than crashing in a stack overflow though.

it would be better than crashing in a stack overflow though.

No, I don't think so.
There should be a possibility to serialize a realm object to a string without infinity recursion. Maybe something which prevents to show the link if the same object already go serialized in a higher order.

This also stops me using reactotron, reactotron sending stringify results to reactotron server, but my realm object contains such recursive relationships.

See issue #491 and pr #2373 , I have upgrade realm to 2.28.1 and I think It's not that slow now. thanks those guys, really appreciate.

Was this page helpful?
0 / 5 - 0 ratings