Skip to content

bpo-43441: fix [Subinterpreters]: global variable next_version_tag cause method cache bug #24822

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

Merged
merged 2 commits into from
Mar 13, 2021
Merged
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
15 changes: 10 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class object "PyObject *" "&PyBaseObject_Type"
PyUnicode_IS_READY(name) && \
(PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE)

// 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 = 0;

Expand Down Expand Up @@ -252,8 +253,9 @@ _PyType_InitCache(PyInterpreterState *interp)


static unsigned int
_PyType_ClearCache(struct type_cache *cache)
_PyType_ClearCache(PyInterpreterState *interp)
{
struct type_cache *cache = &interp->type_cache;
#if MCACHE_STATS
size_t total = cache->hits + cache->collisions + cache->misses;
fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n",
Expand All @@ -267,7 +269,10 @@ _PyType_ClearCache(struct type_cache *cache)
#endif

unsigned int cur_version_tag = next_version_tag - 1;
next_version_tag = 0;
if (_Py_IsMainInterpreter(interp)) {
next_version_tag = 0;
Copy link
Member

Choose a reason for hiding this comment

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

It would be safer to have a per-interpreter next_version_tag. The problem is that there are still many static types which are shared between interpreters :-/ You may add a comment explaining that:

// bpo-42745: next_version_tag remains shared by all interpreters because of static types

Or put the comment on the variable definition:

// Used to set PyTypeObject.tp_version_tag
static unsigned int next_version_tag = 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, ok i added

}

type_cache_clear(cache, 0);

return cur_version_tag;
Expand All @@ -277,15 +282,15 @@ _PyType_ClearCache(struct type_cache *cache)
unsigned int
PyType_ClearCache(void)
{
struct type_cache *cache = get_type_cache();
return _PyType_ClearCache(cache);
PyInterpreterState *interp = _PyInterpreterState_GET();
return _PyType_ClearCache(interp);
}


void
_PyType_Fini(PyInterpreterState *interp)
{
_PyType_ClearCache(&interp->type_cache);
Copy link
Member

Choose a reason for hiding this comment

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

This change leaks memory: type_cache_clear() clears objects and should be done in subinterpreters.

I suggest to pass interp to _PyType_ClearCache() and check _Py_IsMainInterpreter(interp) in this function to decide if next_version_tag must be modified or not.

Copy link
Contributor Author

@JunyiXie JunyiXie Mar 11, 2021

Choose a reason for hiding this comment

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

Thank you for your review, it is very reasonable. I have made the changes as you suggested.

_PyType_ClearCache(interp);
if (_Py_IsMainInterpreter(interp)) {
clear_slotdefs();
}
Expand Down