Skip to content

Cannot unpickle DocumentNode #112

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

Closed
corydolphin opened this issue Oct 20, 2020 · 6 comments
Closed

Cannot unpickle DocumentNode #112

corydolphin opened this issue Oct 20, 2020 · 6 comments
Assignees
Labels
discussion Needs more discussion help wanted Extra attention is needed investigate Needs investigaton or experimentation

Comments

@corydolphin
Copy link

Reporting issues with GraphQL-core 3

Unpickling of DocumentNode(s) fail since unpickling a FrozenList fails. I believe the root issue is that List is extended, and pickle assumes it may call append, resulting in a FrozenError.

@Cito Cito self-assigned this Oct 20, 2020
@Cito Cito added the investigate Needs investigaton or experimentation label Oct 20, 2020
@Cito
Copy link
Member

Cito commented Oct 20, 2020

Hm. This is not nice.

Actually I think it was a mistake to use a FrozenList runtime type for these attributes. It would have been better to use FrozenList as a protocol only. I had so far shied away from using protocols because they were introduced in Py 3.8 only. But I think we can import typing_extensions for older Python version.

Will try to work on it this week.

@corydolphin
Copy link
Author

corydolphin commented Oct 20, 2020 via email

@Cito
Copy link
Member

Cito commented Oct 20, 2020

Good point. You're right, Mapping is immutable already. So Sequence and Mapping look like the proper types to use where I currently use FrozenList and FrozenDict.

I think we should show a deprecation warning when somebody imports the FrozenList or FrozenDict modules telling them to use List/Sequence or Dict/Mapping instead, and then eventually remove these modules.

If you like, you can send a PR otherwise I will work on this. Make sure the tox tests are passing before submittting.

@Cito
Copy link
Member

Cito commented Nov 7, 2020

@corydolphin I like to move this forward, but I have limited time, so I would be glad if you or anybody else could help.

The first thing we need is a test that makes sure that document asts can be properly pickled and unpickled. So I added this at the bottom of test_schema_parser.py:

    def can_pickle_and_unpickle_kitchen_sink_schema(kitchen_sink_sdl):  # noqa: F811
        sys.setrecursionlimit(2000)
        doc = parse(kitchen_sink_sdl)
        dumped = pickle.dumps(doc)
        loaded = pickle.loads(dumped)
        assert loaded == doc

This test fails due to the FrozenError issue and passes if we replace the FrozenLists with an ordinary list.

However, it baffles me that I had to set sys.setrecursionlimit(2000) to make it pass, otherweise I get a RecursionError. The kitchen_sink document is actually not that complicated or deeply nested. Does anybody understand what's going on here?

@Cito Cito added discussion Needs more discussion help wanted Extra attention is needed labels Nov 7, 2020
@Cito
Copy link
Member

Cito commented Dec 28, 2021

However, it baffles me that I had to set sys.setrecursionlimit(2000) to make it pass, otherweise I get a RecursionError. The kitchen_sink document is actually not that complicated or deeply nested. Does anybody understand what's going on here?

I have investigated this now. The reason is that the AST nodes keep a reference to their locations which keep a reference to their tokens which form a double-linked-list. Pickling this list is possible, but very expensive. The simplest solution is to modify the Token class so that the links are not pickled. They are only needed while parsing or for debugging purposes, not for using a schema.

Cito added a commit that referenced this issue Dec 28, 2021
These utility classes were a bit awkward and e.g. caused problems
with pickle since a list object is assumed to be appendable (#112).

Instead of FrozenLists, we now use homogeneous tuples to guarantee
that node lists and other collections are immutable. We also use the
Collection and Mapping types to indicate immutability.

Ordinary tuples are more lightweight and pythonic than FrozenLists.

We also exclude the links when pickling Tokens in order to make
pickling GraphQL schemas (which contain the tokens) less expensive.
@Cito
Copy link
Member

Cito commented Dec 28, 2021

This has now been solved in b9423b7 by using tuples instead of FrozenLists for lists of nodes, and by not pickling the Token links, as explained above. Will be available in v3.2.0rc4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Needs more discussion help wanted Extra attention is needed investigate Needs investigaton or experimentation
Projects
None yet
Development

No branches or pull requests

2 participants