Skip to content

gh-161751: Fix race in mimalloc abandoned segment visit - #162003

Open
corona10 wants to merge 1 commit into
python:mainfrom
corona10:gh-161751-mimalloc-abandoned-visit-race
Open

corona10 wants to merge 1 commit into
python:mainfrom
corona10:gh-161751-mimalloc-abandoned-visit-race

Conversation

@corona10

@corona10 corona10 commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

@corona10

Copy link
Copy Markdown
Member Author

@DinoV @nascheme @daanx (For mimalloc upstream report)

Since mimalloc 2.5.2, abandoned segments of all sub-processes are tracked in one shared arena bitmap, so a reclaim in one sub-process can temporarily clear the bit of a segment that belongs to another sub-process. A full visit read that bitmap before taking abandoned_visit_lock, so it could see a temporarily cleared bit and skip that segment. As a downstream change, a full visit now takes the lock before reading the bitmap.

@corona10

corona10 commented Nov 14, 2026 •

Copy link
Copy Markdown
Member Author

I also reproduced the reported issue and confirmed that this fix resolves it.

@corona10

corona10 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

In my local benchmark, no overhead is observed in free-threaded multithreading and multiple subinterpreters. (only noisy)

import pyperf
from concurrent import interpreters

CODE = """
import collections, gc, threading, time

gc.disable()
window = collections.deque(maxlen=8)

def churn():
    t = threading.Thread(target=lambda: window.append([{} for _ in range(2000)]))
    t.start()
    t.join()

for _ in range(8):
    churn()
op = gc.collect if role == "gc" else churn
ready.put(None)
if background:
    while stop.empty():
        op()
else:
    while (loops := cmd.get()) is not None:
        t0 = time.perf_counter()
        for _ in range(loops):
            op()
        out.put(time.perf_counter() - t0)
"""


class Pool:
    def __init__(self, roles, bg_roles):
        self.ready, self.out, self.stop = (
            interpreters.create_queue() for _ in range(3))
        self.cmds, self.threads = [], []
        for role, background in ([(r, False) for r in roles]
                                 + [(r, True) for r in bg_roles]):
            cmd = interpreters.create_queue()
            interp = interpreters.create()
            interp.prepare_main(role=role, background=background, cmd=cmd,
                                ready=self.ready, out=self.out, stop=self.stop)
            thread = threading.Thread(target=interp.exec, args=(CODE,))
            thread.start()
            self.threads.append(thread)
            if not background:
                self.cmds.append(cmd)
        for _ in self.threads:
            self.ready.get()

    def run(self, loops):
        for cmd in self.cmds:
            cmd.put(loops)
        return sum(self.out.get() for _ in self.cmds) / len(self.cmds)

    def close(self):
        self.stop.put(None)
        for cmd in self.cmds:
            cmd.put(None)
        for thread in self.threads:
            thread.join()


pool = None


def bench(loops, roles, bg_roles):
    global pool
    if pool is None:
        pool = Pool(roles, bg_roles)
    return pool.run(loops)


if __name__ == "__main__":
    runner = pyperf.Runner()
    try:
        # name, measured interpreters, background interpreters
        runner.bench_time_func("gc_collect_1interp", bench, ["gc"], [])
        runner.bench_time_func("gc_collect_4interp", bench, ["gc"] * 4, [])
        runner.bench_time_func("thread_churn", bench, ["churn"] * 2, [])
        runner.bench_time_func("thread_churn_gc_loop", bench, ["churn"] * 2, ["gc"])
    finally:
        if pool is not None:
            pool.close()
import collections
import gc
import threading
import time

import pyperf

window = collections.deque(maxlen=8)
stop = threading.Event()
background = []


def churn():
    t = threading.Thread(target=lambda: window.append([{} for _ in range(2000)]))
    t.start()
    t.join()


def run_until_stopped(op):
    while not stop.is_set():
        op()


def bench(loops, op, bg_ops):
    if not window:
        gc.disable()
        for _ in range(8):
            churn()
        for bg_op in bg_ops:
            thread = threading.Thread(target=run_until_stopped, args=(bg_op,))
            thread.start()
            background.append(thread)
    t0 = time.perf_counter()
    for _ in range(loops):
        op()
    return time.perf_counter() - t0


if __name__ == "__main__":
    runner = pyperf.Runner()
    try:
        # name, measured operation, background threads
        runner.bench_time_func("gc_collect", bench, gc.collect, [])
        runner.bench_time_func("gc_collect_churn", bench, gc.collect, [churn] * 2)
        runner.bench_time_func("thread_churn", bench, churn, [])
        runner.bench_time_func("thread_churn_gc_loop", bench, churn, [gc.collect])
    finally:
        stop.set()
        for thread in background:
            thread.join()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant