-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
gh-117398: Use Per-Interpreter State for the _datetime Static Types #119929
gh-117398: Use Per-Interpreter State for the _datetime Static Types #119929
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the crashes and refleaks on main/sub interpreters, just a rough workaround can be seen in my repo: 6e761d7. Inheritance between _datetime
types seems to make things complicated, unlike the static builtin types.
Modules/_datetimemodule.c
Outdated
if (PyUnstable_AtExit(interp, callback_for_interp_exit, NULL) < 0) { | ||
return -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok to run callback_for_interp_exit(NULL)
directly before the return when PyUnstable_AtExit()
fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that should work fine. Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Objects/typeobject.c
Outdated
if (!isbuiltin) { | ||
PyMutex_Lock(&interp->types.mutex); | ||
index = interp->types.for_extensions.next_index; | ||
interp->types.for_extensions.next_index++; | ||
PyMutex_Unlock(&interp->types.mutex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels safer to me for each _datetime
type to have a constant index value, if the module can be reloaded multiple times before the interpreter-dict gets a key. Index overflow, intermittent indexes are my concerns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can simply not clear the value ever and adjust various asserts accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, there isn't much value in keeping the index fixed between reinits. Furthermore, that wouldn't be an option if multiple extension modules were involved. I'd rather iron this out in later 3.14 changes that we won't be backporting.
static managed_static_type_state * | ||
managed_static_type_state_get(PyInterpreterState *interp, PyTypeObject *self) | ||
{ | ||
// It's probably a builtin type. | ||
size_t index = managed_static_type_index_get(self); | ||
managed_static_type_state *state = | ||
&(interp->types.builtins.initialized[index]); | ||
if (state->type == self) { | ||
return state; | ||
} | ||
if (index > _Py_MAX_MANAGED_STATIC_EXT_TYPES) { | ||
return state; | ||
} | ||
return &(interp->types.for_extensions.initialized[index]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could _datetime
types have their index plus 200 (_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES
) in the tp_subclasses
? Then, is_builten = tp_subclasses <= 200
, and like index = tp_subclasses % 200 - 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I considered doing that and plan on an approach like that eventually. However, for now I'd like to get the simpler approach in for the 3.13 backport.
Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
Good catch. I'll take a look. |
I've taken care of this and did it in a similar way to what you did. Thanks for bringing it up. |
Thanks for saying so! :)
I appreciate this feedback. Normally I'd actually have split this into a number of PRs along the lines you've enumerated. However, the current schedule makes it a bit trickier. |
Misc/NEWS.d/next/Library/2024-06-01-16-58-43.gh-issue-117398.kR0RW7.rst
Outdated
Show resolved
Hide resolved
Thanks @ericsnowcurrently for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…ypes (pythongh-119929) We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. The relevant code could use some cleanup but I opted to avoid the significant churn in this change. I'll tackle that separately. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly. (cherry picked from commit 105f22e) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
GH-120009 is a backport of this pull request to the 3.13 branch. |
…Types (gh-120009) We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly. (cherry picked from commit 105f22e, AKA gh-119929) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Thank you very much for landing this. |
|
…ypes (pythongh-119929) We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. The relevant code could use some cleanup but I opted to avoid the significant churn in this change. I'll tackle that separately. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly.
…ypes (pythongh-119929) We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. The relevant code could use some cleanup but I opted to avoid the significant churn in this change. I'll tackle that separately. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly.
…ypes (pythongh-119929) We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. The relevant code could use some cleanup but I opted to avoid the significant churn in this change. I'll tackle that separately. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly.
We make use of the same mechanism that we use for the static builtin types. This required a few tweaks.
The relevant code could use some cleanup but I opted to avoid the significant churn in this PR. I'll tackle that separately.
This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly.
(Note for reviewers: this PR is based on top of gh-119810. Consider reviewing only the commits from 1e3005d ("_PyStaticType_Dealloc() -> _PyStaticType_FiniBuiltin()") down or you can wait for that other PR to be merged first. For simplicity sake: https://github.com/python/cpython/pull/119929/files/1e3005d1874534121aba7de890e91300ac5fac84..HEAD)