- 
          
- 
        Couldn't load subscription status. 
- Fork 33.3k
Closed
Closed
Copy link
Labels
3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-subinterpreters
Description
The next_version_tag in typeobject.c is currently not thread safe and relies on GIL to protect against concurrent increments across threads.
Lines 46 to 50 in 63140b4
| // bpo-42745: next_version_tag remains shared by all interpreters because of static types | |
| // Used to set PyTypeObject.tp_version_tag | |
| static unsigned int next_version_tag = 1; | |
| typedef struct PySlot_Offset { | 
For per-interpreter GIL, it must be made thread-safe otherwise the type cache will be affected by race conditions. Static types are not affected because they are immutable so this is a issue for pure Python classes aka heap types. This issue is for discussion of possible implementations.
Possible Solutions:
- Make the next_version_tagcounter per interpreter, while this may seems to fix the issue but since objects are shared across the interpreters and the type version tag is also used to represent a current state of a type in the specializing interpreter, two interpreters can have same the same tag for different types this won't work as expected.
- Make the next_version_tagan atomic counter and increment it atomically using thepyatomic APIs. This is my preferred solution sincenext_version_tagis only modified when a type is modified so is a rare operation and not a performance issue. Since this is just a counterrelaxed orderingcan be used here.
Linked PRs
Metadata
Metadata
Assignees
Labels
3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-subinterpreters
Projects
Status
Done