Skip to content

Commit db3aff4

Browse files
: python: actor: shutdown_context() terminates this_host() (#2178)
Summary: now `shutdown_context()` terminates the OS process running `this_host()`. Differential Revision: D89498869
1 parent 997a64e commit db3aff4

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

hyperactor/src/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static RUNTIME: OnceLock<tokio::runtime::Handle> = OnceLock::new();
1919
///
2020
/// Panics if the runtime has not been initialized *and* the caller is not in an
2121
/// async context.
22-
pub(crate) fn get_runtime() -> tokio::runtime::Handle {
22+
pub fn get_runtime() -> tokio::runtime::Handle {
2323
match RUNTIME.get() {
2424
Some(handle) => handle.clone(),
2525
None => tokio::runtime::Handle::current(),

hyperactor/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ pub use hyperactor_telemetry::declare_static_timer;
142142
pub use hyperactor_telemetry::key_value;
143143
pub use hyperactor_telemetry::kv_pairs;
144144
#[doc(inline)]
145+
pub use init::get_runtime;
146+
#[doc(inline)]
145147
pub use init::initialize;
146148
#[doc(inline)]
147149
pub use init::initialize_with_current_runtime;

monarch_hyperactor/src/v1/host_mesh.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,14 @@ fn py_host_mesh_from_bytes(bytes: &Bound<'_, PyBytes>) -> PyResult<PyHostMesh> {
344344
}
345345

346346
#[pyfunction]
347-
fn shutdown_local_host_mesh() -> PyResult<PyPythonTask> {
347+
fn shutdown_local_host_mesh() -> PyResult<()> {
348348
let agent = HOST_MESH_AGENT_FOR_HOST
349349
.get()
350350
.ok_or_else(|| PyException::new_err("No local host mesh to shutdown"))?
351351
.clone();
352352

353-
PyPythonTask::new(async move {
353+
// Block on the async shutdown operation
354+
hyperactor::get_runtime().block_on(async move {
354355
// Create a temporary instance to send the shutdown message
355356
let temp_proc = hyperactor::Proc::local();
356357
let (instance, _) = temp_proc
@@ -365,8 +366,11 @@ fn shutdown_local_host_mesh() -> PyResult<PyPythonTask> {
365366
.await
366367
.map_err(|e| PyException::new_err(e.to_string()))?;
367368

368-
Ok(())
369-
})
369+
Ok::<(), pyo3::PyErr>(())
370+
})?;
371+
372+
// Exit the process
373+
std::process::exit(0)
370374
}
371375

372376
pub fn register_python_bindings(hyperactor_mod: &Bound<'_, PyModule>) -> PyResult<()> {

python/monarch/_rust_bindings/monarch_hyperactor/v1/host_mesh.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,19 @@ def bootstrap_host(
114114
"""
115115
...
116116

117-
def shutdown_local_host_mesh() -> PythonTask[None]:
117+
def shutdown_local_host_mesh() -> None:
118118
"""
119-
Shutdown the local host mesh created by bootstrap_host().
119+
Shutdown the local host mesh created by bootstrap_host() and exit the process.
120+
121+
This function blocks until shutdown completes. On successful shutdown, it
122+
calls exit(0) and never returns. On failure, it raises an exception.
120123
121124
Sends ShutdownHost message to the local host mesh agent with:
122125
- timeout: 10 seconds grace period before SIGTERM escalation
123126
- max_in_flight: 16 concurrent child terminations
124127
125128
Raises:
126129
RuntimeError: If no local host mesh exists (bootstrap_host not called)
130+
or if shutdown fails
127131
"""
128132
...

python/monarch/_src/actor/actor_mesh.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ def shutdown_context() -> "Future[None]":
388388
shutdown_local_host_mesh,
389389
)
390390

391-
return Future(coro=shutdown_local_host_mesh())
391+
# This function blocks and then exits the process
392+
shutdown_local_host_mesh()
393+
# Never reached
392394
except RuntimeError:
393395
# No local host mesh to shutdown
394396
pass

0 commit comments

Comments
 (0)