Task-safe Python region state: per-thread cache, TaskState refactor, and fast path optimizations - #834
Merged
cjdoris merged 1 commit intoOct 4, 2026
Conversation
cjdoris
marked this pull request as ready for review
October 4, 2026 19:50
cjdoris
merged commit Oct 4, 2026
766ae4d
into
codex/implement-task-safe-cpython-thread-state-management
1 of 2 checks passed
cjdoris
deleted the
codex/github-mention-manage-cpython-thread-states-automatically-a-uyzm9i
branch
October 4, 2026 19:51
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.
Motivation
PyThreadStateto avoid expensive lookups for nested regions.@pyregionand@pyregionbreakcan efficiently handle Python-originated calls (borrowed thread state) and nested regions without unnecessary synchronization.Description
ThreadState/TaskStatelayout with a newThreadStatethat holds a semaphore, a lazily-allocatedtstate, and cachedtask/task_state, and extendTaskStatewith fieldssem,attached,tid, andoldstickyto manage session lifecycle.current_task_state,set_task!, andclear_task!and updatestart_session!,enter_region,exit_region,enter_break, andexit_breakto pin tasks, manage stickiness, acquire/release semaphores, and maintain the thread-local cache invariant.@pyregionmacro to take a fast path whencurrent_tstate() != C_NULLto avoid task- and thread-local lookups for Python-originated or nested regions, and update@pyregionbreakto use the cachedcurrent_task_state.test/Region.jland add expanded checks totest/C.jlunder the new test item"Python regions and task-safe thread states"that validate cache ownership, nested region behavior, breaks, exception propagation, multi-threaded spawning, and callback behavior.Testing
test/C.jl, including the new"Python regions and task-safe thread states"test item, which exercises@pyregion,@pyregionbreak, nested regions, and multi-threaded use; all tests passed.python_executable_path,python_library_path,python_library_handle,python_version) were also executed and succeeded.Codex Task