diff --git a/Cargo.lock b/Cargo.lock index d9758e6f..7985a411 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3188,7 +3188,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.10.1" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "assign", "js_int", @@ -3210,7 +3210,7 @@ dependencies = [ [[package]] name = "ruma-appservice-api" version = "0.10.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "js_int", "ruma-common", @@ -3222,7 +3222,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.18.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "as_variant", "assign", @@ -3245,7 +3245,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.13.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "as_variant", "base64 0.22.1", @@ -3276,7 +3276,7 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.28.1" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "as_variant", "indexmap 2.7.0", @@ -3301,7 +3301,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.9.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "bytes", "http", @@ -3319,7 +3319,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.9.5" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "js_int", "thiserror 2.0.11", @@ -3328,7 +3328,7 @@ dependencies = [ [[package]] name = "ruma-identity-service-api" version = "0.9.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "js_int", "ruma-common", @@ -3338,7 +3338,7 @@ dependencies = [ [[package]] name = "ruma-macros" version = "0.13.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "cfg-if", "proc-macro-crate", @@ -3353,7 +3353,7 @@ dependencies = [ [[package]] name = "ruma-push-gateway-api" version = "0.9.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "js_int", "ruma-common", @@ -3365,7 +3365,7 @@ dependencies = [ [[package]] name = "ruma-server-util" version = "0.3.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "headers", "http", @@ -3378,7 +3378,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.15.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "base64 0.22.1", "ed25519-dalek", @@ -3394,7 +3394,7 @@ dependencies = [ [[package]] name = "ruma-state-res" version = "0.11.0" -source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8" dependencies = [ "futures-util", "js_int", diff --git a/Cargo.toml b/Cargo.toml index 042587fc..b8c145ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -332,7 +332,7 @@ version = "0.1.2" [workspace.dependencies.ruma] git = "https://github.com/girlbossceo/ruwuma" #branch = "conduwuit-changes" -rev = "c4f55b39900b33b2d443dd12a6a2dab50961fdfb" +rev = "427877d5bc14988ed877e500bbb27f8bc08b84e8" features = [ "compat", "rand", diff --git a/src/admin/debug/commands.rs b/src/admin/debug/commands.rs index a77587b0..49078dde 100644 --- a/src/admin/debug/commands.rs +++ b/src/admin/debug/commands.rs @@ -725,7 +725,7 @@ pub(super) async fn force_set_room_state_from_server( .save_state(room_id.clone().as_ref(), new_room_state) .await?; - let state_lock = self.services.rooms.state.mutex.lock(&room_id).await; + let state_lock = self.services.rooms.state.mutex.lock(&*room_id).await; self.services .rooms .state diff --git a/src/core/utils/mutex_map.rs b/src/core/utils/mutex_map.rs index 9b9821fe..03a4adf1 100644 --- a/src/core/utils/mutex_map.rs +++ b/src/core/utils/mutex_map.rs @@ -1,7 +1,13 @@ -use std::{fmt::Debug, hash::Hash, sync::Arc}; +use std::{ + fmt::Debug, + hash::Hash, + sync::{Arc, TryLockError::WouldBlock}, +}; use tokio::sync::OwnedMutexGuard as Omg; +use crate::{err, Result}; + /// Map of Mutexes pub struct MutexMap { map: Map, @@ -30,16 +36,17 @@ where } #[tracing::instrument(level = "trace", skip(self))] - pub async fn lock(&self, k: &K) -> Guard + pub async fn lock<'a, K>(&'a self, k: &'a K) -> Guard where K: Debug + Send + ?Sized + Sync, - Key: for<'a> From<&'a K>, + Key: TryFrom<&'a K>, + >::Error: Debug, { let val = self .map .lock() .expect("locked") - .entry(k.into()) + .entry(k.try_into().expect("failed to construct key")) .or_default() .clone(); @@ -49,6 +56,51 @@ where } } + #[tracing::instrument(level = "trace", skip(self))] + pub fn try_lock<'a, K>(&self, k: &'a K) -> Result> + where + K: Debug + Send + ?Sized + Sync, + Key: TryFrom<&'a K>, + >::Error: Debug, + { + let val = self + .map + .lock() + .expect("locked") + .entry(k.try_into().expect("failed to construct key")) + .or_default() + .clone(); + + Ok(Guard:: { + map: Arc::clone(&self.map), + val: val.try_lock_owned().map_err(|_| err!("would yield"))?, + }) + } + + #[tracing::instrument(level = "trace", skip(self))] + pub fn try_try_lock<'a, K>(&self, k: &'a K) -> Result> + where + K: Debug + Send + ?Sized + Sync, + Key: TryFrom<&'a K>, + >::Error: Debug, + { + let val = self + .map + .try_lock() + .map_err(|e| match e { + | WouldBlock => err!("would block"), + | _ => panic!("{e:?}"), + })? + .entry(k.try_into().expect("failed to construct key")) + .or_default() + .clone(); + + Ok(Guard:: { + map: Arc::clone(&self.map), + val: val.try_lock_owned().map_err(|_| err!("would yield"))?, + }) + } + #[must_use] pub fn contains(&self, k: &Key) -> bool { self.map.lock().expect("locked").contains_key(k) }