thread_aio: stop the thread pool on close(), bump Free Threading classifier to Beta - #74
Merged
Merged
Conversation
Context.close() is now explicit and idempotent (submit() after it raises RuntimeError), wired into thread_aio_asyncio's _destroy_context to match python_aio and the loop-detach the other backends already do on close(). The pool-pointer swap and submit()'s dispatch loop share one per-object critical section, so a concurrent close() can't race a concurrent submit() into using a pool mid-teardown - a no-op under the GIL, a real lock under free-threaded builds. Verified against a real free-threaded 3.14 interpreter on Linux (all 4 backends green); bumped the Free Threading classifier to 2 - Beta.
mosquito
force-pushed
the
thread-aio-close-and-ft-beta
branch
from
August 4, 2026 14:38
623540a to
080b1c0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
thread_aio.Contexthad noclose()at all - the native thread pool only stopped ondealloc, soAsyncioContextBase.close()/async withonthread_aio_asyncio.AsyncioContextwas a silent no-op and the pool kept running until GC. Added an explicit, idempotentclose()(submit() after it raisesRuntimeError) and wired it intothread_aio_asyncio.AsyncioContext._destroy_context, matchingpython_aio'sclose()and the loop-detach behavior already present forlinux_aio/linux_uring.close()/dealloc()and the dispatch loop insubmit()now share one per-object critical section (CAIO_BEGIN/END_CRITICAL_SECTION) so a concurrentclose()can't race a concurrentsubmit()into using a pool that's being torn down - a no-op under the GIL, a real per-object lock under free-threaded builds.Programming Language :: Python :: Free Threadingclassifier from1 - Unstableto2 - Beta.Test plan
uv run pytest --cov=caio --cov-report=term-missing tests- 78 passed (macOS, no linux_aio/linux_uring)ruff check/mypyclean on changed filespython3.14tinterpreter on Linux (sys._is_gil_enabled() is False) - 741 passed, 0 failed, all 4 backends includingtest_free_threading.pyContext(pool_size=8)spins up 8 real OS threads (/proc/pid/task),close()tears them back down to 1;submit()afterclose()raisesRuntimeError;linux_uring's eventfd reader is removed from the event loop afterAsyncioContext.close()