fix(service): Mark state::create unsafe and document lifetime invariants

This commit is contained in:
Jade Ellis committed 2026-11-21 15:50:28 +00:00
1 parent f81eee7710
commit aabf631f85
2 files changed
+14 -2

No files matched your search

+5 -1
View File
@@ -8,7 +8,11 @@ use ruma::api::error::ErrorKind;
pub(crate) fn build(services: &Arc<Services>) -> (Router, Guard) {
let router = Router::<state::State>::new();
let (state, guard) = state::create(services.clone());
// SAFETY: `guard` is returned alongside `router` and held in `serve()`
// until the HTTP server has finished serving all requests and dropped
// `router`, ensuring no `State` copy can be dereferenced after `Services`
// is dropped.
let (state, guard) = unsafe { state::create(services.clone()) };
let router = conduwuit_api::router::build(router, state)
.merge(conduwuit_web::build(services))
.fallback(not_found)
+9 -1
View File
@@ -11,7 +11,15 @@ pub struct Guard {
services: Arc<Services>,
}
pub fn create(services: Arc<Services>) -> (State, Guard) {
/// Create a raw-pointer `State` handle paired with a `Guard` that keeps
/// `Services` alive.
///
/// # Safety
/// `State` is `Copy` and dereferences its raw pointer without reference
/// counting. The caller must ensure that no `State` (nor any copy of it) is
/// dereferenced after `Guard` and all other `Arc<Services>` clones have been
/// dropped.
pub unsafe fn create(services: Arc<Services>) -> (State, Guard) {
let state = State {
services: Arc::into_raw(services.clone()),
};