Expand description
Runtime-identity tagging: which Tokio runtime a thread belongs to.
Monarch runs Python on more than one Tokio runtime: the shared control-plane
runtime that drives PythonActor dispatch, and separate data-plane runtimes
where GIL work runs safely, off the control plane (the tensor streams; the
RDMA managers, during buffer registration). The
hazard is contention on the shared runtime, where a thread holding the GIL
while blocked stalls every actor loop, supervisor, and network task on it.
So data-plane work runs on its own runtime, and control-plane GIL use is
kept to brief, sanctioned sites. This module records a thread’s
RuntimeKind (stamped at thread start via on_thread_start, read via
current_runtime_kind) so GIL-entry sites can tell the two apart. An
unstamped thread is not owned by a Monarch runtime and reads as None.
§Invariants (RI-*)
- RI-1 (unstamped-is-none):
current_runtime_kindreturnsNoneon any thread no runtime builder has tagged. - RI-2 (tagging-is-thread-local-and-once):
tag_current_threadsets only the current OS thread’s marker, and only once. - RI-3 (data-plane-workers-tagged):
build_data_plane_runtimestamps every worker thread of the runtime it returnsDataPlane(label), via the builder’son_thread_start. - RI-4 (tagging-does-not-leak): spawning work onto a tagged runtime does not change the spawning thread’s observed kind.
- RI-5 (process-lifetime-runtime): the handle from
build_data_plane_runtimeis kept alive by theDATA_PLANE_RUNTIMESregistry and stays valid until the runtime is torn down byshutdown_data_plane_runtimesat process teardown; callers must not spawn onto it afterward. - RI-6 (block-on-host-tagged): a data-plane runtime hosted on a manually
spawned thread that drives its work via
rt.block_onmust tag that hosting thread explicitly;on_thread_startstamps only the runtime’s own worker threads, not theblock_oncaller. - RI-7 (teardown-registered): every runtime from
build_data_plane_runtimeis registered inDATA_PLANE_RUNTIMESand shut down byshutdown_data_plane_runtimes. The pyo3 layer calls that at Python teardown beforePy_Finalize; this crate stays Python-free.
Enums§
- Runtime
Kind - Which kind of Tokio runtime a thread belongs to.
Functions§
- build_
data_ plane_ runtime - Build a dedicated data-plane runtime on its own OS thread, tagged
DataPlane(label), and return a handle for spawning onto it. - current_
runtime_ kind - The
RuntimeKindof the current thread, if it is stamped. - shutdown_
data_ plane_ runtimes - Shut down every data-plane runtime built by
build_data_plane_runtime, waiting up totimeoutfor each. Idempotent: a second call finds an empty registry and does nothing. Pure Rust (no Python); the pyo3 layer must call this at Python teardown, beforePy_Finalize, so data-plane workers stop before the interpreter is finalized. - tag_
current_ thread - Stamp the current thread with
kind. Call from a runtime builder’son_thread_startso every worker of that runtime carries the marker.