Skip to content

Task-safe Python region state: per-thread cache, TaskState refactor, and fast path optimizations - #834

Merged
cjdoris merged 1 commit into
codex/implement-task-safe-cpython-thread-state-managementfrom
codex/github-mention-manage-cpython-thread-states-automatically-a-uyzm9i
Oct 4, 2026
Merged

cjdoris merged 1 commit into
codex/implement-task-safe-cpython-thread-state-managementfrom
codex/github-mention-manage-cpython-thread-states-automatically-a-uyzm9i

Conversation

@cjdoris

@cjdoris cjdoris commented Oct 4, 2026

Copy link
Copy Markdown
Member

Motivation

  • Make PythonCall region management task-safe by pinning task sessions to Julia threads and keeping a thread-local cached PyThreadState to avoid expensive lookups for nested regions.
  • Ensure that @pyregion and @pyregionbreak can efficiently handle Python-originated calls (borrowed thread state) and nested regions without unnecessary synchronization.

Description

  • Replace the old ThreadState/TaskState layout with a new ThreadState that holds a semaphore, a lazily-allocated tstate, and cached task/task_state, and extend TaskState with fields sem, attached, tid, and oldsticky to manage session lifecycle.
  • Add helper functions current_task_state, set_task!, and clear_task! and update start_session!, enter_region, exit_region, enter_break, and exit_break to pin tasks, manage stickiness, acquire/release semaphores, and maintain the thread-local cache invariant.
  • Optimize @pyregion macro to take a fast path when current_tstate() != C_NULL to avoid task- and thread-local lookups for Python-originated or nested regions, and update @pyregionbreak to use the cached current_task_state.
  • Adjust semantics so that nested regions inside a break behave correctly, and ensure the cache is cleared before releasing the semaphore and restored after reacquiring it.
  • Update tests: remove test/Region.jl and add expanded checks to test/C.jl under 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

  • Ran the updated unit tests in 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.
  • Existing Python info tests (python_executable_path, python_library_path, python_library_handle, python_version) were also executed and succeeded.

Codex Task

@cjdoris
cjdoris marked this pull request as ready for review October 4, 2026 19:50
@cjdoris
cjdoris merged commit 766ae4d into codex/implement-task-safe-cpython-thread-state-management Oct 4, 2026
1 of 2 checks passed
@cjdoris
cjdoris deleted the codex/github-mention-manage-cpython-thread-states-automatically-a-uyzm9i branch October 4, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant