Repository navigation
Make weakref thread-safe without the GIL #111926
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement3.13only security fixesonly security fixes
on Nov 9, 2023 Maybe we should check the performance impact the default build first.
I will try to investigate it.The critical section APIs are no-ops on the default build:
cpython/Include/internal/pycore_critical_section.h
Lines 107 to 113 in 289af86
#else /* !Py_NOGIL */ // The critical section APIs are no-ops with the GIL. # define Py_BEGIN_CRITICAL_SECTION(op) # define Py_END_CRITICAL_SECTION() # define Py_BEGIN_CRITICAL_SECTION2(a, b) # define Py_END_CRITICAL_SECTION2() #endif /* !Py_NOGIL */ I know that there is no op for the default build but the commit you suggested modify some internal structure, I just want to know ovehead from the structure changed, even I actually do not expect real overhead from some refactoring. (I didn't look at deeply now please let me know if I miss something)
@corona10 the commit I linked to (in nogil-3.12) is not how I intend to make weakref thread-safe in CPython 3.13. I wouldn't spend too much time investigating it because it's not going to make it's way into 3.13. I linked to it because it's sometimes helpful for me to look back at the nogil-3.12 implementation.
Reacted by Donghee NaBy the way, I am working on it :)
Reacted by Sam Gross- added 5 commits that reference this issue
on Nov 17, 2023 34 remaining items
- added 2 commits that reference this issue
on Apr 2, 2024 - added 4 commits that reference this issue
on Apr 17, 2024
Feature or enhancement
The current weakref implementation relies on the GIL for thread-safety.
The
nogil-3.12fork substantially modifies the weakref implementation. I think we can implement a simpler change in CPython 3.13 (main) now that all PyObject's have their own mutex (in--disable-gilbuilds).Basic idea
Protect access to the weakrefs linked list using the mutex in the weakly referenced object. Use the critical section API.
Prior implementation: colesbury/nogil-3.12@0dddcb6f9d
Linked PRs