-
-
Notifications
You must be signed in to change notification settings - Fork 959
Description
Hello! This is just a heads-up that the lock in functools.cached_property
was deemed to be a bug (see python/cpython#87634) because it is class-wide, meaning that it can lead to pathological lock contention if there are lots of instances. The chosen resolution for functools.cached_property
was to remove the locking behavior entirely in Python 3.12. This means that in 3.12, it will be possible for a cached_property
getter func to run multiple times for the same instance, if used in a multi-threaded context.
I'm filing this issue because I see that in https://github.com/celery/kombu/blob/main/kombu/utils/objects.py you use functools.cached_property
if available, with a fallback to threaded_cached_property
from the cached_property
third-party library.
If you need the locking, and you aren't affected by high lock contention due to the class-wide nature of the lock, you may want to simply unconditionally rely on cached_property.threaded_cached_property
going forward, and not use functools.cached_property
. Alternately, if you want to use functools.cached_property
, you can replicate the current behavior by putting a threading.RLock
on the class and checking it inside your cached property getter function.