Repository navigation
gh-161751: Fix race in mimalloc abandoned segment visit - #162003
Conversation
|
@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. |
|
I also reproduced the reported issue and confirmed that this fix resolves it. |
|
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() |
|
Thanks @corona10 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-162269 is a backport of this pull request to the 3.15 branch. |
|
GH-162270 is a backport of this pull request to the 3.14 branch. |
|
|
|
|
Uh oh!
There was an error while loading. Please reload this page.