Skip to content

hashlib.new cannot be pickled when passed to ProcessPoolExecutor as a hash factory #162716

Not planned

Description

@limadog9

Bug report

Passing hashlib.new to a ProcessPoolExecutor worker as a hash factory raises PicklingError before the worker runs. Passing hashlib.sha256 to the same worker succeeds. The failure can be reproduced without starting a worker or creating a hash object:

import hashlib
import pickle

assert pickle.loads(pickle.dumps(hashlib.sha256)) is hashlib.sha256
assert pickle.loads(pickle.dumps(hashlib.new)) is hashlib.new

The first assertion passes. The second fails in pickle.dumps on CPython 3.16.0a2:

_pickle.PicklingError: Can't pickle <function __hash_new at ...>: it's not found as hashlib.__hash_new

Lib/hashlib.py exports either __hash_new or __py_new as new, then deletes both private names. The exported function retains its private __qualname__, so pickle tries to look it up under a name the module has deleted. With _hashlib unavailable, the same example fails looking up hashlib.__py_new.

To check whether the public name works across backends, I set hashlib.new.__qualname__ = "new" in the sending process. An unmodified receiving process then restored its own hashlib.new. This passed in both directions between the OpenSSL and built-in implementations with pickle protocols 0 through 5.

Related issue #55980 concerns serializing a hash object after construction. This reproducer only serializes the module-level constructor.

Reproduced on Linux with CPython 3.11.18, 3.12.14, 3.13.5, 3.14.10, 3.15.2, 3.16.0a2, and main at 3b30937.

CPython versions tested on:

3.11, 3.12, 3.13, 3.14, 3.15, 3.16, CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    type-bug
    on Nov 27, 2026
  2. sirosen commented on Nov 27, 2026

    @sirosen
    Contributor

    Can you explain why it is important to be able to pickle hashlib.new?
    Not all objects support pickling.

    I'm inclined to close this as a wontfix, unless there is some real motivation for it.

  3. added
    pending
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions