Bug report
fi_freelist isn't thread-safe (move to pycore_freelist.h and follow that pattern)
enter_task, leave_task, and swap_current_task aren't thread-safe due to shared state->current_tasks and borrowed references.
register_task and unregister_task aren't thread-safe due to shared state->asyncio_tasks linked list
_asyncio_all_tasks_impl isn't thread-safe due the the asyncio_tasks linked list.
For 2, 3, and 4, we can consider using critical sections to protect the accesses to state->current_tasks and state->asyncio_tasks.
Longer term, moving data to per-loop will probably help with multi-threaded scaling.
Linked PRs
Bug report
fi_freelistisn't thread-safe (move topycore_freelist.hand follow that pattern)enter_task,leave_task, andswap_current_taskaren't thread-safe due to sharedstate->current_tasksand borrowed references.register_taskandunregister_taskaren't thread-safe due to sharedstate->asyncio_taskslinked list_asyncio_all_tasks_implisn't thread-safe due the theasyncio_taskslinked list.For 2, 3, and 4, we can consider using critical sections to protect the accesses to
state->current_tasksandstate->asyncio_tasks.Longer term, moving data to per-loop will probably help with multi-threaded scaling.
Linked PRs
swap_current_tasksafe in free-threaded build #122317swap_current_tasksafe in free-threaded build (GH-122317) #122612