[GH-ISSUE #1260] Question / Feature: Shared Trust-DNS and Runtime(tokio/*) #637

Closed
opened 2026-03-15 23:34:20 +03:00 by kerem · 10 comments
Owner

Originally created by @JRAndreassen on GitHub (Oct 21, 2020).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/1260

Hi...

I have a couple of questions regarding instantiating Trust-DNS in a app using Tokio...

  1. Is there a way to tell Trust-DNS to use the "Current" runtime ?
    Since there is already a Tokio instance running, creating another seems a bit excessive.
    (Perhaps use the current and add a few worker threads to it).
    It would be a great addon to Tokio 0.3.0 [#1250]

  2. My application relies on reqwest , which also uses trust-dns...
    Is there a way to share this instance ?
    As it goes, I have 3-tokio runtimes and 2 Trust-DNS instances running

Originally created by @JRAndreassen on GitHub (Oct 21, 2020). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/1260 Hi... I have a couple of questions regarding instantiating Trust-DNS in a app using Tokio... 1) Is there a way to tell Trust-DNS to use the "Current" runtime ? Since there is already a Tokio instance running, creating another seems a bit excessive. (Perhaps use the current and add a few worker threads to it). It would be a great addon to Tokio 0.3.0 [#1250] 2) My application relies on reqwest , which also uses trust-dns... Is there a way to share this instance ? As it goes, I have 3-tokio runtimes and 2 Trust-DNS instances running
kerem closed this issue 2026-03-15 23:34:25 +03:00
Author
Owner

@bluejekyll commented on GitHub (Oct 21, 2020):

To get access to the current Runtime in Tokio, there is this method in 0.2: https://docs.rs/tokio/0.2.22/tokio/runtime/struct.Handle.html#method.current

I believe in tokio 0.3, this method will be the only way, as handle has been removed: https://docs.rs/tokio/0.3.0/tokio/fn.spawn.html

<!-- gh-comment-id:713623513 --> @bluejekyll commented on GitHub (Oct 21, 2020): To get access to the current Runtime in Tokio, there is this method in 0.2: https://docs.rs/tokio/0.2.22/tokio/runtime/struct.Handle.html#method.current I believe in tokio 0.3, this method will be the only way, as handle has been removed: https://docs.rs/tokio/0.3.0/tokio/fn.spawn.html
Author
Owner

@JRAndreassen commented on GitHub (Oct 21, 2020):

@bluejekyll 👍

Thanks for responding...

I tried the handle approach... It did not like it...
It complained about no runtime... at least in the test...
I'll try again in the full run mode...

        let mut  rt_anchor : Option<tokio::runtime::Runtime> = None;

        // Spawn the runtime to a new thread...
        //
        // This thread will manage the actual resolution runtime
        thread::spawn(move || {
            // A runtime for this new thread
            let current_handle = 
                match Handle::try_current() 
                {
                    Ok(curr_handle) => {
                        curr_handle.clone()
                    },
                    Err(_) =>{
                        let runtime = tokio::runtime::Runtime::new().expect("failed to launch Runtime");
                        let ret = runtime.handle().clone();
                        // Have to keep refence
                        rt_anchor = Some(runtime);
                        ret
                    }
                };
...

Yes, I noticed the handle being gone (I had to back off to "0.2"...)
Perhaps the option of passing in a runtime would work and Trust-DNS can spawn what it needs...
TokioAsyncResolver::new(ResolverConfig::quad9(), ResolverOpts::default(), &runtime)
Just a thought...

Thanks
JR

<!-- gh-comment-id:713633113 --> @JRAndreassen commented on GitHub (Oct 21, 2020): @bluejekyll 👍 Thanks for responding... I tried the handle approach... It did not like it... It complained about no runtime... at least in the test... I'll try again in the full run mode... ```rust let mut rt_anchor : Option<tokio::runtime::Runtime> = None; // Spawn the runtime to a new thread... // // This thread will manage the actual resolution runtime thread::spawn(move || { // A runtime for this new thread let current_handle = match Handle::try_current() { Ok(curr_handle) => { curr_handle.clone() }, Err(_) =>{ let runtime = tokio::runtime::Runtime::new().expect("failed to launch Runtime"); let ret = runtime.handle().clone(); // Have to keep refence rt_anchor = Some(runtime); ret } }; ... ``` Yes, I noticed the handle being gone (I had to back off to "0.2"...) Perhaps the option of passing in a runtime would work and Trust-DNS can spawn what it needs... TokioAsyncResolver::new(ResolverConfig::quad9(), ResolverOpts::default(), &runtime) Just a thought... Thanks JR
Author
Owner

@JRAndreassen commented on GitHub (Oct 21, 2020):

Hi…

Yes, I saw the issue to move it forward to 0.3.0…

I’ll be looking out for it, when it is finished…

I did a quick test, using the handle.spawn…

It got stuck somewhere…

I’ll have to chase it later…

Thanks for responding…

JR

From: Benjamin Fry notifications@github.com
Sent: Wednesday, October 21, 2020 9:53 AM
To: bluejekyll/trust-dns trust-dns@noreply.github.com
Cc: JR Andreassen janrune@theandreassens.net; Author author@noreply.github.com
Subject: Re: [bluejekyll/trust-dns] Question / Feature: Shared Trust-DNS and Runtime(tokio/*) (#1260)

To be clear, trust-dns does not yet support Tokio 0.3.

You will need a Runtime associated to the current thread. This can be done in many ways, but the simples is to have a single point where you spawn all the futures.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub https://github.com/bluejekyll/trust-dns/issues/1260#issuecomment-713635267 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ACA5BTHAVNKIG3IVWIZIPBTSL3YWHANCNFSM4SZ2YVSA . https://github.com/notifications/beacon/ACA5BTCHACIGL3DCXNIWVF3SL3YWHA5CNFSM4SZ2YVSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOFKETLQY.gif

<!-- gh-comment-id:713674974 --> @JRAndreassen commented on GitHub (Oct 21, 2020): Hi… Yes, I saw the issue to move it forward to 0.3.0… I’ll be looking out for it, when it is finished… I did a quick test, using the handle.spawn… It got stuck somewhere… I’ll have to chase it later… Thanks for responding… JR From: Benjamin Fry <notifications@github.com> Sent: Wednesday, October 21, 2020 9:53 AM To: bluejekyll/trust-dns <trust-dns@noreply.github.com> Cc: JR Andreassen <janrune@theandreassens.net>; Author <author@noreply.github.com> Subject: Re: [bluejekyll/trust-dns] Question / Feature: Shared Trust-DNS and Runtime(tokio/*) (#1260) To be clear, trust-dns does not yet support Tokio 0.3. You will need a Runtime associated to the current thread. This can be done in many ways, but the simples is to have a single point where you spawn all the futures. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <https://github.com/bluejekyll/trust-dns/issues/1260#issuecomment-713635267> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACA5BTHAVNKIG3IVWIZIPBTSL3YWHANCNFSM4SZ2YVSA> . <https://github.com/notifications/beacon/ACA5BTCHACIGL3DCXNIWVF3SL3YWHA5CNFSM4SZ2YVSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOFKETLQY.gif>
Author
Owner

@djc commented on GitHub (Oct 23, 2020):

I found this bug report a bit vague on the details. How do you notice that you have 3 tokio runtimes and 2 trust-dns instances running? What do you mean by "instances"? Why is having these instantiated a problem for you?

At least for trust-dns-resolver, it doesn't look like AsyncResolver will start its own runtime, so sharing a tokio runtime with other tasks seems fairly straightforward. Can you share some of your application code, or, preferably, a reduced code sample that leads to running 3 tokio runtimes and 2 trust-dns instances?

It does look a little unfortunate that reqwest doesn't give you access to the trust-dns resolver it is using, or a way to configure it, which might be nice, but that would probably require some work on the reqwest side.

<!-- gh-comment-id:715301772 --> @djc commented on GitHub (Oct 23, 2020): I found this bug report a bit vague on the details. How do you notice that you have 3 tokio runtimes and 2 trust-dns instances running? What do you mean by "instances"? Why is having these instantiated a problem for you? At least for trust-dns-resolver, it doesn't look like `AsyncResolver` will start its own runtime, so sharing a tokio runtime with other tasks seems fairly straightforward. Can you share some of your application code, or, preferably, a reduced code sample that leads to running 3 tokio runtimes and 2 trust-dns instances? It does look a little unfortunate that reqwest doesn't give you access to the trust-dns resolver it is using, or a way to configure it, which might be nice, but that would probably require some work on the reqwest side.
Author
Owner

@JRAndreassen commented on GitHub (Oct 23, 2020):

@djc :
Well, I'm sorry... Did not mean to be "vague"...
Regarding the instances..:

  1. #[tokio::main] Or #[tokio::test(core_threads = 4)]
  2. let runtime = tokio::runtime::Runtime::new().expect("failed to launch Runtime"); (See #1259 )
  3. from reqwest::connect
    #[cfg(feature = "trust-dns")]
    pub(crate) fn new_trust_dns() -> crate::Result<HttpConnector> {
        TrustDnsResolver::new()
            .map(hyper::client::HttpConnector::new_with_resolver)
            .map(Self::TrustDns)
            .map_err(crate::error::builder)
    }
}

By default, a tokio runtime will create 1 thread per core... Right...
My machine has 16-cores...
The application, running in production mode has 78 threads....
Though I can't give you the definitive proof, without doing a lot of work,
I think it is fairly safe to say, there is at least 3 Tokio runtimes active...
I will however create a sample (modify #1259 ) with a Reqwest client in addition to the sample above...

Edit: After some more digging...
40 of the threads were caused by wmi-rs, crate...
There are still at least 2 runtimes...
But, I'll keep digging...

JR

<!-- gh-comment-id:715440274 --> @JRAndreassen commented on GitHub (Oct 23, 2020): @djc : Well, I'm sorry... Did not mean to be "vague"... Regarding the instances..: 1) #[tokio::main] Or #[tokio::test(core_threads = 4)] 2) let runtime = tokio::runtime::Runtime::new().expect("failed to launch Runtime"); (See #1259 ) 3) from reqwest::connect ```rust #[cfg(feature = "trust-dns")] pub(crate) fn new_trust_dns() -> crate::Result<HttpConnector> { TrustDnsResolver::new() .map(hyper::client::HttpConnector::new_with_resolver) .map(Self::TrustDns) .map_err(crate::error::builder) } } ``` By default, a tokio runtime will create 1 thread per core... Right... My machine has 16-cores... The application, running in production mode has 78 threads.... Though I can't give you the definitive proof, without doing a lot of work, I think it is fairly safe to say, there is at least 3 Tokio runtimes active... I will however create a sample (modify #1259 ) with a Reqwest client in addition to the sample above... Edit: After some more digging... 40 of the threads were caused by wmi-rs, crate... There are still at least 2 runtimes... But, I'll keep digging... JR
Author
Owner

@djc commented on GitHub (Oct 24, 2020):

If you have #[tokio::main] or #[tokio::test(core_threads = 4)], there's no need to start a runtime explicitly, why are you doing so?

<!-- gh-comment-id:715908696 --> @djc commented on GitHub (Oct 24, 2020): If you have `#[tokio::main]` or `#[tokio::test(core_threads = 4)]`, there's no need to start a runtime explicitly, why are you doing so?
Author
Owner

@JRAndreassen commented on GitHub (Oct 24, 2020):

@djc
Well, I tried to spawn with handle and pass it, but it paniced with a "No runtime"..

I guess the question I should ask is:
What is the correct way to spawn trust-dns when already running in a tokio context ?

I appreciate the pointer's...
JR

<!-- gh-comment-id:715916649 --> @JRAndreassen commented on GitHub (Oct 24, 2020): @djc Well, I tried to spawn with handle and pass it, but it paniced with a "No runtime".. I guess the question I should ask is: What is the correct way to spawn trust-dns when already running in a tokio context ? I appreciate the pointer's... JR
Author
Owner

@bluejekyll commented on GitHub (Oct 24, 2020):

@JRAndreassen, what is it that you want to do? The answer to your question depends a bit on how you plan to use the library.

<!-- gh-comment-id:716053031 --> @bluejekyll commented on GitHub (Oct 24, 2020): @JRAndreassen, what is it that you want to do? The answer to your question depends a bit on how you plan to use the library.
Author
Owner

@JRAndreassen commented on GitHub (Oct 25, 2020):

@bluejekyll ...

It's pretty straight forward and reverse lookups...
I'm using it to time forward resolutions and reverse for display.
So, nothing fancy, just everyday lookups..
I do need to have a "Global" handle since there will be a lot of threads running...
(See #1259 )

I appreciate the assistance.
JR

<!-- gh-comment-id:716072780 --> @JRAndreassen commented on GitHub (Oct 25, 2020): @bluejekyll ... It's pretty straight forward and reverse lookups... I'm using it to time forward resolutions and reverse for display. So, nothing fancy, just everyday lookups.. I do need to have a "Global" handle since there will be a lot of threads running... (See #1259 ) I appreciate the assistance. JR
Author
Owner

@JRAndreassen commented on GitHub (Oct 25, 2020):

I got it to work...
See #1259

<!-- gh-comment-id:716174286 --> @JRAndreassen commented on GitHub (Oct 25, 2020): I got it to work... See #1259
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#637
No description provided.