mirror of
https://github.com/element-hq/synapse.git
synced 2026-08-14 22:21:24 +00:00
Move the "current logcontext" storage out of the Python `threading.local` and into a Rust slot (`rust/src/logcontext.rs`, exposed as `synapse.synapse_rust.logcontext`). `current_context()` now consults the tokio task-local first, then the OS thread-local, then the sentinel. This makes a single source of truth visible from both worlds: - Log records emitted from Rust while a tokio task is being polled (e.g. reqwest connecting, and any `log::` records from our async code or its dependencies) resolve `current_context()` to the context that was current when Python called into Rust, so `LoggingContextFilter` attributes them correctly — no per-record stamping machinery, pyo3-log unchanged. - `run_python_awaitable` now restores that captured context on the reactor thread before driving the awaitable, so Python invoked from Rust (e.g. `DatabasePool.runInteraction` from the native `/versions` handler) runs in the right logcontext and its DB-transaction accounting lands on the right request. Resolves the FIXME at deferred.rs:223. `create_deferred` captures the caller's context at the FFI boundary and scopes it onto the spawned task. Python keeps the accounting policy: `set_current_context` still does the `getrusage` start/stop bookkeeping and only delegates the raw slot write to `swap_current_context`. The sentinel is pushed into Rust at import (`register_sentinel`) so `context is SENTINEL_CONTEXT` identity and `bool(context)` semantics are preserved, and Rust never imports `synapse.logging.context` (no circular import). Tests: Rust unit tests for the slot/lookup precedence; a trial test that a Rust-origin log record is attributed to the caller's logcontext; and a trial test that the `/versions` per-user DB lookup is accounted against the caller's logcontext (the resolved FIXME). Existing tests/util/test_logcontext.py and tests/synapse_rust pass unmodified. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011LnJSXQ86AAWNtihR4C5PH
101 lines
2.6 KiB
Rust
101 lines
2.6 KiB
Rust
use std::convert::Infallible;
|
|
|
|
use lazy_static::lazy_static;
|
|
use pyo3::prelude::*;
|
|
use pyo3_log::ResetHandle;
|
|
|
|
pub mod acl;
|
|
pub mod canonical_json;
|
|
pub mod config;
|
|
pub mod deferred;
|
|
pub mod duration;
|
|
pub mod errors;
|
|
pub mod events;
|
|
pub mod handlers;
|
|
pub mod http;
|
|
pub mod http_client;
|
|
pub mod identifier;
|
|
pub mod json;
|
|
pub mod logging;
|
|
pub mod matrix_const;
|
|
pub mod msc4388_rendezvous;
|
|
pub mod push;
|
|
pub mod rendezvous;
|
|
pub mod room_versions;
|
|
pub mod segmenter;
|
|
pub mod storage;
|
|
pub mod tokio_runtime;
|
|
pub mod types;
|
|
|
|
lazy_static! {
|
|
static ref LOGGING_HANDLE: ResetHandle = pyo3_log::init();
|
|
}
|
|
|
|
/// Returns the hash of all the rust source files at the time it was compiled.
|
|
///
|
|
/// Used by python to detect if the rust library is outdated.
|
|
#[pyfunction]
|
|
fn get_rust_file_digest() -> &'static str {
|
|
env!("SYNAPSE_RUST_DIGEST")
|
|
}
|
|
|
|
/// Returns the `rustc` version used when this native module was built.
|
|
///
|
|
/// This value is embedded at build time, so it can be exported as a prometheus metrics.
|
|
#[pyfunction]
|
|
pub fn get_rustc_version() -> &'static str {
|
|
env!("SYNAPSE_RUSTC_VERSION")
|
|
}
|
|
|
|
/// Formats the sum of two numbers as string.
|
|
#[pyfunction]
|
|
#[pyo3(text_signature = "(a, b, /)")]
|
|
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
|
Ok((a + b).to_string())
|
|
}
|
|
|
|
/// Reset the cached logging configuration of pyo3-log to pick up any changes
|
|
/// in the Python logging configuration.
|
|
///
|
|
#[pyfunction]
|
|
fn reset_logging_config() {
|
|
LOGGING_HANDLE.reset();
|
|
}
|
|
|
|
/// The entry point for defining the Python module.
|
|
#[pymodule]
|
|
fn synapse_rust(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
|
m.add_function(wrap_pyfunction!(get_rust_file_digest, m)?)?;
|
|
m.add_function(wrap_pyfunction!(get_rustc_version, m)?)?;
|
|
m.add_function(wrap_pyfunction!(reset_logging_config, m)?)?;
|
|
|
|
acl::register_module(py, m)?;
|
|
logging::context::register_module(py, m)?;
|
|
deferred::register_module(py, m)?;
|
|
push::register_module(py, m)?;
|
|
events::register_module(py, m)?;
|
|
handlers::register_module(py, m)?;
|
|
http_client::register_module(py, m)?;
|
|
rendezvous::register_module(py, m)?;
|
|
msc4388_rendezvous::register_module(py, m)?;
|
|
segmenter::register_module(py, m)?;
|
|
room_versions::register_module(py, m)?;
|
|
types::register_module(py, m)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub trait UnwrapInfallible<T> {
|
|
fn unwrap_infallible(self) -> T;
|
|
}
|
|
|
|
impl<T> UnwrapInfallible<T> for Result<T, Infallible> {
|
|
fn unwrap_infallible(self) -> T {
|
|
match self {
|
|
Ok(val) => val,
|
|
Err(never) => match never {},
|
|
}
|
|
}
|
|
}
|