The adaptive counter doesn't do anything currently in the free-threaded
build and TSan reports a data race due to concurrent modifications to
the counter.
(cherry picked from commit 2b163aa9e7)
Co-authored-by: Sam Gross <colesbury@gmail.com>
In the free-threaded build, we need to lock pending->mutex when clearing
the handling_thread in order not to race with a concurrent
make_pending_calls in the same interpreter.
(cherry picked from commit c557ae97d6)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-122029: Log call events in sys.setprofile when it's a method with c function (GH-122072)
Log call events in sys.setprofile when it is a method with a C function.
(cherry picked from commit e91ef13861)
Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
gh-121390: tracemalloc: Fix tracebacks memory leak (GH-121391)
The tracemalloc_tracebacks hash table has traceback keys and NULL
values, but its destructors do not reflect this -- key_destroy_func is
NULL while value_destroy_func is raw_free. Swap these to free the
traceback keys instead.
(cherry picked from commit db39bc42f9)
Co-authored-by: Josh Brobst <jbrobst@proton.me>
This is a small refactoring to the current design that allows us to
avoid manually iterating over threads.
This should also fix gh-118490.
(cherry picked from commit e059aa6b01)
Co-authored-by: mpage <mpage@meta.com>
gh-121621: Move asyncio_running_loop to private struct (GH-121939)
This avoids changing the ABI and keeps the field in the private struct.
(cherry picked from commit 81fd625b5c)
Co-authored-by: Sam Gross <colesbury@gmail.com>
* The result has type Py_ssize_t, not intptr_t.
* Type cast between unsigned and signed integer types should be explicit.
* Downcasting should be explicit.
* Fix integer overflow check in sum().
(cherry picked from commit 1801545)
We should maintain the invariant that a zero `ob_tid` implies the
refcount fields are merged.
* Move the assignment in `_Py_MergeZeroLocalRefcount` to immediately
before the refcount merge.
* Update `_PyTrash_thread_destroy_chain` to set `ob_ref_shared` to
`_Py_REF_MERGED` when setting `ob_tid` to zero.
Also check this invariant with assertions in the GC in debug builds.
That uncovered a bug when running out of memory during GC.
(cherry picked from commit d23be3947c)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-121546: Disable contextvar caching on free-threading build (GH-121740)
(cherry picked from commit e904300882)
Co-authored-by: Ken Jin <kenjin@python.org>
The change in gh-118157 (b2cd54a) should have also updated clear_singlephase_extension() but didn't. We fix that here. Note that clear_singlephase_extension() (AKA _PyImport_ClearExtension()) is only used in tests.
(cherry picked from commit 15d48aea02, AKA gh-121503)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
The `_PySeqLock_EndRead` function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section. The missing fence can trigger bugs on macOS arm64.
Additionally, we need a release fence in `_PySeqLock_LockWrite` to
ensure that the sequence update is visible before any modifications to
the cache entry.
(cherry picked from commit 1d3cf79a50)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-120837: Update _Py_DumpExtensionModules to be async-signal-safe (gh-121051)
(cherry picked from commit 1a2e7a7475)
Co-authored-by: Donghee Na <donghee.na@python.org>
This change makes things a little less painful for some users. It also fixes a failing assert (gh-120765), by making sure all subinterpreters are destroyed before the main interpreter. As part of that, we make sure Py_Finalize() always runs with the main interpreter active.
(cherry picked from commit 4be1f37b20, AKA gh-121060)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
We also add _PyThreadState_NewBound() and drop _PyThreadState_SetWhence().
This change only affects internal API.
(cherry picked from commit a905721b9c, AKA gh-121010)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* Add an InternalDocs file describing how interning should work and how to use it.
* Add internal functions to *explicitly* request what kind of interning is done:
- `_PyUnicode_InternMortal`
- `_PyUnicode_InternImmortal`
- `_PyUnicode_InternStatic`
* Switch uses of `PyUnicode_InternInPlace` to those.
* Disallow using `_Py_SetImmortal` on strings directly.
You should use `_PyUnicode_InternImmortal` instead:
- Strings should be interned before immortalization, otherwise you're possibly
interning a immortalizing copy.
- `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to
`SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in
backports, as they are now part of public API and version-specific ABI.
* Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery.
* Make sure the statically allocated string singletons are unique. This means these sets are now disjoint:
- `_Py_ID`
- `_Py_STR` (including the empty string)
- one-character latin-1 singletons
Now, when you intern a singleton, that exact singleton will be interned.
* Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic).
* Intern `_Py_STR` singletons at startup.
* For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup.
* Beef up the tests. Cover internal details (marked with `@cpython_only`).
* Add lots of assertions
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This makes the following macros public as part of the non-limited C-API for
locking a single object or two objects at once.
* `Py_BEGIN_CRITICAL_SECTION(op)` / `Py_END_CRITICAL_SECTION()`
* `Py_BEGIN_CRITICAL_SECTION2(a, b)` / `Py_END_CRITICAL_SECTION2()`
The supporting functions and structs used by the macros are also exposed for
cases where C macros are not available.
(cherry picked from commit 8f17d69b7b)
gh-120726: Fix compiler warnings on is_core_module() (GH-120727)
Fix compiler warnings on is_core_module() and
check_interpreter_whence(): only define them when
assertions are built.
(cherry picked from commit a816cd67f4)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Fix a race in `PyMember_GetOne` and `PyMember_SetOne` for `Py_T_OBJECT_EX`.
These functions implement `__slots__` accesses for Python objects.
(cherry picked from commit 362cd2680b)
Co-authored-by: Daniele Parmeggiani <8658291+dpdani@users.noreply.github.com>
gh-115649: Copy the filename into main interpreter before intern in import.c (GH-120315)
(cherry picked from commit 28140d1f2d)
Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
map() requires at least one iterable arg.
(cherry picked from commit d4039d3f6f)
Signed-off-by: Adam Williamson <awilliam@redhat.com>
Co-authored-by: Adam Williamson <adam@blueradius.ca>
In gh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown. However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection. The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection. To avoid the problem for _datetime, I have applied a similar approach here.
Also, credit goes to @mgorny and @neonene for the new tests.
FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting. I'll circle back to the cleaner approach with a future change on the main branch.
(cherry picked from commit b2e71ff4f8, AKA gh-120182)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
gh-120400 :Support Linux perf profile to see Python calls on RISC-V architecture (GH-120089)
(cherry picked from commit 4b1e85bafc)
Co-authored-by: ixgbe00 <yangwang@iscas.ac.cn>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
gh-117657: Fix some simple races in instrumentation.c (GH-120118)
* stop the world when setting local events
(cherry picked from commit b1b61dc4ce)
Co-authored-by: Ken Jin <kenjin@python.org>
gh-93691: fix too broad source locations of for statement iterators (GH-120330)
(cherry picked from commit 97b69db167)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>