From 884d893ac8a3384f18501c0c89b54b328ff57a28 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Sun, 26 Jul 2026 15:48:43 +0300 Subject: [PATCH] gh-154734: Fix data race on Task.get_coro() with eager tasks in FT build --- .../Library/2026-07-26-15-38-17.gh-issue-154734.8FDOiq.rst | 2 ++ Modules/_asynciomodule.c | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-26-15-38-17.gh-issue-154734.8FDOiq.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-26-15-38-17.gh-issue-154734.8FDOiq.rst b/Misc/NEWS.d/next/Library/2026-07-26-15-38-17.gh-issue-154734.8FDOiq.rst new file mode 100644 index 00000000000000..ae0fc624088cd6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-26-15-38-17.gh-issue-154734.8FDOiq.rst @@ -0,0 +1,2 @@ +Fix a data race on :meth:`asyncio.Task.get_coro` with eager tasks in the +:term:`free-threaded build`. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 0cd41d0b4c4d0d..c09ff3f55ce18e 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3498,10 +3498,13 @@ task_eager_start(_PyThreadStateImpl *ts, asyncio_state *state, TaskObj *task) retval = -1; } + // gh-154734: Task.get_coro() reads task_coro under this lock + Py_BEGIN_CRITICAL_SECTION(task); if (task->task_state != STATE_PENDING) { // This seems to really help performance on pyperformance benchmarks clear_task_coro(task); } + Py_END_CRITICAL_SECTION(); return retval; }