summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-101659: Isolate "obmalloc" State to Each Interpreter (gh-101660)Eric Snow2023-04-241-4/+13
| | | | | | | | | | | | | | This is strictly about moving the "obmalloc" runtime state from `_PyRuntimeState` to `PyInterpreterState`. Doing so improves isolation between interpreters, specifically most of the memory (incl. objects) allocated for each interpreter's use. This is important for a per-interpreter GIL, but such isolation is valuable even without it. FWIW, a per-interpreter obmalloc is the proverbial canary-in-the-coalmine when it comes to the isolation of objects between interpreters. Any object that leaks (unintentionally) to another interpreter is highly likely to cause a crash (on debug builds at least). That's a useful thing to know, relative to interpreter isolation.
* gh-103323: Get the "Current" Thread State from a Thread-Local Variable ↵Eric Snow2023-04-241-6/+32
| | | | | | | | | | | (gh-103324) We replace _PyRuntime.tstate_current with a thread-local variable. As part of this change, we add a _Py_thread_local macro in pyport.h (only for the core runtime) to smooth out the compiler differences. The main motivation here is in support of a per-interpreter GIL, but this change also provides some performance improvement opportunities. Note that we do not provide a fallback to the thread-local, either falling back to the old tstate_current or to thread-specific storage (PyThread_tss_*()). If that proves problematic then we can circle back. I consider it unlikely, but will run the buildbots to double-check. Also note that this does not change any of the code related to the GILState API, where it uses a thread state stored in thread-specific storage. I suspect we can combine that with _Py_tss_tstate (from here). However, that can be addressed separately and is not urgent (nor critical). (While this change was mostly done independently, I did take some inspiration from earlier (~2020) work by @markshannon (main...markshannon:threadstate_in_tls) and @vstinner (#23976).)
* GH-103082: Implementation of PEP 669: Low Impact Monitoring for CPython ↵Mark Shannon2023-04-121-5/+35
| | | | | | | | | | (GH-103083) * The majority of the monitoring code is in instrumentation.c * The new instrumentation bytecodes are in bytecodes.c * legacy_tracing.c adapts the new API to the old sys.setrace and sys.setprofile APIs
* gh-103176: sys._current_exceptions() returns mapping to exception instances ↵Irit Katriel2023-04-111-7/+6
| | | | instead of exc_info tuples (#103177)
* gh-100227: Use an Array for _PyRuntime's Set of Locks During Init (gh-103315)Eric Snow2023-04-061-60/+48
| | | This cleans things up a bit and simplifies adding new granular global locks.
* gh-100227: Make the Global PyModuleDef Cache Safe for Isolated Interpreters ↵Eric Snow2023-03-291-22/+106
| | | | | | | | | | | | | | | | | | | | | | | (gh-103084) Sharing mutable (or non-immortal) objects between interpreters is generally not safe. We can work around that but not easily. There are two restrictions that are critical for objects that break interpreter isolation. The first is that the object's state be guarded by a global lock. For now the GIL meets this requirement, but a granular global lock is needed once we have a per-interpreter GIL. The second restriction is that the object (and, for a container, its items) be deallocated/resized only when the interpreter in which it was allocated is the current one. This is because every interpreter has (or will have, see gh-101660) its own object allocator. Deallocating an object with a different allocator can cause crashes. The dict for the cache of module defs is completely internal, which simplifies what we have to do to meet those requirements. To do so, we do the following: * add a mechanism for re-using a temporary thread state tied to the main interpreter in an arbitrary thread * add _PyRuntime.imports.extensions.main_tstate` * add _PyThreadState_InitDetached() and _PyThreadState_ClearDetached() (pystate.c) * add _PyThreadState_BindDetached() and _PyThreadState_UnbindDetached() (pystate.c) * make sure the cache dict (_PyRuntime.imports.extensions.dict) and its items are all owned by the main interpreter) * add a placeholder using for a granular global lock Note that the cache is only used for legacy extension modules and not for multi-phase init modules. https://github.com/python/cpython/issues/100227
* gh-100227: Revert gh-102925 "gh-100227: Make the Global Interned Dict Safe ↵Eric Snow2023-03-271-183/+21
| | | | | | | for Isolated Interpreters" (gh-103063) This reverts commit 87be8d9. This approach to keeping the interned strings safe is turning out to be too complex for my taste (due to obmalloc isolation). For now I'm going with the simpler solution, making the dict per-interpreter. We can revisit that later if we want a sharing solution.
* gh-100227: Make the Global Interned Dict Safe for Isolated Interpreters ↵Eric Snow2023-03-231-21/+183
| | | | | | | | | (gh-102925) This is effectively two changes. The first (the bulk of the change) is where we add _Py_AddToGlobalDict() (and _PyRuntime.cached_objects.main_tstate, etc.). The second (much smaller) change is where we update PyUnicode_InternInPlace() to use _Py_AddToGlobalDict() instead of calling PyDict_SetDefault() directly. Basically, _Py_AddToGlobalDict() is a wrapper around PyDict_SetDefault() that should be used whenever we need to add a value to a runtime-global dict object (in the few cases where we are leaving the container global rather than moving it to PyInterpreterState, e.g. the interned strings dict). _Py_AddToGlobalDict() does all the necessary work to make sure the target global dict is shared safely between isolated interpreters. This is especially important as we move the obmalloc state to each interpreter (gh-101660), as well as, potentially, the GIL (PEP 684). https://github.com/python/cpython/issues/100227
* gh-102304: Move the Total Refcount to PyInterpreterState (gh-102545)Eric Snow2023-03-211-2/+8
| | | | | Moving it valuable with a per-interpreter GIL. However, it is also useful without one, since it allows us to identify refleaks within a single interpreter or where references are escaping an interpreter. This becomes more important as we move the obmalloc state to PyInterpreterState. https://github.com/python/cpython/issues/102304
* gh-102304: Fix Non-Debug Builds (gh-102846)Eric Snow2023-03-201-0/+2
| | | | | Some debug-only code slipped in with gh-102543. https://github.com/python/cpython/issues/102304
* gh-102304: Move _Py_RefTotal to _PyRuntimeState (gh-102543)Eric Snow2023-03-201-0/+3
| | | | | | | The essentially eliminates the global variable, with the associated benefits. This is also a precursor to isolating this bit of state to PyInterpreterState. Folks that currently read _Py_RefTotal directly would have to start using _Py_GetGlobalRefTotal() instead. https://github.com/python/cpython/issues/102304
* gh-102660: Handle m_copy Specially for the sys and builtins Modules (gh-102661)Eric Snow2023-03-141-0/+1
| | | | | | | It doesn't make sense to use multi-phase init for these modules. Using a per-interpreter "m_copy" (instead of PyModuleDef.m_base.m_copy) makes this work okay. (This came up while working on gh-101660.) Note that we might instead end up disallowing re-load for sys/builtins since they are so special. https://github.com/python/cpython/issues/102660
* gh-100227: Move the dtoa State to PyInterpreterState (gh-102331)Eric Snow2023-02-281-1/+19
| | | https://github.com/python/cpython/issues/100227
* GH-102126: fix deadlock at shutdown when clearing thread states (#102222)Kumar Aditya2023-02-251-3/+10
|
* gh-101758: Fix the wasm Buildbots (gh-101943)Eric Snow2023-02-161-0/+4
| | | | | They were broken by gh-101920. https://github.com/python/cpython/issues/101758
* gh-101758: Clean Up Uses of Import State (gh-101919)Eric Snow2023-02-151-140/+11
| | | | | | | | | | | | | | | This change is almost entirely moving code around and hiding import state behind internal API. We introduce no changes to behavior, nor to non-internal API. (Since there was already going to be a lot of churn, I took this as an opportunity to re-organize import.c into topically-grouped sections of code.) The motivation is to simplify a number of upcoming changes. Specific changes: * move existing import-related code to import.c, wherever possible * add internal API for interacting with import state (both global and per-interpreter) * use only API outside of import.c (to limit churn there when changing the location, etc.) * consolidate the import-related state of PyInterpreterState into a single struct field (this changes layout slightly) * add macros for import state in import.c (to simplify changing the location) * group code in import.c into sections *remove _PyState_AddModule() https://github.com/python/cpython/issues/101758
* GH-101578: Normalize the current exception (GH-101607)Mark Shannon2023-02-081-3/+1
| | | | | | | | | | * Make sure that the current exception is always normalized. * Remove redundant type and traceback fields for the current exception. * Add new API functions: PyErr_GetRaisedException, PyErr_SetRaisedException * Add new API functions: PyException_GetArgs, PyException_SetArgs
* gh-59956: Partial Fix for GILState API Compatibility with Subinterpreters ↵Eric Snow2023-02-061-21/+4
| | | | | | | | | | | (gh-101431) The GILState API (PEP 311) implementation from 2003 made the assumption that only one thread state would ever be used for any given OS thread, explicitly disregarding the case of subinterpreters. However, PyThreadState_Swap() still facilitated switching between subinterpreters, meaning the "current" thread state (holding the GIL), and the GILState thread state could end up out of sync, causing problems (including crashes). This change addresses the issue by keeping the two in sync in PyThreadState_Swap(). I verified the fix against gh-99040. Note that the other GILState-subinterpreter incompatibility (with autoInterpreterState) is not resolved here. https://github.com/python/cpython/issues/59956
* gh-59956: Clarify Runtime State Status Expectations (gh-101308)Eric Snow2023-01-301-145/+346
| | | | | | | | | | | | | A PyThreadState can be in one of many states in its lifecycle, represented by some status value. Those statuses haven't been particularly clear, so we're addressing that here. Specifically: * made the distinct lifecycle statuses clear on PyThreadState * identified expectations of how various lifecycle-related functions relate to status * noted the various places where those expectations don't match the actual behavior At some point we'll need to address the mismatches. (This change also includes some cleanup.) https://github.com/python/cpython/issues/59956
* Fix typos in pystate.c file (#101348)Виталий Дмитриев2023-01-261-5/+5
|
* gh-59956: Allow the "Trashcan" Mechanism to Work Without a Thread State ↵Eric Snow2023-01-231-0/+16
| | | | | | | | | | | | (gh-101209) We've factored out a struct from the two PyThreadState fields. This accomplishes two things: * make it clear that the trashcan-related code doesn't need any other parts of PyThreadState * allows us to use the trashcan mechanism even when there isn't a "current" thread state We still expect the caller to hold the GIL. https://github.com/python/cpython/issues/59956
* gh-101181: Fix `unused-variable` warning in `pystate.c` (#101188)Nikita Sobolev2023-01-201-0/+3
| | | Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
* gh-59956: Fix Function Groupings in pystate.c (gh-101172)Eric Snow2023-01-201-237/+352
| | | | | This is a follow-up to gh-101161. The objective is to make it easier to read Python/pystate.c by grouping the functions there in a consistent way. This exclusively involves moving code around and adding various kinds of comments. https://github.com/python/cpython/issues/59956
* gh-59956: Clarify GILState-related Code (gh-101161)Eric Snow2023-01-191-201/+329
| | | | | | | | | | The objective of this change is to help make the GILState-related code easier to understand. This mostly involves moving code around and some semantically equivalent refactors. However, there are a also a small number of slight changes in structure and behavior: * tstate_current is moved out of _PyRuntimeState.gilstate * autoTSSkey is moved out of _PyRuntimeState.gilstate * autoTSSkey is initialized earlier * autoTSSkey is re-initialized (after fork) earlier https://github.com/python/cpython/issues/59956
* GH-100892: consolidate `HEAD_LOCK/HEAD_UNLOCK` macros (#100953)Kumar Aditya2023-01-151-4/+0
|
* GH-100126: Skip incomplete frames in more places (GH-100613)Brandt Bucher2023-01-091-7/+2
|
* GH-100000: Cleanup and polish various watchers code (GH-99998)Itamar Ostricher2022-12-141-0/+4
| | | | | | * Initialize `type_watchers` array to `NULL`s * Optimize code watchers notification * Optimize func watchers notification
* gh-99741: Implement Multi-Phase Init for the _xxsubinterpreters Module ↵Eric Snow2022-12-051-56/+128
| | | | | | | (gh-99742) _xxsubinterpreters is an internal module used for testing. https://github.com/python/cpython/issues/99741
* gh-99741: Clean Up the _xxsubinterpreters Module (gh-99940)Eric Snow2022-12-021-9/+11
| | | | | This cleanup up resolves a few subtle bugs and makes the implementation for multi-phase init much cleaner. https://github.com/python/cpython/issues/99741
* gh-99741: Fix the Cross-Interpreter Data API (gh-99939)Eric Snow2022-12-021-23/+84
| | | | | There were some minor issues that showed up while I was working on porting _xxsubinterpreters to multi-phase init. This fixes them. https://github.com/python/cpython/issues/99741
* GH-91054: Add code object watchers API (GH-99859)Itamar Ostricher2022-12-021-0/+5
| | | | | | * Add API to allow extensions to set callback function on creation and destruction of PyCodeObject Co-authored-by: Ye11ow-Flash <janshah@cs.stonybrook.edu>
* gh-91053: Add an optional callback that is invoked whenever a function is ↵mpage2022-11-221-0/+5
| | | | modified (#98175)
* gh-99377: Revert audit events for thread state creation and free, because ↵Steve Dower2022-11-171-18/+1
| | | | the GIL is not properly held at these times (GH-99543)
* gh-81057: Move the global Dict-Related Versions to _PyRuntimeState (gh-99497)Eric Snow2022-11-161-1/+1
| | | | | We also move the global func version. https://github.com/python/cpython/issues/81057
* gh-99377: Add audit events for thread creation and clear (GH-99378)Steve Dower2022-11-161-6/+25
|
* gh-81057: Move More Globals in Core Code to _PyRuntimeState (gh-99516)Eric Snow2022-11-161-5/+5
| | | https://github.com/python/cpython/issues/81057
* GH-99205: remove `_static` field from `PyThreadState` and ↵Kumar Aditya2022-11-151-10/+6
| | | | `PyInterpreterState` (GH-99385)
* gh-81057: Move the Allocators to _PyRuntimeState (gh-99217)Eric Snow2022-11-111-2/+5
| | | | | The global allocators were stored in 3 static global variables: _PyMem_Raw, _PyMem, and _PyObject. State for the "small block" allocator was stored in another 13. That makes a total of 16 global variables. We are moving all 16 to the _PyRuntimeState struct as part of the work for gh-81057. (If PEP 684 is accepted then we will follow up by moving them all to PyInterpreterState.) https://github.com/python/cpython/issues/81057
* GH-96421: Insert shim frame on entry to interpreter (GH-96319)Mark Shannon2022-11-101-0/+1
| | | | | | * Adds EXIT_INTERPRETER instruction to exit PyEval_EvalDefault() * Simplifies RETURN_VALUE, YIELD_VALUE and RETURN_GENERATOR instructions as they no longer need to check for entry frames.
* gh-99300: Use Py_NewRef() in Python/ directory (#99302)Victor Stinner2022-11-101-14/+7
| | | | Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Python/ directory.
* GH-99205: Mark new interpreters and threads as non-static (GH-99268)Brandt Bucher2022-11-091-0/+8
|
* gh-98608: Change _Py_NewInterpreter() to _Py_NewInterpreterFromConfig() ↵Eric Snow2022-10-261-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | (gh-98609) (see https://github.com/python/cpython/issues/98608) This change does the following: 1. change the argument to a new `_PyInterpreterConfig` struct 2. rename the function to `_Py_NewInterpreterFromConfig()`, inspired by `Py_InitializeFromConfig()` (takes a `_PyInterpreterConfig` instead of `isolated_subinterpreter`) 3. split up the boolean `isolated_subinterpreter` into the corresponding multiple granular settings * allow_fork * allow_subprocess * allow_threads 4. add `PyInterpreterState.feature_flags` to store those settings 5. add a function for checking if a feature is enabled on an opaque `PyInterpreterState *` 6. drop `PyConfig._isolated_interpreter` The existing default (see `Py_NewInterpeter()` and `Py_Initialize*()`) allows fork, subprocess, and threads and the optional "isolated" interpreter (see the `_xxsubinterpreters` module) disables all three. None of that changes here; the defaults are preserved. Note that the given `_PyInterpreterConfig` will not be used outside `_Py_NewInterpreterFromConfig()`, nor preserved. This contrasts with how `PyConfig` is currently preserved, used, and even modified outside `Py_InitializeFromConfig()`. I'd rather just avoid that mess from the start for `_PyInterpreterConfig`. We can preserve it later if we find an actual need. This change allows us to follow up with a number of improvements (e.g. stop disallowing subprocess and support disallowing exec instead). (Note that this PR adds "private" symbols. We'll probably make them public, and add docs, in a separate change.)
* GH-91052: Add C API for watching dictionaries (GH-31787)Carl Meyer2022-10-071-0/+4
|
* GH-91079: Decouple C stack overflow checks from Python recursion checks. ↵Mark Shannon2022-10-051-2/+3
| | | | (GH-96510)
* GH-96754: Check whether the interpreter frame is complete before creating ↵Mark Shannon2022-09-131-0/+3
| | | | frame object. (GH-96776)
* Fix possible NULL pointer dereference in _PyThread_CurrentFrames (GH-96584)Kumar Aditya2022-09-091-1/+6
|
* GH-96569: Add two NULL checks to avoid undefined behavior. (GH-96585)Mark Shannon2022-09-061-8/+5
|
* GH-96071: fix deadlock in PyGILState_Ensure (GH-96124)Kumar Aditya2022-08-191-11/+16
| | | Alternative of #96107
* GH-95909: Make `_PyArg_Parser` initialization thread safe (GH-95958)Kumar Aditya2022-08-161-6/+21
|
* GH-95818: Skip incomplete frames in `PyThreadState_GetFrame` (GH-95886)Mark Shannon2022-08-111-2/+6
|