Skip to content

Commit ebfe917

Browse files
committed
Fix tokio panic
This might not be needed after all because of tokio-rs/tokio#5710, but since I already wrote the code, I created a PR anyway.
1 parent 5b435d1 commit ebfe917

File tree

16 files changed

+65
-29
lines changed

16 files changed

+65
-29
lines changed

deltachat-jsonrpc/src/api/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use anyhow::{anyhow, bail, ensure, Context, Result};
66
pub use deltachat::accounts::Accounts;
77
use deltachat::message::get_msg_read_receipts;
88
use deltachat::qr::Qr;
9+
use deltachat::tools;
910
use deltachat::{
1011
chat::{
1112
self, add_contact_to_chat, forward_msgs, get_chat_media, get_chat_msgs, get_chat_msgs_ex,
@@ -152,7 +153,7 @@ impl CommandApi {
152153
impl CommandApi {
153154
/// Test function.
154155
async fn sleep(&self, delay: f64) {
155-
tokio::time::sleep(std::time::Duration::from_secs_f64(delay)).await
156+
tools::sleep(std::time::Duration::from_secs_f64(delay)).await
156157
}
157158

158159
// ---------------------------------------------

examples/simple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use deltachat::chat::{self, ChatId};
2-
use deltachat::chatlist::*;
32
use deltachat::config;
43
use deltachat::contact::*;
54
use deltachat::context::*;
65
use deltachat::message::Message;
76
use deltachat::stock_str::StockStrings;
7+
use deltachat::{chatlist::*, tools};
88
use deltachat::{EventType, Events};
99
use tempfile::tempdir;
1010

@@ -80,7 +80,7 @@ async fn main() {
8080
}
8181

8282
// wait for the message to be sent out
83-
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
83+
tools::sleep(std::time::Duration::from_secs(1)).await;
8484

8585
log::info!("fetching chats..");
8686
let chats = Chatlist::try_load(&ctx, 0, None, None).await.unwrap();

python/tests/test_3_offline.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,19 @@ def test_create_contact(self, acfactory):
481481
contact2 = ac1.create_contact("display1 <[email protected]>", "real")
482482
assert contact2.name == "real"
483483

484+
def test_send_lots_of_offline_msgs(self, acfactory):
485+
ac1 = acfactory.get_pseudo_configured_account()
486+
ac1.set_config("configured_mail_server", "example.org")
487+
ac1.set_config("configured_mail_user", "example.org")
488+
ac1.set_config("configured_mail_pw", "example.org")
489+
ac1.set_config("configured_send_server", "example.org")
490+
ac1.set_config("configured_send_user", "example.org")
491+
ac1.set_config("configured_send_pw", "example.org")
492+
ac1.start_io()
493+
chat = ac1.create_contact("[email protected]", name="some1").create_chat()
494+
for i in range(50):
495+
chat.send_text("hello")
496+
484497
def test_create_chat_simple(self, acfactory):
485498
ac1 = acfactory.get_pseudo_configured_account()
486499
contact1 = ac1.create_contact("[email protected]", name="some1")

src/accounts.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,10 @@ impl AccountConfig {
510510
#[cfg(test)]
511511
mod tests {
512512
use super::*;
513-
use crate::stock_str::{self, StockMessage};
513+
use crate::{
514+
stock_str::{self, StockMessage},
515+
tools,
516+
};
514517

515518
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
516519
async fn test_account_new_open() {
@@ -732,7 +735,7 @@ mod tests {
732735

733736
// Test that event emitter does not return `None` immediately.
734737
let duration = std::time::Duration::from_millis(1);
735-
assert!(tokio::time::timeout(duration, event_emitter.recv())
738+
assert!(tools::timeout(duration, event_emitter.recv())
736739
.await
737740
.is_err());
738741

src/authres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ Authentication-Results: dkim=";
669669

670670
// Sleep to make sure key reset is ignored because of DKIM failure
671671
// and not because reordering is suspected.
672-
tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
672+
tools::sleep(std::time::Duration::from_millis(1100)).await;
673673

674674
let bob2 = tcm.unconfigured().await;
675675
bob2.configure_addr("[email protected]").await;

src/chat.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,6 +3798,7 @@ mod tests {
37983798
use crate::message::delete_msgs;
37993799
use crate::receive_imf::receive_imf;
38003800
use crate::test_utils::TestContext;
3801+
use crate::tools;
38013802
use tokio::fs;
38023803

38033804
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -4107,17 +4108,17 @@ mod tests {
41074108

41084109
add_contact_to_chat(&alice, alice_chat_id, claire_id).await?;
41094110
let add2 = alice.pop_sent_msg().await;
4110-
tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
4111+
tools::sleep(std::time::Duration::from_millis(1100)).await;
41114112

41124113
add_contact_to_chat(&alice, alice_chat_id, daisy_id).await?;
41134114
let add3 = alice.pop_sent_msg().await;
4114-
tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
4115+
tools::sleep(std::time::Duration::from_millis(1100)).await;
41154116

41164117
assert_eq!(get_chat_contacts(&alice, alice_chat_id).await?.len(), 4);
41174118

41184119
remove_contact_from_chat(&alice, alice_chat_id, claire_id).await?;
41194120
let remove1 = alice.pop_sent_msg().await;
4120-
tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
4121+
tools::sleep(std::time::Duration::from_millis(1100)).await;
41214122

41224123
remove_contact_from_chat(&alice, alice_chat_id, daisy_id).await?;
41234124
let remove2 = alice.pop_sent_msg().await;
@@ -4154,11 +4155,11 @@ mod tests {
41544155

41554156
send_text_msg(&alice, alice_chat_id, "populate".to_string()).await?;
41564157
let add = alice.pop_sent_msg().await;
4157-
tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
4158+
tools::sleep(std::time::Duration::from_millis(1100)).await;
41584159

41594160
remove_contact_from_chat(&alice, alice_chat_id, claire_id).await?;
41604161
let remove1 = alice.pop_sent_msg().await;
4161-
tokio::time::sleep(std::time::Duration::from_millis(1100)).await;
4162+
tools::sleep(std::time::Duration::from_millis(1100)).await;
41624163

41634164
remove_contact_from_chat(&alice, alice_chat_id, daisy_id).await?;
41644165
let remove2 = alice.pop_sent_msg().await;
@@ -4752,9 +4753,9 @@ mod tests {
47524753
.await
47534754
.unwrap()
47544755
.chat_id;
4755-
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
4756+
tools::sleep(std::time::Duration::from_millis(1000)).await;
47564757
let chat_id2 = t.get_self_chat().await.id;
4757-
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
4758+
tools::sleep(std::time::Duration::from_millis(1000)).await;
47584759
let chat_id3 = create_group_chat(&t, ProtectionStatus::Unprotected, "foo")
47594760
.await
47604761
.unwrap();

src/contact.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use regex::Regex;
1616
use rusqlite::OptionalExtension;
1717
use serde::{Deserialize, Serialize};
1818
use tokio::task;
19-
use tokio::time::{timeout, Duration};
19+
use tokio::time::Duration;
2020

2121
use crate::aheader::EncryptPreference;
2222
use crate::chat::ChatId;
@@ -32,6 +32,7 @@ use crate::mimeparser::AvatarAction;
3232
use crate::param::{Param, Params};
3333
use crate::peerstate::{Peerstate, PeerstateVerifiedStatus};
3434
use crate::sql::{self, params_iter};
35+
use crate::tools::timeout;
3536
use crate::tools::{
3637
duration_to_str, get_abs_path, improve_single_line_input, strip_rtlo_characters, time,
3738
EmailAddress,

src/ephemeral.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
7171
use anyhow::{ensure, Result};
7272
use async_channel::Receiver;
7373
use serde::{Deserialize, Serialize};
74-
use tokio::time::timeout;
7574

7675
use crate::chat::{send_msg, ChatId};
7776
use crate::constants::{DC_CHAT_ID_LAST_SPECIAL, DC_CHAT_ID_TRASH};
@@ -84,6 +83,7 @@ use crate::message::{Message, MessageState, MsgId, Viewtype};
8483
use crate::mimeparser::SystemMessage;
8584
use crate::sql::{self, params_iter};
8685
use crate::stock_str;
86+
use crate::tools::timeout;
8787
use crate::tools::{duration_to_str, time};
8888

8989
/// Ephemeral timer value.

src/imap/idle.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use futures_lite::FutureExt;
88
use super::session::Session;
99
use super::Imap;
1010
use crate::imap::{client::IMAP_TIMEOUT, FolderMeaning};
11+
use crate::tools;
1112
use crate::{context::Context, scheduler::InterruptInfo};
1213

1314
const IDLE_TIMEOUT: Duration = Duration::from_secs(23 * 60);
@@ -97,7 +98,7 @@ impl Session {
9798
}
9899
}
99100

100-
let mut session = tokio::time::timeout(Duration::from_secs(15), handle.done())
101+
let mut session = tools::timeout(Duration::from_secs(15), handle.done())
101102
.await
102103
.with_context(|| format!("{folder_name}: IMAP IDLE protocol timed out"))?
103104
.with_context(|| format!("{folder_name}: IMAP IDLE failed"))?;

src/imex.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ mod tests {
790790
use crate::pgp::{split_armored_data, HEADER_AUTOCRYPT, HEADER_SETUPCODE};
791791
use crate::stock_str::StockMessage;
792792
use crate::test_utils::{alice_keypair, TestContext};
793+
use crate::tools;
793794

794795
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
795796
async fn test_render_setup_file() {
@@ -963,7 +964,7 @@ mod tests {
963964
// The database is still unconfigured;
964965
// fill the config cache with the old value.
965966
context2.is_configured().await.ok();
966-
tokio::time::sleep(Duration::from_micros(1)).await;
967+
tools::sleep(Duration::from_micros(1)).await;
967968
}
968969

969970
// Assert that the config cache has the new value now.

0 commit comments

Comments
 (0)