https://github.com/python/cpython/commit/320b59196ebd9cbc2f8be18326d65c7e8603dc93
commit: 320b59196ebd9cbc2f8be18326d65c7e8603dc93
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: corona10 <[email protected]>
date: 2026-11-19T13:18:32Z
summary:

[3.14] gh-161751: Fix race in mimalloc abandoned segment visit (gh-162003) 
(gh-162270)

gh-161751: Fix race in mimalloc abandoned segment visit (gh-162003)
(cherry picked from commit 484bc16723ef534e693bcd13e139ccd710a64da4)


gh-161751: Fix race in mimalloc abandoned segment visit in free-threaded GC

Co-authored-by: Donghee Na <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst
M Objects/mimalloc/arena-abandon.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst
new file mode 100644
index 000000000000000..0c39e484386dc58
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-23-17-18.gh-issue-161751.-silGa.rst
@@ -0,0 +1,2 @@
+Fix race in mimalloc abandoned segment visit in free-threaded GC. Patch by
+Donghee Na.
diff --git a/Objects/mimalloc/arena-abandon.c b/Objects/mimalloc/arena-abandon.c
index 460c80fc22782ff..fdc2a4a902a1daa 100644
--- a/Objects/mimalloc/arena-abandon.c
+++ b/Objects/mimalloc/arena-abandon.c
@@ -240,17 +240,20 @@ static mi_segment_t* 
mi_arena_segment_clear_abandoned_next_field(mi_arena_field_
     mi_arena_t* arena = mi_arena_from_index(arena_idx);
     if (arena != NULL) {
       bool has_lock = false;
+      // CPython: gh-161751: for a full visit, take the lock before reading 
the bitmap.
+      // A reclaim from another sub-process can clear our bit for a short time.
+      if (previous->visit_all && 
mi_option_is_enabled(mi_option_visit_abandoned)) {
+        mi_lock_acquire(&arena->abandoned_visit_lock);
+        has_lock = true;
+      }
       // visit the abandoned fields (starting at previous_idx)
       for (; field_idx < arena->field_count; field_idx++, bit_idx = 0) {
         size_t field = 
mi_atomic_load_relaxed(&arena->blocks_abandoned[field_idx]);
         if mi_unlikely(field != 0) { // skip zero fields quickly
-          // we only take the arena lock if there are actually abandoned 
segments present
+          // a reclaim tries to take the lock only when the field has 
abandoned segments
           if (!has_lock && mi_option_is_enabled(mi_option_visit_abandoned)) {
-            has_lock = (previous->visit_all ? 
(mi_lock_acquire(&arena->abandoned_visit_lock),true) : 
mi_lock_try_acquire(&arena->abandoned_visit_lock));
+            has_lock = mi_lock_try_acquire(&arena->abandoned_visit_lock);
             if (!has_lock) {
-              if (previous->visit_all) {
-                _mi_error_message(EFAULT, "internal error: failed to visit all 
abandoned segments due to failure to acquire the visitor lock");
-              }
               // skip to next arena
               break;
             }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to