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

Fix bug regarding serializing custom exceptions which occurs in multiprocessing context #203

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

basnijholt
Copy link
Collaborator

Summary

The bug can be reproduced with:

from qiskit_ionq.exceptions import IonQAPIError
import pickle
err = IonQAPIError(message='hi', status_code=400, headers='hi', body='hi', error_type='hi')
pickle.loads(pickle.dumps(err))

which gives:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 4
      2 import pickle
      3 err = IonQAPIError(message='hi', status_code=400, headers='hi', body='hi', error_type='hi')
----> 4 pickle.loads(pickle.dumps(err))

TypeError: IonQAPIError.__init__() missing 4 required positional arguments: 'status_code', 'headers', 'body', and 'error_type'

@basnijholt basnijholt marked this pull request as draft October 23, 2024 17:19
@basnijholt basnijholt marked this pull request as ready for review October 23, 2024 17:23
@amritpoudelionq
Copy link

@basnijholt also specify OS?

@basnijholt
Copy link
Collaborator Author

basnijholt commented Oct 23, 2024

@amritpoudelionq, the problem occurs on any OS or architecture.

@@ -168,18 +168,12 @@ def __str__(self):
f"error_type={self.error_type!r})"
)

def __getstate__(self) -> object:
def __reduce__(self):
Copy link

@amritpoudelionq amritpoudelionq Oct 23, 2024

Choose a reason for hiding this comment

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

do we understand why __reduce__ works? __getstate__ and __setstate__ are pretty standard way for serialization?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i just tested this out and it seems like __getstate__ and __setstate__ are relying on an instantiated object (which is why we see the error TypeError: IonQAPIError.__init__() missing 4 required positional arguments: 'status_code', 'headers', 'body', and 'error_type') but __reduce__ returns the class and arguments to instantiate it.

@basnijholt basnijholt merged commit b229e12 into qiskit-community:main Oct 24, 2024
16 checks passed
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.

4 participants