Skip to content

Make hashlib related modules thread-safe without the GIL #111916

Closed
@colesbury

Description

@colesbury

Feature or enhancement

The hashlib based modules already have some locking to make some operations thread-safe (with the GIL), but the logic isn't sufficient if running with the GIL disabled.

Relevant files:

  • Modules/_blake2/blake2b_impl.c
  • Modules/_blake2/blake2s_impl.c
  • Modules/_hashopenssl.c
  • Modules/hashlib.h
  • Modules/md5module.c
  • Modules/sha1module.c
  • Modules/sha2module.c
  • Modules/sha3module.c

Basic idea:

  1. Replace PyThread_type_lock lock with PyMutex. This should be both simpler and faster in general and avoid the need for dynamically assigning a lock, which can pose thread-safety issues without the GIL
  2. Add a field bool use_mutex to indicate if the code should lock the mutex. This should always be set to true in Py_NOGIL. In the default build, we should dynamically set it to true in places where we previously allocated self->lock
  3. Update ENTER_HASHLIB and EXIT_HASHLIB macros.

Linked PRs

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions