The next_version_tag in typeobject.c is currently not thread safe and relies on GIL to protect against concurrent increments across threads.
|
// 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_tag counter 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_tag an atomic counter and increment it atomically using the pyatomic APIs. This is my preferred solution since next_version_tag is only modified when a type is modified so is a rare operation and not a performance issue. Since this is just a counter relaxed ordering can be used here.
cc @ericsnowcurrently
Linked PRs
The
next_version_tagintypeobject.cis currently not thread safe and relies on GIL to protect against concurrent increments across threads.cpython/Objects/typeobject.c
Lines 46 to 50 in 63140b4
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:
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.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.cc @ericsnowcurrently
Linked PRs