diff --git a/src/config.rs b/src/config.rs index 31390ce9c..34493e88c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -337,7 +337,7 @@ impl Context { message::delete_msgs(self, &[MsgId::new(webxdc_message_id)]).await?; } self.sql.set_raw_config(key, value).await?; - self.debug_logging.store(0, atomic::Ordering::Release); + self.debug_logging.store(0, atomic::Ordering::Relaxed); } else { let data: &[u8] = include_bytes!("../assets/webxdc_logging.xdc"); @@ -352,7 +352,7 @@ impl Context { .set_raw_config(key, Some(&msg_id.to_u32().to_string())) .await?; self.debug_logging - .store(msg_id.to_u32(), atomic::Ordering::Release); + .store(msg_id.to_u32(), atomic::Ordering::Relaxed); } } _ => { diff --git a/src/context.rs b/src/context.rs index af21ab2c8..a72e7af17 100644 --- a/src/context.rs +++ b/src/context.rs @@ -442,15 +442,22 @@ impl Context { typ: event.clone(), }); - let context = self.clone(); - let debug_logging = context.debug_logging.load(atomic::Ordering::Acquire); + // `task::spawn()` below is a bit slow, so make sure to only call it if + // debug logging is enabled; that's why we have the Context::debug_logging + // property. Using `spawn()` also has the disadvantage that the logs might + // get out of order. + // Both could be solved by using `task::block_on()` instead, but reportedly, + // this could lead to deadlocks. A better solution would be to make + // `emit_event()` async. + let debug_logging = self.debug_logging.load(atomic::Ordering::Relaxed); if debug_logging > 0 { let time = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap_or_default() .as_millis() as i64; - async_std::task::block_on(async move { + let context = self.clone(); + async_std::task::spawn(async move { let webxdc_instance_id = MsgId::new(debug_logging as u32); match context diff --git a/src/message.rs b/src/message.rs index 004eb32d6..4a86336c0 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1288,7 +1288,7 @@ pub async fn delete_msgs(context: &Context, msg_ids: &[MsgId]) -> Result<()> { .sql .set_raw_config(Config::DebugLogging, Some("0")) .await?; - context.debug_logging.store(0, atomic::Ordering::Release); + context.debug_logging.store(0, atomic::Ordering::Relaxed); } } diff --git a/src/sql.rs b/src/sql.rs index 6809d2efd..709feabdb 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -346,7 +346,7 @@ impl Sql { let debug_logging = self.get_raw_config_u32(Config::DebugLogging).await?; context .debug_logging - .store(debug_logging.unwrap_or(0), atomic::Ordering::Release); + .store(debug_logging.unwrap_or(0), atomic::Ordering::Relaxed); Ok(()) }