[GH-ISSUE #2142] tokio::JoinSet and hickory_resolver::Resolver causes "Cannot start a runtime from within a runtime" #901

Open
opened 2026-03-16 00:47:58 +03:00 by kerem · 2 comments
Owner

Originally created by @ktravelet on GitHub (Feb 9, 2024).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2142

Rust newb here. I'm assuming I'm doing something wrong but I cant figure it out.

I created a minimal repro with the error below:

#[tokio::main]
async fn main() -> Result<(),()> {

    let max_async_threads = 1;
    let mut fqdns = Vec::<String>::new();

    while fqdns.len() < 2 {
        fqdns.push("google.com.".to_string());
    }

    let mut join_set = JoinSet::<(String)>::new();
    for fqdn in fqdns {
        
        while join_set.len() >= max_async_threads {
            let result = join_set.join_next().await.expect("Async to return").expect("DNS to resolve");
            println!("{}", result);
        }

        join_set.spawn(some_async(fqdn));
    }

    while let Some(result) = join_set.join_next().await {
        let address = result.expect("DNS to return");
        println!("{}", address);
    }
    
    Ok(())
}

async fn some_async(fqdn: String) -> String {
    let resolver: Resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default()).unwrap();
    let response: Ipv4Lookup = resolver.ipv4_lookup(fqdn).unwrap();
    let address = response.iter().next().expect("no addresses returned!");
    address.to_string()
}
thread 'thread 'tokio-runtime-workertokio-runtime-worker' panicked at ' panicked at C:\Users\kytravel\.cargo\registry\src\index.crates.io-6f17d22bba15001f\hickory-resolver-0.24.0\src\resolver.rsC:\Users\kytravel\.cargo\registry\src\index.crates.io-6f17d22bba15001f\hickory-resolver-0.24.0\src\resolver.rs::149149::55:
:
Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
Originally created by @ktravelet on GitHub (Feb 9, 2024). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2142 Rust newb here. I'm assuming I'm doing something wrong but I cant figure it out. I created a minimal repro with the error below: ``` #[tokio::main] async fn main() -> Result<(),()> { let max_async_threads = 1; let mut fqdns = Vec::<String>::new(); while fqdns.len() < 2 { fqdns.push("google.com.".to_string()); } let mut join_set = JoinSet::<(String)>::new(); for fqdn in fqdns { while join_set.len() >= max_async_threads { let result = join_set.join_next().await.expect("Async to return").expect("DNS to resolve"); println!("{}", result); } join_set.spawn(some_async(fqdn)); } while let Some(result) = join_set.join_next().await { let address = result.expect("DNS to return"); println!("{}", address); } Ok(()) } async fn some_async(fqdn: String) -> String { let resolver: Resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default()).unwrap(); let response: Ipv4Lookup = resolver.ipv4_lookup(fqdn).unwrap(); let address = response.iter().next().expect("no addresses returned!"); address.to_string() } ``` ``` thread 'thread 'tokio-runtime-workertokio-runtime-worker' panicked at ' panicked at C:\Users\kytravel\.cargo\registry\src\index.crates.io-6f17d22bba15001f\hickory-resolver-0.24.0\src\resolver.rsC:\Users\kytravel\.cargo\registry\src\index.crates.io-6f17d22bba15001f\hickory-resolver-0.24.0\src\resolver.rs::149149::55: : Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks. ```
Author
Owner

@bluejekyll commented on GitHub (Feb 9, 2024):

The docs should be a little clearer, and maybe we should disable this sync interface by default. You want to instead use the AsyncResolver: https://docs.rs/hickory-resolver/latest/hickory_resolver/struct.AsyncResolver.html#method.tokio

<!-- gh-comment-id:1936482610 --> @bluejekyll commented on GitHub (Feb 9, 2024): The docs should be a little clearer, and maybe we should disable this sync interface by default. You want to instead use the `AsyncResolver`: https://docs.rs/hickory-resolver/latest/hickory_resolver/struct.AsyncResolver.html#method.tokio
Author
Owner

@ktravelet commented on GitHub (Feb 9, 2024):

that was the ticket! Thanks so much. Do you have a "buy me a coffee" link?

FYI for anyone coming across this. Correct code is here:

#[tokio::main]
async fn main() -> Result<(),()> {
    let max_async_threads = 10;
    let mut fqdns = Vec::<String>::new();

    while fqdns.len() < 100 {
        fqdns.push("google.com.".to_string());
    }

    let mut join_set = JoinSet::<(String)>::new();
    for fqdn in fqdns {
        
        while join_set.len() >= max_async_threads {
            let result = join_set.join_next().await.expect("Async to return").expect("DNS to resolve");
            println!("{}", result);
        }

        join_set.spawn(some_async(fqdn));
    }

    while let Some(result) = join_set.join_next().await {
        let address = result.expect("DNS to return");
        println!("{}", address);
    }
    
    Ok(())
}

async fn some_async(fqdn: String) -> String {
    let resolver = AsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default());
    let response: Ipv4Lookup = resolver.ipv4_lookup(fqdn).await.unwrap();
    let address = response.iter().next().expect("no addresses returned!");
    address.to_string()
}
<!-- gh-comment-id:1936515675 --> @ktravelet commented on GitHub (Feb 9, 2024): that was the ticket! Thanks so much. Do you have a "buy me a coffee" link? FYI for anyone coming across this. Correct code is here: ``` #[tokio::main] async fn main() -> Result<(),()> { let max_async_threads = 10; let mut fqdns = Vec::<String>::new(); while fqdns.len() < 100 { fqdns.push("google.com.".to_string()); } let mut join_set = JoinSet::<(String)>::new(); for fqdn in fqdns { while join_set.len() >= max_async_threads { let result = join_set.join_next().await.expect("Async to return").expect("DNS to resolve"); println!("{}", result); } join_set.spawn(some_async(fqdn)); } while let Some(result) = join_set.join_next().await { let address = result.expect("DNS to return"); println!("{}", address); } Ok(()) } async fn some_async(fqdn: String) -> String { let resolver = AsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default()); let response: Ipv4Lookup = resolver.ipv4_lookup(fqdn).await.unwrap(); let address = response.iter().next().expect("no addresses returned!"); address.to_string() } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hickory-dns#901
No description provided.