graphql-php icon indicating copy to clipboard operation
graphql-php copied to clipboard

Serialization of double-linked-list of tokens breaks recursion limit

Open darvanen opened this issue 3 years ago • 3 comments

The output of \GraphQL\Language\Parser::parse contains a tree in every 'loc' field which appears to branch forever:

image

Now, it doesn't go forever or it would crash for everyone. But get a schema large enough and it does break the recursion limit (default 4096) in unserialize() when being pulled out of cache (in Drupal).

What purpose do these endless recursive references to 'prev' and 'next' serve? Is it possible they could be limited or removed?

darvanen avatar Jun 08 '22 01:06 darvanen

Or perhaps could we come up with a __serialize() member function to \GraphQL\Language\Token to teach the PHP serializer how to handle it?

darvanen avatar Jun 08 '22 03:06 darvanen

I am going to categorize this as a bug, as I believe that serialization of AST nodes should be supported with default PHP settings.

A quick fix for you would be to use the parser option noLocation and bypass this issue. If you do not need the location, this is more efficient.

What purpose do these endless recursive references to 'prev' and 'next' serve? Is it possible they could be limited or removed?

They are neither endless, nor recursive. The tokens form a double-linked-list. I think this data structure was probably copied from the reference implementation graphql-js. It may be possible to optimize this by using something more well suited for PHP.

Or perhaps could we come up with a __serialize() member function to \GraphQL\Language\Token to teach the PHP serializer how to handle it?

I could see this working too, sure. Traversing the double-linked-list can most certainly be transformed into an iterative algorithm.

spawnia avatar Jun 08 '22 13:06 spawnia

Thanks for the explanation :)

I'm afraid this pattern is very new to me (obviously) so I probably won't be able to help very much but I will if I can.

darvanen avatar Jun 09 '22 00:06 darvanen