Skip to content

trio.Cancelled can't be pickled #3248

Closed
@hotpxl

Description

@hotpxl

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions