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

ORM: raise when trying to pickle instance of Entity #5549

Merged
merged 1 commit into from
Jun 1, 2022

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented May 31, 2022

Fixes #5285

The ORM entities should not be pickled since it is not clear whether the
connection to the storage backend should be included as well and if it
were, implementing that in a consistent way across implementations is
not feasible.

Therefore, we now raise an explicit an InvalidOperation when an
instance of Entity is being pickled. This is achieved by overriding
the __getstate__ dunder method which will be called by the pickle
module when dumping a class instance.

The ORM entities should not be pickled since it is not clear whether the
connection to the storage backend should be included as well and if it
were, implementing that in a consistent way across implementations is
not feasible.

Therefore, we now raise an explicit an `InvalidOperation` when an
instance of `Entity` is being pickled. This is achieved by overriding
the `__getstate__` dunder method which will be called by the `pickle`
module when dumping a class instance.
@sphuber sphuber requested a review from ltalirz June 1, 2022 05:59
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sphuber

What if the connection to the storage backend was not included?
Would the resulting unpickled orm objects still be usable?

What I don't know is whether anybody has been trying to pickle these so far... this may warrant a question in "Discussions"?

@@ -217,6 +218,10 @@ def __init__(self, backend_entity: BackendEntityType) -> None:
self._backend_entity = backend_entity
call_with_super_check(self.initialize)

def __getstate__(self):
"""Prevent an ORM entity instance from being pickled."""
raise InvalidOperation('pickling of AiiDA ORM instances is not supported.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point the user to another method of serialization that they can use?

@chrisjsewell
Copy link
Member

What I don't know is whether anybody has been trying to pickle these so far

As I mentioned in the issue, I think Entties should definitely not be pickled; what they are is essentially "live views" of a stored item (e.g. database row).
The only situation where I imagine people would try to pickle them, is when storing a process checkpoint. In this case, they should simply be storing the entities PK/UUID, then reloading them

@sphuber
Copy link
Contributor Author

sphuber commented Jun 1, 2022

What I don't know is whether anybody has been trying to pickle these so far... this may warrant a question in "Discussions"?

I think the only case we are aware of is of @jbweston which even failed because there is a recursion error. I am not sure what the actual use case was, but it is very likely that this wouldn't have worked anyway. So I think we should officially block this. There are other open issues to provide a base functionality for (de)serializing entities.

@ltalirz
Copy link
Member

ltalirz commented Jun 1, 2022

@jbweston Could you comment on your use case?

I assume it was about transporting some of the orm entities to the compute node via cloudpickle (?). Of course any operations that would involve the database can't work there...

@jbweston
Copy link

jbweston commented Jun 1, 2022

@ltalirz something like that; indeed it's not an important usecase.

@sphuber
Copy link
Contributor Author

sphuber commented Jun 1, 2022

Thanks @jbweston . In that case I recommend we merge this @ltalirz

@ltalirz ltalirz self-requested a review June 1, 2022 16:33
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

@sphuber sphuber merged commit 1c509c5 into aiidateam:main Jun 1, 2022
@sphuber sphuber deleted the fix/5285/forbid-entity-pickling branch June 1, 2022 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unpickling AiiDA Data nodes raises RecursionError
4 participants