Skip to content

Pickleable httpx.Request is not restored proper. #683

Description

@Kludex

Originally opened by @cloasdata on 2022-04-24 18:04:17 in encode/httpx

importpickleimporttracebackimporthttpxclassMyExtension:
some_stuff="stuff"url="https://github.com"method="GET"extensions= {"a_proper_extension": MyExtension()}
request=httpx.Request(method, url, extensions=extensions)
print(request)
pickled_request=pickle.dumps(request)
print(pickled_request)
repickled_request=pickle.loads(pickled_request)
print(repickled_request)
try:
assertrepickled_request==request# is not the same but should be equalexceptAssertionErrorase:
print(traceback.format_exc())
print("extensions:", repickled_request.extensions) # gone! why

It turns out that httpx.Request can be pickled but loading the serialized byte code will not return the same object at all.
I did not a lot of research on it so far and I cannot tell were exactly the problem occurs in httpx.

Reason for pickling is, that I'm running the client in several child processes and this leads to strange errors like
httpx.StreamClosed: Attempted to read or stream content, but the stream has been closed.

At least some warning should be given, that this class is not safe to pickle even if you can pickle it.

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