Skip to content

[3.15] gh-151518: Avoid STW starvation of attaching threads (GH-152826) - #158186

Merged
encukou merged 1 commit into
python:3.15from
tpn:codex/gh-151518-backport-315
Oct 13, 2026
Merged

encukou merged 1 commit into
python:3.15from
tpn:codex/gh-151518-backport-315

Conversation

@tpn

@tpn tpn commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Backport of gh-152826, merged as f156510, for gh-151518.

A tight loop of stop-the-world pauses can repeatedly suspend a thread trying to reattach after sleeping, preventing it from returning and stopping the loop. This was found during intentional numba-cuda GC stress on many-core machines. The fix preserves active attach waiters across successive pauses so they have a chance to run.

This branch predates gh-157839, so the waiter-aware resume operation is a private helper in pystate.c. The attachment changes, regression test, and guard that avoids blocking waits on WASI are carried over from the merged fix.

Validation:

  • Free-threaded debug and TSan: all 31 focused threading, GC, and asyncio tests passed.
  • With only the test changes, the unfixed branch hits the 30-second watchdog with 4 and 64 CPUs. The backport passes three runs each with 1, 4, and 64 CPUs.
  • Ordinary GIL and WASI builds pass the internal C API helper tests, including test_stop_the_world. Local WASI checks used SDK 34 and Wasmtime 48; the WASI CI job also passed with the branch's configured toolchain.

CI note: the required Tests workflow passed. The Intel macOS JIT debug job exceeded its one-hour limit; its log is unavailable, so the cause is not yet known. A targeted retry is running on the unchanged commit. The other 18 JIT jobs passed.

@hugovk Given the 3.15 RC freeze, should this go into 3.15.0 or wait for the first maintenance release?

…thonGH-152826)

Preserve active attach waiters across successive stop-the-world pauses
so a tight collection loop cannot keep them from making progress.

Keep the waiter-aware resume operation local to pystate.c because this
branch does not have the newer BRC suspend and resume helpers.

(cherry picked from commit f156510)
@hugovk

hugovk commented Sep 29, 2026

Copy link
Copy Markdown
Member

This can go into 3.15.0 if not too risky, and PRs need core peer review during the RC, see https://discuss.python.org/t/the-3-15-branch-is-locked-for-rc1/108389.

@encukou

encukou commented Oct 13, 2026

Copy link
Copy Markdown
Member

@colesbury, do you want to double-check this?
The backport matches the original, but maybe you didn't trigger backports on purpose? The issue only affects artificial stress tests.

@colesbury colesbury 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.

LGTM

@encukou
encukou merged commit ed4f3bc into python:3.15 Oct 13, 2026
97 of 98 checks passed
@nascheme

Copy link
Copy Markdown
Member

This seems to break CI for the free-threaded builds. I see:

Python/pystate.c:2404: void _PyThreadState_ResumeDetached(PyThreadState *): Assertion `_Py_atomic_load_int_relaxed(&tstate->state) ==   _Py_THREAD_SUSPENDED' failed.

The 3.15 branch does have the BRC suspend/resume helpers (added by e538c26). The fix suggested by Claude is to basically move the code from tstate_resume() into _PyThreadState_ResumeDetached() and remove tstate_resume().

@vstinner

Copy link
Copy Markdown
Member

This seems to break CI for the free-threaded builds

Oh, I suppose that's the issue that I just reported: #159373.

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.

6 participants