Skip to content

gh-160697: Process the QSBR queue from the eval breaker, not from _PyMem_FreeDelayed() - #160698

Merged
kumaraditya303 merged 5 commits into
python:mainfrom
Eclips4:ft-free-delayed-reentrancy
Nov 20, 2026
Merged

kumaraditya303 merged 5 commits into
python:mainfrom
Eclips4:ft-free-delayed-reentrancy

Conversation

@Eclips4

@Eclips4 Eclips4 commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

@python-cla-bot

python-cla-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment on lines +58 to +68
def test_type_cache_invalidation(self):
class C:
x = None

holders = self.queue_finalizers(lambda: C.x)
for h in holders:
h.__dict__ = {}
C.x
new = object()
C.x = new
self.assertIs(C.x, new)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type cache is unfortunately affected too.

@Eclips4 Eclips4 added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 26, 2026
@nascheme

nascheme commented Nov 4, 2026

Copy link
Copy Markdown
Member

I think this is the right fix. Running arbitrary Python code (via the Py_DECREF of _PyObject_XDecRefDelayed items) from inside _PyMem_FreeDelayed means any of its call sites -- dict/list/set resize, type cache, code extra, etc. -- can have a destructor run in the middle of a mutation. That's not okay, and it's the same reason we changed the GC to only run from the eval breaker rather than from object allocation when the threshold was exceeded.

Suggested change: should_process is set in should_advance_qsbr_for_free() (after 127 deferred items or > 1 MiB of deferred memory), but nothing sets an eval breaker bit when that happens. _Py_HandlePending() only sees the flag if some other event triggers it, so in practice the chunk-full point (254 items) is the only reliable trigger. That allows up to 254 potentially large buffers (e.g. big shared list arrays) to accumulate before processing. Set _PY_EVAL_PROCESS_DELAYED_BIT wherever should_process = true is set, or replace the should_process flag with the eval breaker bit entirely.

A note on a risk, not to fix here: a thread that stays in C code without reaching the eval breaker (a long C-only loop over shared objects, or an embedder thread that only calls the C API) will now grow its queue without bound until something else drains it. The backstops are the GC, which merges and processes all threads' queues, and _PyMem_AbandonDelayed() on thread exit. If this turns out to be a problem, a follow-up could split the thread-local queue so raw memory frees (which can't re-enter) are still processed synchronously in free_delayed(), and only object decrefs wait for the eval breaker.

@NekoAsakura NekoAsakura left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be freed. `_PyMem_ProcessDelayed` is still called from the free path as a
safety valve when a work-item chunk fills up.

The document needs updating too.

Comment thread Objects/obmalloc.c
Comment on lines 1481 to 1502
if (buf == NULL) {
// failed to allocate a buffer, free immediately
PyObject *to_dealloc = NULL;
_PyEval_StopTheWorld(tstate->base.interp);
if (work_item_should_decref(ptr)) {
PyObject *obj = (PyObject *)(ptr - 1);
if (!_Py_IsImmortal(obj)) {
Py_ssize_t refcount = _Py_ExplicitMergeRefcount(obj, -1);
if (refcount == 0) {
to_dealloc = obj;
}
}
}
else {
PyMem_Free((void *)ptr);
}
_PyEval_StartTheWorld(tstate->base.interp);
if (to_dealloc != NULL) {
_Py_Dealloc(to_dealloc);
}
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still runs __del__ synchronously when PyMem_Calloc fails. Should it go through the eval breaker too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is actually safe because this path only handles the item being freed, not the queue. For a raw buffer it is just PyMem_Free(). Every caller of _PyObject_XDecRefDelayed() (type_setattro, _PyObject_SetDict, _PyObject_SetManagedDict, the function and generator setters) puts it where a plain Py_XSETREF() or Py_XDECREF() used to be. They only delay it for lock-free readers in other threads, and the stop-the-world on this path covers those readers. So running the finalizer there is the same as the old synchronous decref.


/* Reserve a few bits for future use */
#define _PY_EVAL_EVENTS_BITS 8
#define _PY_EVAL_EVENTS_BITS 9

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/* We share the eval-breaker with flags, so the monitoring
* version goes in the top 24 bits */
#define MONITORING_VERSION_INCREMENT (1 << _PY_EVAL_EVENTS_BITS)

The set_events overflows sooner now, is that fine? Above comment needs updating anyway.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, yes, that's true. A tight set_events() toggle loop now hits OverflowError after 2^23 - 1 calls instead of 2^24 - 1.
Unfortunately, all 8 event bits were already in use, so the new flag needs a ninth. Since only the free-threaded build uses this flag, the alternative would be to define it (and the extra bit) only under free-threaded build. I'd rather not have the bit layout differ between builds, so I've just updated the comment to drop the reference to 24 bits.

@Eclips4
Eclips4 requested a review from AA-Turner as a code owner November 7, 2026 15:32
@Eclips4

Eclips4 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member Author

Thank you Neil and Neko for the review!

I've removed should_process and updated docs.

So, finally limits start working:

import resource, sys, threading

L = []
t = threading.Thread(target=L.append, args=(0,))
t.start()
t.join()
chunk = [None] * 150_000
start = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
for _ in range(2000):
    L.extend(chunk)
    L.clear()
peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print(f"peak growth {(peak - start) / 1024:.0f} MiB")
branch Peak RSS growth
main 300 MiB
after this PR 1 MiB

@johng johng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from someone who is trying to gain familiarity with the code base I had a question about the test readability

Comment on lines +31 to +43
def test_dict_resize(self):
d = {}
make_shared(d.get, 0)
holders = self.queue_finalizers(lambda: d.__setitem__(object(), 0))
for n, h in enumerate(holders):
h.__dict__ = {}
if n % 2:
h.__dict__ = {}
for k in list(d):
del d[k]
for i in range(6):
d[i] = i
self.assertEqual(len(d), len(list(d)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this test quite hard to reason with, the odd/even behaviour was a bit difficult to reason exactly why that was picked.

I'm still wondering why it was added when it was not part of the original issue, something like this can equally re-create the issue. Simply staging the decrefs for "Evil" and then iterating with the resizing trigger. I feel this is a bit easier to understand the delayed decref mechanism during the resize.

    def test_dict_resize(self):
        d = {}
        make_shared(d.get, 0)
        # Queue delayed decrefs whose finalizers mutate d.  Use fewer than
        # fit in one chunk of work items, so that none of them are released
        # before the loop below starts.
        holders = self.queue_finalizers(lambda: d.__setitem__(object(), 0),
                                        count=100)
        for h in holders:
            h.__dict__ = {}
        # Every round compacts d, and each compaction delay-frees the old
        # keys.  From here on that is the only source of delayed frees, so
        # if they were released whenever a chunk fills up, the finalizers
        # would run in the middle of dictresize().
        for _ in range(self.ROUNDS):
            for k in list(d):
                del d[k]
            for i in range(6):
                d[i] = i
            self.assertEqual(len(d), len(list(d)))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi and thanks for the review!
The odd/event part was there to shift where the chunk boundary falls. I suppose we can't know how many delayed frees are already in the thread's queue when the test starts (although maybe we can write some kind of helper for that) and the bug shows up only when the chunk fills on the free inside the resize.

I've applied your approach for the dict test (it catches the bug on main for every possible queue fill (0-253), because every delayed free in that loop comes from dictresize). But not for the list, because each round there has three delayed frees (clear and two resizes), when the boundary lands on clear, the finalizers run at a safe point and therefore test passes.

Comment on lines +31 to +43
def test_dict_resize(self):
d = {}
make_shared(d.get, 0)
holders = self.queue_finalizers(lambda: d.__setitem__(object(), 0))
for n, h in enumerate(holders):
h.__dict__ = {}
if n % 2:
h.__dict__ = {}
for k in list(d):
del d[k]
for i in range(6):
d[i] = i
self.assertEqual(len(d), len(list(d)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a separate comment, changing the assert to assertEqual(len(d), sum(1 for _ in d)) would avoid the test seg faulting, instead failing on AssertionError: 106 != 6

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied!

@Eclips4
Eclips4 requested review from NekoAsakura and johng November 9, 2026 15:19
@kumaraditya303
kumaraditya303 merged commit e0d0f2e into python:main Nov 20, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @Eclips4 for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @Eclips4 and @kumaraditya303, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker e0d0f2e153450cf79b84e2ec5ffbcbad8aea7922 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @Eclips4 and @kumaraditya303, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker e0d0f2e153450cf79b84e2ec5ffbcbad8aea7922 3.14

kumaraditya303 pushed a commit to kumaraditya303/cpython that referenced this pull request Nov 20, 2026
… not from `_PyMem_FreeDelayed()` (pythonGH-160698)

(cherry picked from commit e0d0f2e)

Co-authored-by: Kirill Podoprigora <kirill.bast@gmail.com>
kumaraditya303 pushed a commit to kumaraditya303/cpython that referenced this pull request Nov 20, 2026
… not from `_PyMem_FreeDelayed()` (pythonGH-160698)

(cherry picked from commit e0d0f2e)

Co-authored-by: Kirill Podoprigora <kirill.bast@gmail.com>
@bedevere-app

bedevere-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

GH-162330 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 20, 2026
@bedevere-app

bedevere-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

GH-162331 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 20, 2026
kumaraditya303 added a commit that referenced this pull request Nov 20, 2026
…rom `_PyMem_FreeDelayed()` (GH-160698) (#162331)

(cherry picked from commit e0d0f2e)

Co-authored-by: Kirill Podoprigora <kirill.bast@gmail.com>
kumaraditya303 added a commit that referenced this pull request Nov 21, 2026
…rom `_PyMem_FreeDelayed()` (GH-160698) (#162330)

(cherry picked from commit e0d0f2e)

Co-authored-by: Kirill Podoprigora <kirill.bast@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants