Skip to content

Kombu may rely on locking in functools.cached_property that will be removed in Python 3.12 #1668

@carljm

Description

@carljm

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.

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