Files
synapse/tests/synapse_rust
Erik JohnstonandClaude Fable 5 ec2894b4a7 Store the current logcontext in Rust (unified task-local + thread-local)
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
2026-07-16 13:21:46 +00:00
..