From a4ed2ca2b898875353bccaa762206058f7eb31ac Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 28 Jul 2026 11:09:45 +0100 Subject: [PATCH] Cleanup --- rust/src/homeserver.rs | 4 +++- rust/src/http_client.rs | 7 +++++-- rust/src/runtime.rs | 14 +++++++++++--- synapse/server.py | 2 +- synapse/synapse_rust/__init__.pyi | 17 ----------------- synapse/synapse_rust/http_client.pyi | 2 +- synapse/synapse_rust/runtime.pyi | 16 ++++++++++++++++ 7 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 synapse/synapse_rust/runtime.pyi diff --git a/rust/src/homeserver.rs b/rust/src/homeserver.rs index 499668564b..dbff7f5b32 100644 --- a/rust/src/homeserver.rs +++ b/rust/src/homeserver.rs @@ -44,7 +44,9 @@ impl HomeServer { .0 .bind(py) .call_method0(intern!(py, "get_rust_runtime"))? - .extract()?) + .cast::()? + .get() + .clone()) } /// The Rust-side view of `hs.config`. diff --git a/rust/src/http_client.rs b/rust/src/http_client.rs index d500d7a261..802f93b2d0 100644 --- a/rust/src/http_client.rs +++ b/rust/src/http_client.rs @@ -50,7 +50,7 @@ impl HttpClient { #[new] #[pyo3(signature = (runtime, user_agent, http2_only = false))] pub fn py_new( - runtime: RustRuntime, + runtime: &RustRuntime, user_agent: &str, http2_only: bool, ) -> PyResult { @@ -64,7 +64,10 @@ impl HttpClient { let client = builder.build().context("building reqwest client")?; - Ok(HttpClient { client, runtime }) + Ok(HttpClient { + client, + runtime: runtime.clone(), + }) } pub fn get<'a>( diff --git a/rust/src/runtime.rs b/rust/src/runtime.rs index 943756b0ce..51b9e587bf 100644 --- a/rust/src/runtime.rs +++ b/rust/src/runtime.rs @@ -128,7 +128,7 @@ impl Drop for RustRuntimeInner { /// constructor argument — pyo3 extracts a `#[pyclass]` that is `Clone` by /// cloning, which here is just an `Arc` refcount bump — and hold their own /// clone. Derefs to [`RustRuntimeInner`]. -#[pyclass(frozen, from_py_object, module = "synapse.synapse_rust")] +#[pyclass(frozen, skip_from_py_object)] #[derive(Clone)] pub struct RustRuntime { inner: Arc, @@ -191,8 +191,16 @@ impl ShutdownHook { } /// Called when registering modules with python. -pub fn register_module(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; +pub fn register_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { + let child_module = PyModule::new(py, "runtime")?; + + child_module.add_class::()?; + + m.add_submodule(&child_module)?; + + py.import("sys")? + .getattr("modules")? + .set_item("synapse.synapse_rust.runtime", child_module)?; Ok(()) } diff --git a/synapse/server.py b/synapse/server.py index 346a239d9b..0a047728bf 100644 --- a/synapse/server.py +++ b/synapse/server.py @@ -174,10 +174,10 @@ from synapse.state import StateHandler, StateResolutionHandler from synapse.storage import Databases from synapse.storage.controllers import StorageControllers from synapse.streams.events import EventSources -from synapse.synapse_rust import RustRuntime from synapse.synapse_rust.handlers import RustHandlers from synapse.synapse_rust.msc4388_rendezvous import MSC4388RendezvousHandler from synapse.synapse_rust.rendezvous import RendezvousHandler +from synapse.synapse_rust.runtime import RustRuntime from synapse.types import DomainSpecificString, ISynapseReactor from synapse.util import SYNAPSE_VERSION from synapse.util.caches import CACHE_METRIC_REGISTRY diff --git a/synapse/synapse_rust/__init__.pyi b/synapse/synapse_rust/__init__.pyi index ac254c871d..cb3eb7df07 100644 --- a/synapse/synapse_rust/__init__.pyi +++ b/synapse/synapse_rust/__init__.pyi @@ -1,21 +1,4 @@ -from synapse.types import ISynapseReactor - def sum_as_string(a: int, b: int) -> str: ... def get_rust_file_digest() -> str: ... def reset_logging_config() -> None: ... def get_rustc_version() -> str: ... - -class RustRuntime: - """The per-homeserver state for the Rust side of Synapse. - - Holds the tokio thread pool (started lazily on first use, shut down by a - reactor shutdown trigger) and a handle to the reactor. Rust classes that - need either take this as a constructor argument; get it from - `hs.get_rust_runtime()`. - """ - - def __init__( - self, - reactor: ISynapseReactor, - worker_threads: int = 4, - ) -> None: ... diff --git a/synapse/synapse_rust/http_client.pyi b/synapse/synapse_rust/http_client.pyi index 8a193d98b1..e6468bc46b 100644 --- a/synapse/synapse_rust/http_client.pyi +++ b/synapse/synapse_rust/http_client.pyi @@ -14,7 +14,7 @@ from typing import Mapping from twisted.internet.defer import Deferred -from synapse.synapse_rust import RustRuntime +from synapse.synapse_rust.runtime import RustRuntime class HttpClient: """ diff --git a/synapse/synapse_rust/runtime.pyi b/synapse/synapse_rust/runtime.pyi new file mode 100644 index 0000000000..fb61ab81aa --- /dev/null +++ b/synapse/synapse_rust/runtime.pyi @@ -0,0 +1,16 @@ +from synapse.types import ISynapseReactor + +class RustRuntime: + """The per-homeserver state for the Rust side of Synapse. + + Holds the tokio thread pool (started lazily on first use, shut down by a + reactor shutdown trigger) and a handle to the reactor. Rust classes that + need either take this as a constructor argument; get it from + `hs.get_rust_runtime()`. + """ + + def __init__( + self, + reactor: ISynapseReactor, + worker_threads: int = 4, + ) -> None: ...