611 Commits
Author SHA1 Message Date
Donghee NaandGitHub 86e3b9fd53 [3.15] gh-159111: Update vendored mimalloc to v2.5.2 (gh-159112) (gh-160178) 2026-10-22 09:55:05 -07:00
faf16ef256 [3.15] gh-159041: Fix use-after-free in asyncio task tracking during thread state cleanup (GH-159073) (#159694)
gh-159041: Fix use-after-free in asyncio task tracking during thread state cleanup (GH-159073)
(cherry picked from commit 999855b07f)

Co-authored-by: Nathan Goldbaum <[email protected]>
2026-10-19 11:35:08 +05:30
Neil SchemenauerandGitHub ef498e0336 [3.15] Fix _PyThreadState_ResumeDetached() assertion with attach waiters (GH-159372)
Make _PyThreadState_ResumeDetached() waiter-aware and use it from
start_the_world(), matching main branch.  Remove tstate_resume().
2026-10-13 12:43:19 -07:00
Trent NelsonandGitHub ed4f3bccbd [3.15] gh-151518: Avoid STW starvation of attaching threads (GH-152826) (GH-158186)
Preserve active attach waiters across successive stop-the-world pauses
so a tight collection loop cannot keep them from making progress.

Keep the waiter-aware resume operation local to pystate.c because this
branch does not have the newer BRC suspend and resume helpers.

(cherry picked from commit f156510960)
2026-10-13 17:44:50 +02:00
ccbdd1fba6 [3.15] gh-155504: Set the interpreter before fallible thread state initialization (GH-155590) (GH-155884)
(cherry picked from commit 94a75f74a1)

Co-authored-by: Abdul Mohammad <[email protected]>
2026-10-10 13:27:53 +03:00
021f634ed8 [3.15] gh-142349: Simplify lazy import resolution (GH-158282) (#158569)
Backport the shared lazy import resolver to Python 3.15, regenerate
interpreter files, and update the ABI snapshot.

Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
2026-10-01 16:56:39 +00:00
545124fd33 [3.15] gh-158364: Don't report other interpreters' threads in sys._current_frames (GH-158369) (#158471)
Co-authored-by: Himesh Rupchandani <[email protected]>
2026-09-30 12:42:51 +03:00
Kumar AdityaandGitHub e538c26816 [3.15] gh-157838: Merge biased refcounts on behalf of detached threads (#157839) (#158011) 2026-09-29 10:13:34 +03:00
c168fbc84a [3.15] gh-155725: Remove PyGILState_Ensure usage from tracemalloc (GH-156409) (#156628)
Co-authored-by: Kumar Aditya <[email protected]>
2026-08-31 09:53:56 +03:00
dd4bd4c293 [3.15] gh-155363: Fix QSBR slot leak on thread state creation failure (gh-155365) (#155579)
Co-authored-by: Neil Schemenauer <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
2026-08-22 12:39:14 +03:00
44fca7ed29 [3.15] gh-154431: Revert "gh-154431: Fix data race in sys.audithook (GH-154462)" (GH-154769) (GH-154774)
This reverts commit 596cd5c5d7.
This commit caused a refleak.

(cherry picked from commit c7b9a13a75)

Co-authored-by: Petr Viktorin <[email protected]>
2026-07-27 18:02:16 +02:00
e9d9fd9c80 [3.15] gh-154431: Fix data race in sys.audithook (GH-154462) (#154477)
gh-154431: Fix data race in `sys.audithook` (GH-154462)
(cherry picked from commit 596cd5c5d7)

Co-authored-by: sobolevn <[email protected]>
2026-07-22 12:41:16 +00:00
0f3e91b1f8 [3.15] gh-151126: Fix a possible crash during the startup with no memory under Py_STACKREF_DEBUG (GH-152478) (#152677)
gh-151126: Fix a possible crash during the startup with no memory under `Py_STACKREF_DEBUG` (GH-152478)
(cherry picked from commit ecdef17730)

Co-authored-by: sobolevn <[email protected]>
2026-06-30 11:56:43 +00:00
Pablo Galindo SalgadoandGitHub 65fbec64f6 [3.15] gh-151613: Fix remote debugging frame cache ABA (#152448)
gh-151613: Fix remote debugging frame cache ABA (#151614)

The remote debugging frame cache previously used only the last_profiled_frame address as its cache anchor. If a frame returned and a later frame reused the same _PyInterpreterFrame address, the profiler could accept a stale cache entry and splice parent frames from a different call chain into the current stack.

This adds a last_profiled_frame_seq counter next to last_profiled_frame, increments it when the anchor advances, stores it in frame cache entries, and validates cache hits against both the frame address and the sequence. Cache miss walks now copy stack chunks before storing new cache entries so stored continuations come from a stable snapshot. The new regression test exercises alternating call chains and checks that cached stacks never contain frames from both branches.

(cherry picked from commit 8cda6ae2f1)
2026-06-27 17:42:31 +00:00
22c994cc92 [3.15] gh-149335: Avoid JIT trace buffer asserts with overhead above FITNESS_INITIAL (GH-149633) (#150245)
gh-149335: Avoid JIT trace buffer asserts with overhead above `FITNESS_INITIAL` (GH-149633)
(cherry picked from commit 441af3a934)

Co-authored-by: Hai Zhu <[email protected]>
2026-05-24 12:48:58 +05:30
8297d50a63 [3.15] gh-145235: Make dict watcher API thread-safe for free-threaded builds (gh-145233) (#149690)
In free-threaded builds, concurrent calls to PyDict_AddWatcher, PyDict_ClearWatcher, PyDict_Watch, and PyDict_Unwatch can race on the shared callback array and the per-dict watcher tags. This change adds a mutex to serialize watcher registration and removal, atomic operations for tag updates, and atomic acquire/release synchronization for callback dispatch in _PyDict_SendEvent.

(cherry picked from commit 8a4895985f)

Co-authored-by: Alper <[email protected]>
2026-05-11 14:23:39 -04:00
2b7c28a440 gh-149101: Implement PEP 788 (GH-149116)
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Sam Gross <[email protected]>
2026-05-06 17:39:30 -04:00
Hai ZhuandGitHub 618b726d68 gh-146073: Add fitness/exit quality mechanism for JIT trace frontend (GH-148089)
* Replaces ad-hoc logic for ending traces with a simple inequality: `fitness < exit_quality`
* Fitness starts high and is reduced for branches, backward edges, calls and trace length
* Exit quality reflect how good a spot that instruction is to end a trace. Closing a loop is very, specializable instructions are very low and the others in between.
2026-04-24 10:37:01 +01:00
Diego RussoandGitHub 9633c5239d GH-126910: Build/link the JIT shim in the Python interpreter (#148872) 2026-04-23 12:23:18 +01:00
Dino ViehlandandGitHub c0af5c024b gh-146031: Allow keeping specialization enabled when specifying eval frame function (#146032)
Allow keeping specialization enabled when specifying eval frame function
2026-04-16 09:44:26 -07:00
64afa947f4 gh-146302: make Py_IsInitialized() thread-safe and reflect true init completion (GH-146303)
## Summary

- Move the `runtime->initialized = 1` store from before `site.py` import to the end of `init_interp_main()`, so `Py_IsInitialized()` only returns true after initialization has fully completed
- Access `initialized` and `core_initialized` through new inline accessors using acquire/release atomics, to also protect from data race undefined behavior
- `PySys_AddAuditHook()` now uses the accessor, so with the flag move it correctly skips audit hook invocation during all init phases (matching the documented "after runtime initialization" behavior) ... We could argue that running these earlier would be good even if the intent was never explicitly expressed, but that'd be its own issue.

## Motivation

`Py_IsInitialized()` returned 1 while `Py_InitializeEx()` was still running — specifically, before `site.py` had been imported. See https://github.com/PyO3/pyo3/issues/5900 where a second thread could acquire the GIL and start executing Python with an incomplete `sys.path` because `site.py` hadn't finished.

The flag was also a plain `int` with no atomic operations, making concurrent reads a C-standard data race, though unlikely to manifest.

## Regression test:

The added test properly fails on `main` with `ERROR: Py_IsInitialized() was true during site import`.

---

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
2026-04-11 21:54:23 +00:00
Ken JinandGitHub 328da67b2c gh-146073: Revert "gh-146073: Add fitness/exit quality mechanism for JIT trace frontend (GH-147966)" (#148082)
This reverts commit 198b04b75f.
2026-04-04 11:56:40 +00:00
Hai ZhuandGitHub 198b04b75f gh-146073: Add fitness/exit quality mechanism for JIT trace frontend (GH-147966) 2026-04-03 23:54:30 +08:00
Ken JinandGitHub 1384f025f5 gh-126910: Verify that JIT stencils preserve frame pointer (GH-146524) 2026-03-28 03:38:54 +08:00
Diego RussoandGitHub b60b9261b5 GH-126910: avoid reading the FP for getting the SP (GH-146521) 2026-03-28 01:52:48 +08:00
Ken JinandGitHub 3d0824aef2 gh-127958: Trace from RESUME in the JIT (GH-145905) 2026-03-17 00:18:59 +08:00
Hai ZhuandGitHub 81ef1b7317 gh-144888: Replace bloom filter linked lists with continuous arrays to optimize executor invalidating performance (GH-145873) 2026-03-16 15:58:18 +00:00
T. WoutersGitHubblurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
706fd4ec08 gh-142183: Cache one datachunk per tstate to prevent alloc/dealloc thrashing (#145789)
Cache one datachunk per tstate to prevent alloc/dealloc thrashing when repeatedly hitting the same call depth at exactly the wrong boundary.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2026-03-11 15:46:16 +01:00
Hai ZhuandGitHub 29840247ff gh-144068: fix JIT tracer memory leak when daemon thread exits (GH-144077) 2026-01-24 09:43:01 +00:00
Mark ShannonandGitHub d77aaa7311 GH-139109: Partial reworking of JIT data structures (GH-144105)
* Halve size of buffers by reusing combined trace + optimizer buffers for TOS caching
* Add simple buffer struct for more maintainable handling of buffers
* Decouple JIT structs from thread state struct
* Ensure terminator is added to trace, when optimizer gives up
2026-01-22 10:55:49 +00:00
Donghee NaandGitHub 27a7160b8b gh-141504: Move PYTHON_UOPS_OPTIMIZE to policy object (GH-144082) 2026-01-20 18:47:38 +00:00
Donghee NaandGitHub 794f758cd8 gh-141504: Refactor policy object into a single opt_config (gh-143644) 2026-01-15 09:53:00 +09:00
Hai ZhuandGitHub 94dbce1397 gh-138050: Use cold flag instead of warm flag in MAKE_WARM (GH-143827) 2026-01-14 10:27:33 +00:00
Ken JinandGitHub e0fb278064 gh-143421: Allocate all JIT state in one go (GH-143626) 2026-01-09 19:00:49 +00:00
Ken JinandGitHub b852236b26 gh-143421: Lazily allocate tracer code and opt buffers (GH-143597) 2026-01-09 16:56:19 +00:00
aeb3403563 gh-143421: Move JitOptContext from stack allocation to per-thread heap allocation (GH-143536)
* move JitOptContext to _PyThreadStateImpl
* make _PyUOpInstruction buffer a part of _PyThreadStateImpl

Co-authored-by: Kumar Aditya <[email protected]>
2026-01-08 19:38:21 +00:00
Sam GrossandGitHub f11f5ebfe6 gh-129824: Fix data race on runtime->gilstate.check_enabled (gh-143530) 2026-01-07 17:56:14 -05:00
Donghee NaandGitHub 12283f6373 gh-141504: Factor out tracing and optimization heuristics into a single object (gh-143381) 2026-01-03 15:22:14 +00:00
Diego RussoandGitHub 685272eb8a JIT: Rename trampoline.c to shim.c (#142974) 2025-12-19 14:39:41 +00:00
Mark ShannonandGitHub e4058d7cb1 GH-142513: Reimplement executor management (GH-142931)
* Invalidating an executor does not cause arbitrary code to run
* Executors are only freed at safe points
2025-12-18 16:43:44 +00:00
Kevin WangandGitHub 49b1fb43f6 gh-142048: Fix lost gc allocations count on thread cleanup (#142233) 2025-12-10 07:29:40 +00:00
Pablo Galindo SalgadoandGitHub d6d850df89 gh-138122: Don't sample partial frame chains (#141912) 2025-12-07 15:53:48 +00:00
Ken JinandGitHub 4fa80ce74c gh-139109: A new tracing JIT compiler frontend for CPython (GH-140310)
This PR changes the current JIT model from trace projection to trace recording. Benchmarking: better pyperformance (about 1.7% overall) geomean versus current https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20251108-3.15.0a1%2B-7e2bc1d-JIT/bm-20251108-vultr-x86_64-Fidget%252dSpinner-tracing_jit-3.15.0a1%2B-7e2bc1d-vs-base.svg, 100% faster Richards on the most improved benchmark versus the current JIT. Slowdown of about 10-15% on the worst benchmark versus the current JIT. **Note: the fastest version isn't the one merged, as it relies on fixing bugs in the specializing interpreter, which is left to another PR**. The speedup in the merged version is about 1.1%. https://raw.githubusercontent.com/facebookexperimental/free-threading-benchmarking/refs/heads/main/results/bm-20251112-3.15.0a1%2B-f8a764a-JIT/bm-20251112-vultr-x86_64-Fidget%252dSpinner-tracing_jit-3.15.0a1%2B-f8a764a-vs-base.svg

Stats: 50% more uops executed, 30% more traces entered the last time we ran them. It also suggests our trace lengths for a real trace recording JIT are too short, as a lot of trace too long aborts https://github.com/facebookexperimental/free-threading-benchmarking/blob/main/results/bm-20251023-3.15.0a1%2B-eb73378-CLANG%2CJIT/bm-20251023-vultr-x86_64-Fidget%252dSpinner-tracing_jit-3.15.0a1%2B-eb73378-pystats-vs-base.md .

This new JIT frontend is already able to record/execute significantly more instructions than the previous JIT frontend. In this PR, we are now able to record through custom dunders, simple object creation, generators, etc. None of these were done by the old JIT frontend. Some custom dunders uops were discovered to be broken as part of this work gh-140277

The optimizer stack space check is disabled, as it's no longer valid to deal with underflow.

Pros:
* Ignoring the generated tracer code as it's automatically created, this is only additional 1k lines of code. The maintenance burden is handled by the DSL and code generator.
* `optimizer.c` is now significantly simpler, as we don't have to do strange things to recover the bytecode from a trace.
* The new JIT frontend is able to handle a lot more control-flow than the old one.
* Tracing is very low overhead. We use the tail calling interpreter/computed goto interpreter to switch between tracing mode and non-tracing mode. I call this mechanism dual dispatch, as we have two dispatch tables dispatching to each other. Specialization is still enabled while tracing.
* Better handling of polymorphism. We leverage the specializing interpreter for this.

Cons:
* (For now) requires tail calling interpreter or computed gotos. This means no Windows JIT for now :(. Not to fret, tail calling is coming soon to Windows though https://github.com/python/cpython/pull/139962

Design:
* After each instruction, the `record_previous_inst` function/label is executed. This does as the name suggests.
* The tracing interpreter lowers bytecode to uops directly so that it can obtain "fresh" values at the point of lowering.
* The tracing version behaves nearly identical to the normal interpreter, in fact it even has specialization! This allows it to run without much of a slowdown when tracing. The actual cost of tracing is only a function call and writes to memory.
* The tracing interpreter uses the specializing interpreter's deopt to naturally form the side exit chains. This allows it to side exit chain effectively, without repeating much code. We force a re-specializing when tracing a deopt.
* The tracing interpreter can even handle goto errors/exceptions, but I chose to disable them for now as it's not tested.
* Because we do not share interpreter dispatch, there is should be no significant slowdown to the original specializing interpreter on tailcall and computed got with JIT disabled. With JIT enabled, there might be a slowdown in the form of the JIT trying to trace.
* Things that could have dynamic instruction pointer effects are guarded on. The guard deopts to a new instruction --- `_DYNAMIC_EXIT`.
2025-11-13 18:08:32 +00:00
b99db92dde gh-139653: Add PyUnstable_ThreadState_SetStackProtection() (#139668)
Add PyUnstable_ThreadState_SetStackProtection() and
PyUnstable_ThreadState_ResetStackProtection() functions
to set the stack base address and stack size of a Python
thread state.

Co-authored-by: Petr Viktorin <[email protected]>
2025-11-13 17:30:50 +01:00
Neil SchemenauerandGitHub c98c5b3449 gh-131253: free-threaded build support for pystats (gh-137189)
Allow the --enable-pystats build option to be used with free-threading.  The
stats are now stored on a per-interpreter basis, rather than process global.
For free-threaded builds, the stats structure is allocated per-thread and
then periodically merged into the per-interpreter stats structure (on thread
exit or when the reporting function is called). Most of the pystats related
code has be moved into the file Python/pystats.c.
2025-11-03 11:36:37 -08:00
Peter BiermaandGitHub 2cefa70eb9 gh-140544: Always assume that thread locals are available (GH-140690)
Python has required thread local support since 3.12 (see GH-103324). By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.
2025-10-28 09:07:19 -04:00
almandGitHub 1753ccb432 gh-138050: [WIP] JIT - Streamline MAKE_WARM - move coldness check to executor creation (GH-138240) 2025-10-27 16:37:37 +00:00
Kumar AdityaandGitHub ef4665f918 gh-140544: store pointer to interpreter state as a thread local for fast access (#140573) 2025-10-25 19:56:07 +05:30
Kumar AdityaandGitHub ebf9938496 gh-140544: cleanup HAVE_THREAD_LOCAL checks in pystate.c (#140547) 2025-10-24 14:23:06 +00:00
a615fb49c9 gh-140301: Fix memory leak in subinterpreter PyConfig cleanup (#140303)
Co-authored-by: Kumar Aditya <[email protected]>
2025-10-20 09:29:23 +00:00