10009 Commits
Author SHA1 Message Date
aee90465e5 [3.14] gh-148891: prevent crash in HAMT caused by GC during traverse (GH-148928) (#161592)
gh-148891: prevent crash in HAMT caused by GC during traverse (GH-148928)

`_PyHamt_Assoc` allocates HAMT nodes, which can trigger GC. A weakref callback or finaliser fired during GC could re-enter `contextvar_set`, replacing `ctx->ctx_vars` via `Py_SETREF` and freeing the old HAMT while `_PyHamt_Assoc` was still traversing it, causing a use-after-free.

Hold a strong reference to `ctx->ctx_vars` during the `_PyHamt_Assoc` call, so that a re-entrant free cannot affect the HAMT being traversed.
(cherry picked from commit 1798d4fb3c)

Co-authored-by: Thomas Kowalski <[email protected]>
2026-11-07 23:57:31 +00:00
Neko AsakuraandGitHub d0eff573d3 [3.14] gh-161288: Fix perf trampoline crash on UBSan builds (GH-161294) (#161350)
* [3.14] gh-161288: Fix perf trampoline crash on UBSan builds (GH-161294)
(cherry picked from commit a14cccbaa4)

Co-authored-by: Neko Asakura <[email protected]>

* gh-161288: retrigger CI
2026-11-05 00:51:29 +01:00
d16d33aa6e [3.14] gh-160962: Fix PyInitConfig_SetStr() with NULL value (GH-161258) (#161292)
gh-160962: Fix PyInitConfig_SetStr() with NULL value (GH-161258)

Previously, passing NULL to an optional string option crashed.

Document optional string options.

Add more tests on the PyInitConfig API.
(cherry picked from commit 6d69297b2b)

Co-authored-by: Victor Stinner <[email protected]>
2026-11-04 03:41:38 +01:00
b406b4fd47 [3.14] gh-150868: Fix sum() deallocating a float subclass as an exact float (GH-151063) (#161049)
gh-150868: Fix sum() deallocating a float subclass as an exact float (GH-151063)

* gh-151060: Fix sum() deallocating a float subclass as an exact float

The complex-total fast path in builtin_sum_impl() guarded with
PyFloat_Check() (which accepts subclasses) but freed the item with
_Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc), which assumes an
exact float. A float subclass instance reaching refcount 0 in that loop
aborts on an assertion in debug builds, and in release builds skips the
subclass tp_dealloc (type refcount leak, __del__ never runs, subclass
object pushed onto the float freelist).

Use PyFloat_CheckExact() instead, matching the float-total branch, so
subclass instances fall through to the generic PyNumber_Add() path.



* gh-150868: Also use exact-type checks for ints in the fast sum() loops

Per review: use PyLong_CheckExact with the specialized exact-int
deallocator in the float and complex fast-sum loops, so int subclasses
take the generic path.



---------
(cherry picked from commit 47c249adf3)

Co-authored-by: Pieter Eendebak <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
2026-11-01 20:57:24 +00:00
Neil SchemenauerandGitHub be5386f03f [3.14] gh-159136: Avoid stop-the-world when setting function name attributes (gh-159269) (gh-159726)
Setting __name__, __qualname__, __doc__ or __module__ on a function
stopped the world in the free-threaded build since gh-154826 and
gh-154851, making functools.wraps() orders of magnitude slower with
multiple threads.

Use the per-object critical section and _PyObject_XSetRefDelayed()
instead, as gen_set_name() already does.  Use acquire loads in the
readers and hold strong references across error reporting, tracing, and
eval-frame recording that may execute Python or detach the thread.  The
__code__, __defaults__, __kwdefaults__ and __closure__ setters still
stop the world to clear func_version.

Document synchronization requirements for direct field access and the
lifetime of borrowed function attributes.

(cherry picked from commit 010bebbf9f)
2026-10-30 11:03:31 -07:00
48b1e6026b [3.14] gh-159652: Fix import lock leak when the C stack is nearly exhausted (GH-159660) (GH-160780)
gh-159652: Fix import lock leak when the C stack is nearly exhausted (GH-159660)

When an import failed with a stack overflow RecursionError, releasing the import locks on the error path could itself raise RecursionError, because the cleanup runs in a new C frame. This left the global import lock or a module lock held forever, and later imports in other threads blocked indefinitely.

_imp.acquire_lock() now raises RecursionError when there is not enough stack left to release the lock. importlib calls it before taking any import lock, so this covers the per-module locks too. The module lock weakref callback skips cleanup in that case instead of
reporting an unraisable exception.
(cherry picked from commit 926b0bf311)

Co-authored-by: Cary Hawkins <[email protected]>
2026-10-27 22:12:45 +00:00
Stan UlbrychandGitHub f6ba0499c4 [3.14] Fix ignored error returns in sys initialization, _ctypes and _interpchannels (GH-160614) (#160712)
(cherry picked from commit 65057c56b9)
2026-10-26 19:49:48 +00:00
4500eed792 [3.14] gh-156466: fix cleanup on error in codegen_function_body (GH-156511) (#156579)
Co-authored-by: Irit Katriel <[email protected]>
2026-10-25 16:01:30 +02:00
Jelle ZijlstraandGitHub 72caed7b1f [3.14] gh-159739: Hold the codec registry lock while unregistering (GH-160168) (#160485)
Concurrent calls to codecs.unregister() can shrink the search path between its length check and indexed lookup in a free-threaded build. The lookup then returns NULL and an unconditional decref crashes the interpreter; hold the registry mutex across the scan and removal. Also reported in gh-157078. Backport to 3.15 and 3.14.
(cherry picked from commit 784b305df8)
2026-10-24 13:43:43 -07:00
1fa5e07274 [3.14] gh-128883: Store ELF perf trampoline templates in read-only data (GH-160366) (#160492)
CPython copies the trampoline templates into executable memory. Keep the
ELF source in .rodata so BOLT preserves its bytes and boundaries.
(cherry picked from commit 1ff83ad7ad)

Co-authored-by: Zanie Blue <[email protected]>
2026-10-23 20:56:21 +00:00
Carl MeyerandGitHub 9eab48adf8 [3.14] gh-160299: Preserve references from inlined comprehensions (GH-160413) (#160457)
Adapt the reference-flag propagation from GH-156819 to fix gh-160299 without backporting the comprehensive changes to comprehension scope representation and compilation.

(cherry picked from commit cc3a0c9d3a)
2026-10-23 18:31:06 +00:00
a53b1324fc [3.14] gh-156459: Fix cleanup on error in compiler_set_qualname (GH-156462) (#156575)
Co-authored-by: Irit Katriel <[email protected]>
2026-10-23 20:35:42 +03:00
a99e9298b9 [3.14] gh-159739: Preserve ContextVar cache updates from finalizers (GH-160197) (#160329)
Resetting a ContextVar can finalize the replaced value, whose finalizer may set the variable again. Updating the lookup cache after releasing the old context root overwrites that newer cached value, so get() can return a stale value or use a freed object. Update the cache before the old root can be released. Backport to 3.15 and 3.14.
(cherry picked from commit f4f8b933e3)

Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-22 15:22:13 -07:00
ddf7156bab [3.14] gh-159739: Skip context watchers cleared during notification (GH-160164) (#160289)
A context watcher callback can clear a later watcher while a context-switch notification is being dispatched. The dispatcher still uses its earlier snapshot of active watchers, so it calls a NULL callback and crashes; check that each callback is still registered before invoking it. Backport to 3.15 and 3.14.
(cherry picked from commit 0a5762621f)

Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-22 15:20:56 -07:00
Donghee NaandGitHub 1bcc0d165a [3.14] gh-159111: Update vendored mimalloc to v2.5.2 (gh-159112) (gh-160183) 2026-10-22 09:54:37 -07:00
Jelle ZijlstraandGitHub a03d287388 [3.14] gh-159739: Fix reference and error handling in warnings (GH-159758) (#160085)
Do not release the borrowed skip_file_prefixes argument, and stop if
allocation of the fallback filename fails.

(cherry picked from commit fab1405da8)
2026-10-21 19:24:15 +00:00
b93dabc62d [3.14] gh-159739: Clear free-threaded GC worklists after early errors (GH-159979) (#160017)
(cherry picked from commit 2315d94664)

Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-21 11:49:19 -07:00
d8d3df5336 [3.14] gh-149816: #96 Fix a race condition in invoke_gc_callback with free threading (GH-150029) (GH-156119) (#159310)
(cherry picked from commit 91d71dd670)
(cherry picked from commit e96bd136eb)

Co-authored-by: Miss Islington (bot) <[email protected]>
Co-authored-by: dzaramelcone <[email protected]>
2026-10-20 17:42:26 -07:00
442d6ad7cd [3.14] gh-159739: Bound encoding-name lookahead in surrogatepass (GH-159785) (#159912)
(cherry picked from commit 905aab60f7)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-20 18:38:32 +00:00
3ef1c8165c [3.14] gh-159739: Avoid leaking references during free-threaded GC enumeration (GH-159781) (#159908)
(cherry picked from commit 7652a6dacc)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-20 11:15:00 -07:00
62603cd971 [3.14] gh-159739: Handle interpreter configuration encoding errors (GH-159747) (#159844)
Propagate UTF-8 conversion errors before copying a string configuration
value.
(cherry picked from commit 626394e0a3)

Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-20 11:12:29 -07:00
bae32d612d [3.14] gh-159739: Check character formatting allocation failure (GH-159746) (#159840)
Propagate the error from PyUnicode_FromOrdinal() before using the
result.
(cherry picked from commit 5b0ab21364)

Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-20 08:45:25 -07:00
a3fdda2a53 [3.14] gh-159739: Release replaced PyInitConfig strings (GH-159769) (#159810)
gh-159739: Release replaced PyInitConfig strings (GH-159769)

* gh-159739: Release replaced PyInitConfig strings

Backport to 3.15 and 3.14.

---------
(cherry picked from commit 395d766d4f)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-20 00:32:57 +00:00
84990b5b80 [3.14] gh-158973: Reset the warnings lock after fork in the child (gh-158974) (gh-159615)
Co-authored-by: Nathan Goldbaum <[email protected]>
2026-10-20 00:06:42 +00:00
a436cd2191 [3.14] gh-158121: Preserve instruction events when line monitoring is… (#159741)
* [3.14] gh-158121: Preserve instruction events when line monitoring is disabled (GH-158456)
(cherry picked from commit f0f76de0c8)

Co-authored-by: limadog9 <[email protected]>
2026-10-19 14:19:52 -07:00
c542ecdec8 [3.14] gh-159041: Fix use-after-free in asyncio task tracking during thread state cleanup (#159073) (#159695)
(cherry picked from commit 999855b07f)

Co-authored-by: Nathan Goldbaum <[email protected]>
2026-10-19 06:39:35 +00:00
b643e2f531 [3.14] gh-157906: fix an incorrect architecture macro check in free-threaded GC (gh-157907) (gh-159426)
Fix typo in free-threaded GC prefetch check for 32-bit x86 MSVC.
(cherry picked from commit 1b015e66f0)

Co-authored-by: Pengyu Lee <[email protected]>
2026-10-16 20:10:09 -07:00
7c04882496 [3.14] gh-157415: Fix data race in pthread wrapper (#157430) (#159547)
Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid
a data race if another thread calls PyMem_SetAllocator() in parallel.

(cherry picked from commit d1878ccb44)

Co-authored-by: Nathan Goldbaum <[email protected]>
2026-10-16 15:55:12 +00:00
a11d4cd273 [3.14] gh-159320: Enable stack size detection on Android (GH-159321) (#159410)
gh-159320: Enable stack size detection on Android (GH-159321)

Extend the use of pthread_getattr_np in stack size detection from any glibc linux to any glibc *or* android linux. Update tests for android's new recursion limit.
(cherry picked from commit 8ff41c2ae7)

Co-authored-by: T. Wouters <[email protected]>
Co-authored-by: Malcolm Smith <[email protected]>
2026-10-14 12:44:00 +00:00
4489dc3e16 [3.14] gh-156909: Fix AST.__repr__ call getting the _fields attribute (GH-157490) (GH-157596) (GH-159232)
(cherry picked from commit ad0a18a17d)

Co-authored-by: Bartosz Sławecki <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
2026-10-13 12:12:05 +02:00
Gregory P. SmithandGitHub 758278c2eb [3.14] gh-159184: Do not compare set elements when marshalling a set (GH-159196) (#159206)
Set elements are written in the order of their dumps.  They were sorted
as (dump, element) pairs, so elements with the same dump were compared.
That failed for a set of several complex NaNs, which the compiler can
produce by constant folding, making the module impossible to marshal.
On this branch the failure was reported as MemoryError.

Sort (dump, index, element) tuples, in which the indexes are all
different.  Elements with the same dump stay in the order of the set,
as float NaNs did before.

(cherry picked from commit 1967a87ad3)
2026-10-11 23:27:34 +00:00
Gregory P. SmithandGitHub 09e1e663c9 [3.14] gh-159185: Fix marshalling of a set modified while its elements are marshalled (GH-159195) (#159200)
Marshalling a set element can run Python code (__buffer__, an audit
hook) which changes the set.  The number of elements was read before
the loop, so a set which grew was written past the end of the list and
one which shrank left NULL items in the list which was then sorted.
Raise RuntimeError("set changed size during iteration") instead.

(cherry picked from commit cd71799c47)
2026-10-11 14:52:10 -07:00
Victor StinnerandGitHub b23d657d0c [3.14] gh-158893: Make os.strerror() thread-safe (#159120) (#159145)
[3.15] gh-158893: Make os.strerror() thread-safe (#159120)

* gh-157695: Parse also _Py_HAVE_xxx variables in sysconfig (#158926)

Add parse_config_h() tests to test_sysconfig.

(cherry picked from commit 978a8befa5)

* gh-158893: Make os.strerror() thread-safe (#158927)

Make os.strerror() thread-safe: use the reentrant strerror_r()
function if available.

* The configure script now checks if strerror_r() is supported.
* Add a stress test to test_free_threading.test_os (new module).
* Add a comment on decode_current_locale() assertion which fails if
  the input string is mutated.
* Add an assertion to _Py_DecodeLocale() to detect if the input
  string was mutated during the function call.

* gh-158893: Add internal _Py_strerror() function (#158982)

Add a new internal _Py_strerror() function to Python/fileutils.c. It
uses strerror_r() if available, or use strerror() otherwise.

Replace all strerror(code) calls with _Py_strerror(code).

(cherry picked from commit 15dd7356f3)
(cherry picked from commit c774d1526f)
2026-10-10 20:54:38 +00:00
Maurycy Pawłowski-WierońskiandGitHub fb313a3948 [3.14] gh-158539: Fix macOS runtime section lookup for remote debugging (GH-158874) (#158906) 2026-10-06 17:13:01 +01:00
18ef0f0cb5 [3.14] gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading (gh-153720) (#158904)
gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading (gh-153720)

Under high thread concurrency in free-threaded builds, `_Py_Specialize_LoadGlobal` suffers from lock contention when acquiring the critical section mutexes for the `globals` and `builtins` dictionaries during bytecode specialization.

This PR skips LOAD_GLOBAL bytecode specialization if acquiring the two object mutexes would block.
(cherry picked from commit ac8ba0ca5a)

Co-authored-by: Peter Hawkins <[email protected]>
2026-10-06 18:47:51 +05:30
e0c28e2a00 [3.14] Add MSan to CI (GH-158625) (#158834)
* Run a job that the test suite with MSan to the CI (#158625)

* Run the test suite with MSan in CI

* Additional fixes

* Add `_Py_MSAN_UNPOISON_STRING`

* Apply Victor's suggestions

Co-authored-by: Victor Stinner <[email protected]>

* Apply Victor's suggestions

Co-authored-by: Victor Stinner <[email protected]>

---------

Co-authored-by: Victor Stinner <[email protected]>
(cherry picked from commit b93fb19a6e)

* Mark bytes returned by getrandom as initialized for MSan

* Handle invalid non-ASCII struct formats in the fuzz harness

---------

Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
2026-10-05 10:24:22 +00:00
4753622ec0 [3.14] gh-154194: Degrade frames instead of failing the sample (GH-154195) (#158833)
* gh-154194: Degrade unreadable frames on 3.14

(cherry picked from commit 7d25916b41)

Co-authored-by: Pablo Galindo Salgado <[email protected]>

* Preserve the stable ABI when creating fallback frame names

---------

Co-authored-by: Maurycy Pawłowski-Wieroński <[email protected]>
2026-10-05 10:04:44 +00:00
a9eeb9f2f5 [3.14] gh-156204: Guard recursion in PyErr_GivenExceptionMatches (GH-156205) (GH-158623)
(cherry picked from commit 20d5e67fc5)

Co-authored-by: Bhuvansh <[email protected]>
2026-10-02 20:50:00 +00:00
4443885c49 [3.14] gh-158446: Reject float format precision near INT_MAX (GH-158474) (#158477)
gh-158446: Reject float format precision near INT_MAX (GH-158474)

Formatting a float or complex with a precision within about 1000 of
INT_MAX could crash or produce incorrect output.
PyOS_double_to_string() now raises ValueError("precision too big") for
such precisions, as the format string parsers already do for
precisions above INT_MAX.

The limit applies regardless of presentation type or value, so a few
calls that previously succeeded (inf, nan, or 'g' with such a
precision) now raise as well.
(cherry picked from commit b7b4f3ecf2)

Co-authored-by: Gregory P. Smith <[email protected]>
2026-09-30 08:36:53 +00:00
b42e5bad03 [3.14] gh-158364: Don't report other interpreters' threads in sys._current_frames (GH-158369) (GH-158472)
(cherry picked from commit c3cb46870b)

Co-authored-by: Himesh Rupchandani <[email protected]>
2026-09-30 02:53:39 +00:00
Kumar AdityaandGitHub 6daaf3b2a6 [3.14] gh-157838: Merge biased refcounts on behalf of detached threads (#157839) (#158012) 2026-09-29 08:22:24 +05:30
373bc1abdf [3.14] gh-157549: Use sysconf() for the page size in remote_debug.h (GH-157800) (#157814)
gh-157549: Use sysconf() for the page size in remote_debug.h (GH-157800)

getpagesize() is not an ISO C or POSIX function. Darwin hides the prototype under -std=c11, so the _remote_debugging build fails with -Werror=implicit-function-declaration. That is the failure in the reported x86_64-apple-darwin → arm64-apple-darwin cross build.

Call sysconf(_SC_PAGESIZE) instead, which is already how resource.getpagesize() works. Fall back to 4096 if sysconf() fails.

--without-remote-debug is unchanged. That flag only disables the target-side attach protocol (Py_REMOTE_DEBUG); it does not exclude the _remote_debugging client module.
(cherry picked from commit 9193375382)

Co-authored-by: ノウラ | Flare <[email protected]>
2026-09-19 16:45:19 +00:00
Pablo Galindo SalgadoandGitHub d3369c2f67 [3.14] gh-156114: Fix crash in perf trampoline with unencodable code names (GH-156300) (#157775) 2026-09-18 21:35:08 +00:00
c348765d48 [3.14] gh-128509: Use bytes singletons in marshal (GH-157398) (#157432)
gh-128509: Use bytes singletons in marshal (GH-157398)

Replace soft deprecated PyBytes_FromStringAndSize(NULL, n) with
PyBytes_FromStringAndSize(str, n) so marshal can get bytes singleton.
(cherry picked from commit 658612ae77)

Co-authored-by: Victor Stinner <[email protected]>
2026-09-13 14:45:28 +00:00
0ba71429fa [3.14] gh-155504: Set the interpreter before fallible thread state initialization (GH-155590) (#155885)
gh-155504: Set the interpreter before fallible thread state initialization (GH-155590)
(cherry picked from commit 94a75f74a1)

Co-authored-by: Abdul Mohammad <[email protected]>
2026-09-08 13:48:13 +05:30
Miss Islington (bot)andGitHub dd36d014da [3.14] gh-156091: Fix crash compiling deeply nested inlined comprehensions (GH-156957) (#156993) 2026-09-05 16:27:05 +00:00
1546d44f6e [3.14] gh-156689: Fix out-of-bounds read in PyAst_CheckMode() for mode='func_type' (GH-156697) (#156704)
(cherry picked from commit d7f9c6400f)

Co-authored-by: Stan Ulbrych <[email protected]>
2026-08-31 14:19:57 +01:00
Irit KatrielandGitHub 3656c15ce8 [3.14]gh-156466: fix cleanup on error after Enter in codegen_enter_scope (#156467) (#156617)
gh-156466: fix cleanup on error after Enter in codegen_enter_scope (#156467)

(cherry picked from commit 41b3f0af26)
2026-08-30 14:14:31 +01:00
Irit KatrielandGitHub e9eb34f390 [3.14] gh-156466: fix cleanup on error in codegen_leave_annotations_scope (#156522)
[3.14] gh-156466: fix cleanup on error in codegen_leave_annotations_scope
2026-08-30 14:13:40 +01:00
d4ae98b0ec [3.14] gh-86427: Use the console code page for the stdio encoding on Windows (GH-155415) (GH-156603)
In the legacy Windows stdio mode PyConfig.stdio_encoding is now initialized
with the console output code page instead of the ANSI code page.

Only one encoding can be specified, so sys.stdin.encoding is still wrong if
the input code page differs from the output one.
(cherry picked from commit 0bb312a4ea)

Co-authored-by: Serhiy Storchaka <[email protected]>
2026-08-29 19:45:46 +00:00