Skip to content

GH-99293: Fix stale method caches and assertion errors in SWIG-generated modules #99294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an issue where extension modules could inadvertently trigger an assertion error in typeobject.c by clearing the Py_TPFLAGS_VALID_VERSION_TAG tp_flag without clearing the tp_version_tag field. (Extension modules should use PyType_Modified() instead, however.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed Py_TPFLAGS_VALID_VERSION_TAG isn't actually documented.

Suggested change
Fix an issue where extension modules could inadvertently trigger an assertion error in typeobject.c by clearing the Py_TPFLAGS_VALID_VERSION_TAG tp_flag without clearing the tp_version_tag field. (Extension modules should use PyType_Modified() instead, however.)
Fix an issue where extension modules could inadvertently trigger an assertion error in ``typeobject.c`` by clearing the ``Py_TPFLAGS_VALID_VERSION_TAG`` :c:member:`~PyTypeObject.tp_flags` without clearing the :c:member:`~PyTypeObject.tp_version_tag` field. (Extension modules should use :c:func:`PyType_Modified` instead, however.)

9 changes: 7 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4176,12 +4176,17 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
unsigned int h = MCACHE_HASH_METHOD(type, name);
struct type_cache *cache = get_type_cache();
struct type_cache_entry *entry = &cache->hashtable[h];
/* Some extension modules (e.g. those generated by SWIG) don't always
* change/clear tp_version_tag when they clear the
* Py_TPFLAGS_VALID_VERSION_TAG tp_flag, so check both tp_version_tag
* and the tp_flag. (Invalidated cache entries are replaced below.)
*/
if (entry->version == type->tp_version_tag &&
entry->name == name) {
entry->name == name &&
_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
#if MCACHE_STATS
cache->hits++;
#endif
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
return entry->value;
}

Copy link
Member

@Fidget-Spinner Fidget-Spinner Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other reviewers wondering what happens down this code path if Py_TPFLAGS_VALID_VERSION_TAG is not set:

  1. A normal lookup happens where we walk the MRO.
  2. We assign a new version tag to the type. This sets Py_TPFLAGS_VALID_VERSION_TAG.

So this should be safe.

Expand Down