mvniekerk / tokio-cron-scheduler

Schedule tasks on Tokio using cron-like annotation
Apache License 2.0
529 stars 59 forks source link

Thread dead lock occures #28

Closed gy0801151351 closed 2 years ago

gy0801151351 commented 2 years ago

version: 0.7.4 os: windows11 rustc: 1.61.0 cargo: 1.61.0

#![allow(unused)]

use std::time::Duration;
use tokio_cron_scheduler::{Job, JobScheduler, JobToRun};

fn main() {
    let handle = std::thread::Builder::new()
        .name("schedule thread".to_string())
        .spawn(move || {
            tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("build runtime failed")
                .block_on(start());
        })
        .expect("spawn thread failed");
    handle.join().expect("join failed");
}

async fn start() {
    let mut sched = JobScheduler::new().expect("new scheduler failed");

    sched.add(
        Job::new("1/2 * * * * *", |uuid, l| {
            println!("I run every 2 seconds");
        })
        .unwrap(),
    );

    sched.start().expect("start schedule failed");
}
mvniekerk commented 2 years ago

Hi @gy0801151351 Thank you for the bug report. I've added an example (https://github.com/mvniekerk/tokio-cron-scheduler/blob/main/examples/simple_job_tokio_in_a_thread.rs).

The issue is new_current_thread() vs new_multi_thread(). I don't have a solution for you in the library apart from advising you to use new_multi_thread() when creating the tokio context.

gy0801151351 commented 2 years ago

Noted! But it works fine with version 0.5.x. This issue is found when I use version 0.6.x or 0.7.x. For some situations, we need run some tasks within local future, where objects with "!Send" are used. So, I wish your crate supporting "new_current_thread() ". Thanks!