From a7178f4f25ba166751fb448483180deb8bc98b4d Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Sun, 6 Sep 2020 18:38:12 +0200 Subject: [PATCH] Hack to fix group chat creation race condition In the current design the dc_receive_imf() pipeline calls handle_securejoin_handshake() before it creates the group. However handle_securejoin_handshake() already signals to securejoin() that the chat exists, which is not true. The proper fix would be to re-desing how group-creation works, potentially allowinng handle_securejoin_handshake() to already create the group and no longer require any further processing by dc_receive_imf(). --- src/securejoin.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/securejoin.rs b/src/securejoin.rs index f89be7eae..33409eae8 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -1,6 +1,6 @@ //! Verified contact protocol implementation as [specified by countermitm project](https://countermitm.readthedocs.io/en/stable/new.html#setup-contact-protocol) -use std::time::Duration; +use std::time::{Duration, Instant}; use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC}; @@ -351,6 +351,27 @@ async fn securejoin(context: &Context, qr: &str) -> ChatId { while !context.shall_stop_ongoing().await { async_std::task::sleep(Duration::from_millis(50)).await; } + + // handle_securejoin_handshake() calls Context::stop_ongoing before the group chat + // is created (it is created after handle_securejoin_handshake() returns by + // dc_receive_imf()). As a hack we just wait a bit for it to appear. + let start = Instant::now(); + while start.elapsed() < Duration::from_secs(7) { + { + let bob = context.bob.read().await; + if chat::get_chat_id_by_grpid( + context, + bob.qr_scan.as_ref().unwrap().text2.as_ref().unwrap(), + ) + .await + .is_ok() + { + break; + } + } + async_std::task::sleep(Duration::from_millis(50)).await + } + cleanup(&context, contact_chat_id, true, join_vg).await } else { // for a one-to-one-chat, the chat is already known, return the chat-id,