9122 Commits

Author SHA1 Message Date
Jelle Zijlstra
bc92e7878f
[3.13] gh-144169: Fix three crashes in AST objects with non-str kwargs (GH-144178) (#144260)
(cherry picked from commit 639c1ad4f1ef5c2409a62fa8ed16e6aa3a6f9ab8)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-01-27 20:37:35 -08:00
Miss Islington (bot)
e4e36a716a
[3.13] gh-142779: Initialize reserved field for proper padding (GH-142780) (#144160)
gh-142779: Initialize reserved field for proper padding (GH-142780)

The jitdump specification specifies a reserved field for padding.

Initialize it so no garbage data is embedded in the jitdump files.
(cherry picked from commit 77bf4ba732a8736910bd6b7ec5a58b8ee833c95f)

Co-authored-by: stratakis <cstratak@redhat.com>
2026-01-22 17:29:57 +00:00
Serhiy Storchaka
149ecbb9a9
[3.13] gh-142829: Fix use-after-free in Context.__eq__ via re-entrant ContextVar.set (GH-142905) (GH-143871)
(cherry picked from commit a4086d7f89e5d388e4ffcdb13e4fba0255234286)

Co-authored-by: A.Ibrahim <abdulrasheedibrahim47@gmail.com>
2026-01-15 15:00:44 +00:00
Miss Islington (bot)
8c6d87693b
[3.13] gh-106287: Do not write objects after an unmarshalling error (GH-132715) (GH-143833)
Writing out an object may involve a slot lookup, which is not safe to do with
an exception raised. In debug mode an assertion failure will occur if this
happens.
(cherry picked from commit ce8f5f98c6fc95a1704360d986a4d0281eeada79)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
2026-01-14 11:48:52 +00:00
Shamil
ab8224697a
[3.13] gh-140306: Clear uncaught exception info after applying error in subinterpreter (GH-140397) 2026-01-12 18:19:57 +02:00
Bénédikt Tran
733a5cf51c
[3.13] gh-143377: fix crashes in _interpreters.capture_exception (GH-143418) (#143653)
(cherry picked from commit ce6bae92da671e31013b00901591ce2b595b61ce)
2026-01-10 13:59:14 +00:00
Miss Islington (bot)
57c56b0fb1
[3.13] gh-143547: Fix PyErr_FormatUnraisable() fallback (GH-143557) (GH-143603) (#143612)
[3.14] gh-143547: Fix PyErr_FormatUnraisable() fallback (GH-143557) (GH-143603)

gh-143547: Fix PyErr_FormatUnraisable() fallback (GH-143557)

Hold a strong reference to 'hook' while calling the default
unraisable took to log hook failure.
(cherry picked from commit 1d0baf1ae48f98b61cc869a82e1b7206298f653f)


(cherry picked from commit 39a2bcf949095bd603f7b73f15b5b478dbb49ba9)

Co-authored-by: Victor Stinner <vstinner@python.org>
2026-01-09 15:33:25 +00:00
Pablo Galindo Salgado
de34f6d404
[3.13] gh-143228: Fix UAF in perf trampoline during finalization (GH-143233) (#143248) 2025-12-28 14:32:16 +00:00
Stan Ulbrych
e132893cc7
[3.13] gh-143135: Fix sys.flags.inspect when PYTHONINSPECT=0 (GH-143136) (GH-143168)
(cherry picked from commit 3509fa5a12855805f0c6d7f8a6a3b162744a8fd4)
2025-12-25 12:57:20 +02:00
Mikhail Efimov
4504ff89d5
[3.13] gh-140373: Correctly emit PY_UNWIND event when generator is closed (GH-140767) (#140821) 2025-12-20 16:21:11 +02:00
stratakis
4b8c9f5a7b
[3.13] gh-142776: Ensure fp file descriptor is closed on all code paths in import.c (GH-142777) (#142989)
gh-142776: Ensure fp file descriptor is closed on all code paths in import.c (GH-142777)

(cherry picked from commit 6a4f10325d58deb1906b39d68dc8e84f4c2bf5a4)
2025-12-20 12:48:42 +05:30
Savannah Ostrowski
8f7c4f1cef
[3.13] GH-100964: Fix reference cycle in exhausted generator frames (G… (#142904)
* [3.13] GH-100964: Fix reference cycle in exhausted generator frames (GH-141112)
(cherry picked from commit 92243dc62ce10715ab0d9074b23dea5a1bfa9dcc)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
2025-12-17 20:04:53 +00:00
Miss Islington (bot)
5e1a9c8392
[3.13] gh-142873: Do not check for PyContextVar_CheckExact twice in PyContextVar_Set (GH-142874) (#142894)
gh-142873: Do not check for `PyContextVar_CheckExact` twice in `PyContextVar_Set` (GH-142874)
(cherry picked from commit e61a447d0e0753621e8fc347e99dfc87884d68ab)

Co-authored-by: sobolevn <mail@sobolevn.me>
2025-12-17 17:04:39 +00:00
Bartosz Sławecki
3e45440382
[3.13] gh-142737: Handle lost io.open in _Py_FindSourceFile (GH-142747) (GH-142774)
(cherry picked from commit f277781bba684322dffffe45cd878f4652ccf7e4)
2025-12-15 23:27:15 +00:00
Stan Ulbrych
9b24418867
[3.13] gh-142571: Check for errors before calling each syscall in PyUnstable_CopyPerfMapFile() (GH-142460) (#142602)
Co-authored-by: AZero13 <gfunni234@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
2025-12-11 22:08:08 +00:00
Kumar Aditya
fda856f368
[3.13] gh-142048: Fix lost gc allocations count on thread cleanup (GH… (#142506)
[3.13] gh-142048: Fix lost gc allocations count on thread cleanup (GH-142233)
(cherry picked from commit 49b1fb43f65290dadeb83ed6f7c0c74995fda7a1)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
2025-12-11 15:07:38 +05:30
Serhiy Storchaka
93d5c481af
[3.13] gh-141370: Fix undefined behavior when using Py_ABS() (GH-141548) (#142304)
(cherry picked from commit 706fdda8b360120a25b272898df40c8913381723)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
2025-12-05 15:52:12 +00:00
Sam Gross
01393ffbe6
[3.13] gh-142048: Fix quadratically increasing GC delays (gh-142051) (#142167)
The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.
(cherry picked from commit eb892868b31322d7cf271bc25923e14b1f67ae38)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
2025-12-02 00:31:36 +00:00
Itamar Oren
385688d4b4
[3.13] gh-140011: Delete importdl assertion that prevents importing embedded modules from packages (GH-141605) (#141987)
gh-140011: Delete importdl assertion that prevents importing embedded modules from packages (GH-141605)

(cherry picked from commit 27f62eb711720c215f9798ae30728ee5a1b4d442)
2025-11-28 23:31:11 -08:00
Victor Stinner
80b752285d
[3.13] gh-116008: Detect freed thread state in faulthandler (#141988) (#142017)
gh-116008: Detect freed thread state in faulthandler (#141988)

Add _PyMem_IsULongFreed() function.

(cherry picked from commit d5d9e89dde9843a61b46872b1914c5b75ad05998)
2025-11-27 17:55:31 +01:00
Sam Gross
dbb539efc3
[3.13] gh-120158: Fix inconsistent monitoring state when setting events too frequently (gh-141845) (gh-141880)
If we overflowed the global version counter (i.e., after 2*24 calls to
`_PyMonitoring_SetEvents`), we bailed out after setting global monitoring
events but before instrumenting code objects, which led to assertion errors
later on.

Also add a `time.sleep()` to `test_free_threading.test_monitoring` to avoid
overflowing the global version counter.
(cherry picked from commit e457d60daafe66534283e0f79c81517634408e57)
2025-11-23 15:30:37 +00:00
Pablo Galindo Salgado
7e1a0dbc07
[3.13] gh-141579: Fix perf_jit backend in sys.activate_stack_trampoline() (GH-141580) (#141582) 2025-11-15 03:17:49 +00:00
Bénédikt Tran
718886f6c6
[3.13] gh-140530: fix a reference leak in an error path for raise exc from cause (GH-140908) (#141283)
gh-140530: fix a reference leak in an error path for `raise exc from cause` (#140908)

Fix a reference leak in `raise E from T` when `T` is an exception
subtype for which `T.__new__` does not return an exception instance.

(cherry picked from commit 0c77e7c23b5c270a3142105542c56c59b59c52a0)
2025-11-09 15:57:26 +01:00
Victor Stinner
43882c7c4e
[3.13] gh-140815: Fix faulthandler for invalid/freed frame (#140921) (#140985)
gh-140815: Fix faulthandler for invalid/freed frame (#140921)

faulthandler now detects if a frame or a code object is invalid or
freed.

Add helper functions:

* _PyCode_SafeAddr2Line()
* _PyFrame_SafeGetCode()
* _PyFrame_SafeGetLasti()

_PyMem_IsPtrFreed() now detects pointers in [-0xff, 0xff] range
as freed.

(cherry picked from commit a84181c31bfc45a1d6bcb1296bd298ad612c54d0)
2025-11-05 18:39:28 +01:00
Shamil
fd6df66624
[3.13] gh-140301: Fix memory leak in subinterpreter PyConfig cleanup (GH-140303) (#140684)
[3.13] gh-140301: Fix memory leak in subinterpreter `PyConfig` cleanup (GH-140303)
(cherry picked from commit a615fb49c948902a982c3256899507abcc9f9bc8)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-10-27 19:42:13 +00:00
Stan Ulbrych
1cc2c954d6
[3.13] gh-140471: Fix buffer overflow in AST node initialization with malformed _fields (GH-140506) (#140510)
(cherry picked from commit 95953b692db6cbd88139de12d81fb123293ec2d5)
2025-10-24 00:38:02 +01:00
Serhiy Storchaka
d7473f7a47
[3.13] gh-140306: Fix memory leaks in cross-interpreter data handling (GH-140307) (GH-140357)
(cherry picked from commit f9323213c98c9f1f7f3bf5af883b73047432fe50)

Co-authored-by: Shamil <ashm.tech@proton.me>
2025-10-20 10:13:15 +00:00
Serhiy Storchaka
92f7965cf6
[3.13] gh-139640: Fix swallowing syntax warnings in different modules (GH-139755) (GH-140119)
Revert GH-131993.

Fix swallowing some syntax warnings in different modules if they accidentally
have the same message and are emitted from the same line.

Fix duplicated warnings in the "finally" block.

(cherry picked from commit 279db6bede30be3a1b86803585eb4404d27800da)
2025-10-14 15:46:11 +00:00
Sam Gross
16d2e97cf3
[3.13] gh-137400: Fix thread-safety issues when profiling all threads (gh-137518) (gh-137733)
There were a few thread-safety issues when profiling or tracing all
threads via PyEval_SetProfileAllThreads or PyEval_SetTraceAllThreads:

* The loop over thread states could crash if a thread exits concurrently
  (in both the free threading and default build)
* The modification of `c_profilefunc` and `c_tracefunc` wasn't
  thread-safe on the free threading build.
(cherry picked from commit a10152f8fd0f4b291e53d646cffe22fbeec73e1e)
2025-10-09 11:42:47 -04:00
Kumar Aditya
dac827cb98
[3.13] gh-139748: fix leaks in AC error paths when using unicode FS-b… (#139792)
* [3.13] gh-139748: fix leaks in AC error paths when using unicode FS-based converters (GH-139765)
(cherry picked from commit b04a57deef66ce08233be57d1ab5873388df2cea)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-10-08 22:21:49 +05:30
Peter Bierma
63dd27da3b
[3.13] gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164) (GH-139169)
* gh-112729: Correctly fail when the process is out of memory during interpreter creation (GH-139164)

(cherry picked from commit d06113c7a7cac76a28847702685e601b79f71bf8)
2025-09-19 15:09:44 +00:00
yihong
afec6a5460
[3.13] gh-134163: Fix an infinite loop when the process runs out of memory in a try block (GH-138491)
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
2025-09-10 12:54:42 -04:00
Miss Islington (bot)
837df2774f
[3.13] fix comment reference from man 7 signal to man 7 signal-safety (GH-138554) (#138595)
fix comment reference from man 7 signal to man 7 signal-safety (GH-138554)
(cherry picked from commit 2f5ace780ba3880ce07c4aacbda08ebba36e3b4d)


docs: fix comment reference from man 7 signal to man 7 signal-safety

Co-authored-by: mqudah <mohghq@gmail.com>
2025-09-06 19:02:33 +00:00
Kliment Lamonov
ebccd1de88
[3.13] gh-132744: Check recursion limit in _PY_FRAME_GENERAL (GH-132746) (GH-138032) 2025-08-22 20:25:24 +08:00
Gregory P. Smith
9face218e7
[3.13] gh-87135: Hang non-main threads that attempt to acquire the GIL during finalization (GH-105805) (GH-137827)
* [3.13] gh-87135: Hang non-main threads that attempt to acquire the GIL during finalization (GH-105805)

Instead of surprise crashes and memory corruption, we now hang threads that attempt to re-enter the Python interpreter after Python runtime finalization has started. These are typically daemon threads (our long standing mis-feature) but could also be threads spawned by extension modules that then try to call into Python. This marks the `PyThread_exit_thread` public C API as deprecated as there is no plausible safe way to accomplish that on any supported platform in the face of things like C++ code with finalizers anywhere on a thread's stack. Doing this was the least bad option.

(cherry picked from commit 8cc5aa47ee464ddfd8da5461edecf4a5c72df2ff)

Co-authored-by: Jeremy Maitin-Shepard <jeremy@jeremyms.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>

* state "3.13.7 and earlier"
* backport: do not add the deprecated marker
* fix Py_IsFinalizing doc ref

---------

Co-authored-by: Jeremy Maitin-Shepard <jeremy@jeremyms.com>
2025-08-17 09:08:14 -07:00
adam j hartz
5131b8fe7e
[3.13] gh-137576: Fix for Basic REPL showing incorrect code in tracebacks with PYTHONSTARTUP (GH-137625) (#137778)
(cherry picked from commit 04f8ef663be7589def9f456a2024e1707e3408ea)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
2025-08-15 12:10:39 +03:00
Serhiy Storchaka
3a74d52584
[3.13] gh-137668: Document that ord() supports also bytes and bytearray (GH-137669) (GH-137704)
(cherry picked from commit 35759fe2faf1443455dfcb15ef7c435e34b492c7)
2025-08-13 09:37:15 +00:00
Sam Gross
e236568c0e
[3.13] gh-137400: Fix a crash when disabling profiling across all threads (gh-137471) (gh-137649)
The `PyEval_SetProfileAllThreads` function and other related functions
had a race condition on `tstate->c_profilefunc` that could lead to a
crash when disable profiling or tracing on all threads while another
thread is starting to profile or trace a a call.

There are still potential crashes when threads exit concurrently with
profiling or tracing be enabled/disabled across all threads.
(cherry picked from commit 362692852f13cdd1d33cc7ed35c0cbac7af1a785)
2025-08-11 16:07:21 +00:00
Miss Islington (bot)
599454e65d
[3.13] gh-137440: Update comment in Python/hamt.c on importing for testing (GH-137441) (GH-137520)
gh-137440: Update comment in Python/hamt.c on importing for testing (GH-137441)

Switch from `_testcapi` to `_testinternalcapi`.
(cherry picked from commit 7ab68cd50658f76abc9e0f12e6212736e2440720)

Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
2025-08-07 16:54:52 +00:00
Miss Islington (bot)
f2d6931656
[3.13] gh-58124: Avoid CP_UTF8 in UnicodeDecodeError (GH-137415) (#137461)
gh-58124: Avoid CP_UTF8 in UnicodeDecodeError (GH-137415)

Fix name of the Python encoding in Unicode errors of the code page
codec: use "cp65000" and "cp65001" instead of "CP_UTF7" and "CP_UTF8"
which are not valid Python code names.
(cherry picked from commit ce1b747ff68754635b7b12870dfc527184ee3b39)

Co-authored-by: Victor Stinner <vstinner@python.org>
2025-08-06 12:59:11 +00:00
Neil Schemenauer
5c16f699d2
[3.13] GH-133136: Revise QSBR to reduce excess memory held (gh-135473) (gh-136480)
The free threading build uses QSBR to delay the freeing of dictionary
keys and list arrays when the objects are accessed by multiple threads
in order to allow concurrent reads to proceed with holding the object
lock. The requests are processed in batches to reduce execution
overhead, but for large memory blocks this can lead to excess memory
usage.

Take into account the size of the memory block when deciding when to
process QSBR requests.

Also track the amount of memory being held by QSBR for mimalloc pages.
Advance the write sequence if this memory exceeds a limit.  Advancing
the sequence will allow it to be freed more quickly.

Process the held QSBR items from the "eval breaker", rather than from
`_PyMem_FreeDelayed()`.  This gives a higher chance that the global read
sequence has advanced enough so that items can be freed.

(cherry picked from commit 113de8545ffe74a4a1dddb9351fa1cbd3562b621)

Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
Co-authored-by: Sam Gross <colesbury@gmail.com>
2025-07-30 11:06:44 -07:00
Miss Islington (bot)
f2f30c4947
[3.13] gh-134411: assert PyLong_FromLong(x) != NULL when x is known to be small (GH-134415) (#136911)
gh-134411: assert `PyLong_FromLong(x) != NULL` when `x` is known to be small (GH-134415)

Since `PyLong_From Long(PY_MONITORING_DEBUGGER_ID)` falls to `small_int` case and can't return `NULL`. Added `assert`s for extra confidence.
https://github.com/python/cpython/issues/134411#issuecomment-2897653868
(cherry picked from commit cf19b6435d02dd7be11b84a44f4a8a9f1a935b15)

Co-authored-by: Sergey Muraviov <smurav@mail.ru>
2025-07-21 09:21:30 +00:00
Pablo Galindo Salgado
cba2974a54
[3.13] gh-136541: Fix several problems of perf trampolines in x86_64 and aarch64 (GH-136500) (#136545)
This commit fixes the following problems:

* The x86_64 trampolines are not preserving frame pointers
* The hardcoded offsets to the code segment from the FDE only worked properly for x64_64
* The CIE data was not following conventions of aarch64
* The eh_frame for aarch64 was not fully correct
(cherry picked from commit 236f733d8ffb3d587e1167fa0a0248c24512e7fd)
2025-07-11 14:02:19 +00:00
Miss Islington (bot)
2362cb6c6e
[3.13] gh-109700: fix interpreter finalization while handling memory error (GH-136342) (#136353)
gh-109700: fix interpreter finalization while handling memory error (GH-136342)
(cherry picked from commit 0c3e3da19570424649c33c0c2c29dc12541935e7)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2025-07-07 07:37:35 +00:00
Sam Gross
c64df2a3ec
[3.13] gh-135871: Fix needless spinning in _PyMutex_LockTimed with zero timeout (gh-135872) (gh-135947)
The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41caf135b8598a560854cd59e992a2ccfed)

Co-authored-by: Joseph Tibbertsma <josephtibbertsma@gmail.com>
2025-06-25 17:09:17 +00:00
Brian Schubert
3e4362812e
[3.13] gh-135855: Raise TypeError When Passing Non-dict Object to _interpreters.set___main___attrs (gh-135903)
(cherry picked from commit 4e6f0d116e, AKA gh-135856)
2025-06-24 16:23:26 -06:00
T. Wouters
6ae51582a1
[3.13] GH-135171: Roll back all fixes for GH-127682 as they are not suitable for 3.13 (#135390) 2025-06-11 16:57:35 +02:00
stratakis
65192572d2
[3.13] gh-128605: Add branch protections for x86_64 in asm_trampoline.S (#128606) (#135353)
Apply Intel Control-flow Technology for x86-64 on asm_trampoline.S.

Required for mitigation against return-oriented programming (ROP)
and Call or Jump Oriented Programming (COP/JOP) attacks.

Manual application is required for the assembly files.

See also: https://sourceware.org/annobin/annobin.html/Test-cf-protection.html
2025-06-11 12:09:20 +02:00
Miss Islington (bot)
857647fa67
[3.13] Heavily comment Python/perf_jit_trampoline.c to improve maintainability (GH-134527) (#135300) 2025-06-09 22:09:37 +01:00
Miss Islington (bot)
f9c18bab7f
[3.13] gh-128605: Revert "Add branch protections for x86_64 in asm_tr…ampoline.S (GH-128606) (GH-135077)" (GH-135175) (GH-135203)
[3.14] gh-128605: Revert "Add branch protections for x86_64 in asm_tr…ampoline.S (GH-128606) (GH-135077)" (GH-135175)

This reverts commit 899cca6dbf76bf3e06a99f60a5f996ad6ba0761f,
which broke buildbots.
(cherry picked from commit b477e21d5cb85bc764d6d9d18d122aa703d7b548)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
2025-06-06 16:09:09 +02:00