Skip to content

trio.Cancelled can't be pickled #3248

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
hotpxl opened this issue Apr 10, 2025 · 1 comment · Fixed by #3250
Closed

trio.Cancelled can't be pickled #3248

hotpxl opened this issue Apr 10, 2025 · 1 comment · Fixed by #3250

Comments

@hotpxl
Copy link

hotpxl commented Apr 10, 2025

trio.Cancelled can't be constructed directly so it can't be pickled. Usually this is fine as no one needs to pickle and move around a trio.Cancelled object. But sometimes it gets trapped in some other exception's __context__ or __cause__.

One example is

async def test():
    with trio.fail_after(0.1):
        await trio.sleep(5)

try:
    trio.run(test)
except trio.TooSlowError as e:
    print(e.__context__)

(By default __context__ of an exception doesn't get pickled but tblib.pickling_support adds it, which I think is reasonable?)

And now we can't pickle this TooSlowError anymore.

Here's the full example:

from tblib import pickling_support
pickling_support.install(trio.TooSlowError)

async def test():
    with trio.fail_after(0.1):
        await trio.sleep(5)

try:
    trio.run(test)
except trio.TooSlowError as e:
    b = e

import pickle
pickle.loads(pickle.dumps(b))

Can we add pickle support for trio.Cancelled?

@A5rocks
Copy link
Contributor

A5rocks commented Apr 11, 2025

Prior discussion in #3105. (well "discussion" as in @jakkdl saying this would be nice polish :^)

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 a pull request may close this issue.

2 participants