Skip to content

gh-158406: Fix run_until_complete making the next run stop early - #158450

Open
tjkuson wants to merge 1 commit into
python:mainfrom
tjkuson:gh-158406
Open

tjkuson wants to merge 1 commit into
python:mainfrom
tjkuson:gh-158406

Conversation

@tjkuson

@tjkuson tjkuson commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Each run_until_complete call registers its own done callback that only stops the loop whilst that call is running. It no longer stops a run after the call returns.

I'll flag that AnyIO currently finds its main task by looking for asyncio.base_events._run_until_complete_cb in task callbacks. On this branch, it no longer finds it, so anyio.to_thread starts a new thread per task and anyio.to_process jobs can fail. I think matching on the callback's __module__ in AnyIO would fix it.

I also tried an alternative that records the current future on the loop instead. It keeps AnyIO working, but adds loop state, hangs in two edge cases, and doesn't fix a later run_forever().

A neater solution would inline _run_until_complete_cb into the closure and drop its gh-66619 early return, which is now redundant, but I kept it because removing it make AnyIO fail at import.

Closes gh-158406.

@tjkuson tjkuson changed the title gh-158406: Fix asyncio run_until_complete making the next run stop early gh-158406: Fix run_until_complete making the next run stop early Sep 29, 2026
@tjkuson
tjkuson marked this pull request as ready for review October 2, 2026 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asyncio: interrupting run_until_complete makes the next run stop early

1 participant