Use spawn() instead of block_on() again because it reportedly might lead to deadlocks

Also, fix some minor stuff
This commit is contained in:
Hocuri
2022-05-09 18:41:40 +02:00
committed by Septias
parent 2fdab88d19
commit 5a856eccef
4 changed files with 14 additions and 7 deletions

View File

@@ -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);
}
}
_ => {

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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(())
}