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

Infinite recursion #86

Open
sergiopereiraTT opened this issue Sep 17, 2013 · 8 comments
Open

Infinite recursion #86

sergiopereiraTT opened this issue Sep 17, 2013 · 8 comments
Labels

Comments

@sergiopereiraTT
Copy link

I had a bug in my code where, by accident, a reference to the window object was added as an attribute to a NestedModel. That caused an infinite recursion in _setAttr (and subsequent crash) because window has a few self-referencing properties like window.window, window.self, window._top, window.parent, etc.

Even though that was my fault, it revealed that (I think) this could happen with legitimately created recursions (like order.orderLines[0].order or pet.owner.pets[0], etc.)

Maybe NestedModel could try to keep track of the walked objects and avoid the infinite loop.

@afeld
Copy link
Owner

afeld commented Sep 17, 2013

Hmm, I wonder why this wasn't fixed by #80... mind submitting a failing test (that preferably doesn't involve window)?

@sergiopereiraTT
Copy link
Author

I should have mentioned the version I'm running: 1.1.2 from July 16th, which seems to include the fixes from #80. Maybe it only fails for window then.

@sergiopereiraTT
Copy link
Author

OK, I don't have a test but pasting this in a JS console seems to recreate the error:

var father = {name: 'bob'}
var child = {name: 'danny', father: father}
father.children = [child]
var family = new Backbone.NestedModel({lastName: 'Smith', head: father})
family.set({'head.name': 'Bobby'})
family.set({'head.name': 'Robert'})
family.set({'head.name': 'Rob'})
// Now try the same value again:
family.set({'head.name': 'Rob'})
// ==> RangeError: Maximum call stack size exceeded

sergiopereiraTT added a commit to sergiopereiraTT/backbone-nested that referenced this issue Sep 18, 2013
@vkovalskiy
Copy link

Any updates on this going to happen soon?

@gkatsev
Copy link
Collaborator

gkatsev commented Jun 9, 2014

@vkovalskiy unfortunately, both I and @afeld are pretty busy. I'll try to get to this in the following week or two.

@vkovalskiy
Copy link

@gkatsev thanks for reply. However, I believe that the solution would be to simply forbid recursive models because you'll never know when it's ok to stop traversing. I believe the best that can be done here is informing the user that model has recursive attrbutes and throw, possibly stating the problem attribute name.

At least, there would be no browser doom state J

@sergiopereiraTT
Copy link
Author

I agree with @vkovalskiy. Throwing with a nice error message is better than trying to guess the best way to handle it.

@gkatsev
Copy link
Collaborator

gkatsev commented Jun 16, 2014

That may very well be the best way to handle it 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants