telegram-rs / telegram-bot

Rust Library for creating a Telegram Bot
MIT License
940 stars 157 forks source link

UpdatesStream is not Send => can't be used with spawn() #192

Closed netvl closed 4 years ago

netvl commented 4 years ago

This very simple piece of code fails to compile with a very scary error:

fn test_bot() {
    tokio::task::spawn(async {
        let api: Api = todo!();
        let updates = api.stream().fuse();
        loop {
            select! {
                upd = updates.select_next_some() => println!("{:?}", upd),
            }
        }
    });
}
error[E0277]: `(dyn core::future::future::Future<Output = std::result::Result<std::option::Option<std::vec::Vec<telegram_bot_raw::types::update::Update>>, telegram_bot::errors::Error>> + 'static)` cannot be sent between threads safely
   --> src/server/bot.rs:56:5
    |
56  |     tokio::task::spawn(async {
    |     ^^^^^^^^^^^^^^^^^^ `(dyn core::future::future::Future<Output = std::result::Result<std::option::Option<std::vec::Vec<telegram_bot_raw::types::update::Update>>, telegram_bot::errors::Error>> + 'static)` cannot be sent between threads safely
    | 
   ::: /home/netvl/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.6/src/task/spawn.rs:123:21
    |
123 |         T: Future + Send + 'static,
    |                     ---- required by this bound in `tokio::task::spawn::spawn`
    |
...

This error points out the fact that the current_request field is not Send, because the nested trait object is not Box<... + Send>.

This seems to make it impossible to run any kind of update handling procedure in sub-tasks (created by various spawn methods), but only in the "main" task (created by the block_on kind of methods). This is unfortunately a hard restriction for many complex projects.

gugahoa commented 4 years ago

Closed by #200

zvirja commented 4 years ago

@gugahoa Could you please trigger a new release so we can consume this change? Now I'm blocked to proceed further, as I'm forced to use tokio_compat:: API and I need to have future implementing Send 😟

Thank you!

UPD: Found that actually I can specify git repo directly in dependencies (like telegram-bot = { git = "https://github.com/telegram-rs/telegram-bot.git" }), but still would be cool to use it directly from package store.

keithmss commented 4 years ago

Only disadvantage to this is that I'm also using the tracing crate. My application is now being flooded with trace and debug messages from this crate...