Skip to content

_PyMem_mi_page_maybe_free may insert pages into wrong thread's list #145614

@colesbury

Description

@colesbury

Bug report

In the free threading build, _PyMem_mi_page_maybe_free is called on mimalloc pages when they're about to be freed. We delay freeing of some pages in case another thread may be concurrently accessing it. In the meantime, they're stored on the tstate->mimalloc.page_list linked list.

_PyMem_mi_page_maybe_free inserts the page into the current thread's list. However, during GC and other stop-the-world pauses, we may traverse the heaps of other threads. That traversal does some processing of free lists, which can end up calling _PyMem_mi_page_maybe_free. In that case, we should insert the page into the owning thread's list, not the current thread's list. Otherwise, we can end up with a data race later on.

For more background see:
https://github.com/python/cpython/blob/main/InternalDocs/qsbr.md

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions