libunbound: setting a deadline/timeout for ub_resolve

W.C.A. Wijngaards wouter at
Fri Jun 8 05:56:38 UTC 2018

Hi Alex,

On 08/06/18 01:57, Alex Zorin via Unbound-users wrote:
> Hello,
> I'm using libunbound to perform iterative DNS lookups for a diagnostic service: .
> One of the problems I have is when one or more of a domain's authoritative nameservers are timing out, ub_resolve takes an incredibly long time to give up, something like ~10 minutes when the ub_ctx is configured with these options:

If a timeout is needed, set a timeout.  So the code calling libunbound
sets the timeout it wants.  And then cancels or ignores the return value
of the lookup.

For example, use ub_resolve_async(), and then ub_fd(ctx) put that file
descriptor in select or poll, with a timeout that you want to select or
poll. So, sketching code that would look like this:
ub_resolve_async(ctx, name, type, class, 0, funcwhendone, &async_id);
struct pollfd fds[1];
fds[0].fd = ub_fd(ctx);
fds[0].events = POLLIN;
fds[0].revents = 0;
int r = poll(fds, 1, 180 * 1000);
if(r == 0) /* timeout */
else if(r == -1) /* poll has a system error */
else ub_process(ctx); /* calls funcwhendone */

At timeout you could call ub_cancel(ctx, async_id) or just ignore the
result when funcwhendone gets called (eventually).  Or delete the
context, you say you only use it once, this also removes that callback.

Best regards, Wouter

> I've tried to grok the meaning of but I'm not seeing the solution after some weeks of pondering the problem..
> Ideally I would like to place a deadline of something like 180 seconds on the ub_resolve operation. I am using a fresh ub_ctx for each lookup, which is done to avoid sharing any resolver state between lookups.
> Do you have any further guidance on the ability to restrict ub_resolve's runtime?
> Thanks!
> Alex

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the Unbound-users mailing list