diff --git a/src/context.rs b/src/context.rs index 9aac12140..1df160c04 100644 --- a/src/context.rs +++ b/src/context.rs @@ -442,13 +442,20 @@ impl Context { typ: event.clone(), }); - // `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. + // `task::block_on()` below is not how async is meant to be used + // since `emit_event()` is often called from an async context. + // This could generally lead to deadlocks, so we make sure to only + // call it if `debug_logging` is on. It's not that much of a problem + // since most of the things we do in the `async` block - esp. the + // database access - is blocking anyway. + // + // A better solution would be to make `emit_event()` async or to + // create a debug_logger background loop. + // + // Alternatively, we could use `task::spawn()`; this would be + // non-deterministic, so that the logs might get out of order, and + // it could make e.g. backups fail since it could still run in the + // background while trying to move/close the database. let debug_logging = self.debug_logging.load(atomic::Ordering::Relaxed); if debug_logging > 0 { let time = SystemTime::now() @@ -457,7 +464,7 @@ impl Context { .as_millis() as i64; let context = self.clone(); - async_std::task::spawn(async move { + async_std::task::block_on(async move { let webxdc_instance_id = MsgId::new(debug_logging as u32); match context