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

Circular references throw stack overflows #16

Closed
rtfeldman opened this issue Nov 20, 2014 · 9 comments
Closed

Circular references throw stack overflows #16

rtfeldman opened this issue Nov 20, 2014 · 9 comments

Comments

@rtfeldman
Copy link
Owner

If you call Immutable(obj) on an obj that contains a circular reference, you get a stack overflow as it tries to clone it.

I don't think supporting objects containing circular references is a good idea, but you should at least get a nice error message.

A heuristic could be useful here for performance, e.g. start by just keep count of how big the stack is getting; if it gets over (let's say) 1,000 frames, start over with circular reference detection.

@rtfeldman rtfeldman added the bug label Nov 20, 2014
@rtfeldman rtfeldman self-assigned this Nov 20, 2014
@alanhogan
Copy link

Is it undesirable or impossible for immutable data structures to handle cycles?

@alanhogan
Copy link

A better question is: why do you think it is unwise to support circular references? I am merely curious, of course

@rtfeldman
Copy link
Owner Author

Just that it's time-consuming both from an implementation and from a performance perspective. It would entail building up an intermediate data structure just to track where things need to end up, then converting that back into a cloned object with references appropriately reconnected...

This came up because I accidentally called Immutable on a React component (which contained a circular reference), and it just threw a stack overflow, which made it harder to figure out what I'd done wrong. As soon as I realized the root cause, it was easy to fix, so having a check that results in an exception is all I need to avoid that particular issue in the future. 😄

@rtfeldman
Copy link
Owner Author

That said, if it's something someone actually needs, I'm not aware of any reason it couldn't be done.

@alanhogan
Copy link

Yep, makes total sense. seems smart to me

@crudh
Copy link
Collaborator

crudh commented Apr 20, 2015

Now that there is a dev/prod mode maybe it would make sense to check this only in development mode? That way most cases can be found without sacrificing any performance.

A really simple way would be to do a test before making it immutable, like:
JSON.stringify(object);

If it contains a circular reference it will throw a TypeError with the message "Converting circular structure to JSON".

@rtfeldman
Copy link
Owner Author

Great point! It could also then console.error(object) so you could actually see the problematic object in question.

@vegetabill
Copy link

FYI I was creating an immutable copy of a Backbone object that was using the backbone-associations add-on and ran into this. immutablejs handles them fine but I liked the sound of this library for all the reasons mentioned in your blog: http://noredinktech.tumblr.com/post/107617838018/switching-from-immutable-js-to-seamless-immutable

@rtfeldman
Copy link
Owner Author

Resolved by #119

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

No branches or pull requests

4 participants