Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"nodename nor servname provided, or not known" when trying to connect to google.com on MacOS #1285

Closed
jamesmcl113 opened this issue Jun 14, 2021 · 11 comments

Comments

@jamesmcl113
Copy link

jamesmcl113 commented Jun 14, 2021

I'm getting this error :
Error: reqwest::Error { kind: Request, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("www.google.com")), port: None, path: "/", query: None, fragment: None }, source: hyper::Error(Connect, ConnectError("dns error", Custom { kind: Other, error: "failed to lookup address information: nodename nor servname provided, or not known" })) }

when I run this code:

extern crate reqwest;
extern crate select;
extern crate tokio;

use select::document::Document;
use select::predicate::Class;

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    get_website("https://wwww.google.com").await?;
    Ok(())
}

async fn get_website(url: &str) -> Result<(), reqwest::Error> { 
    let body = reqwest::get(url).await?.text().await?;
    println!("{:?}", body);
    Ok(())
}

Note that it works for other sites, namely reddit.com, however it also fails on rust-lang.org, twitter.com among others. It's seemingly a bit random which sites it will work on. ATM I'm assuming this is something to do with my setup, running MacOS 10.14.6, but I am unsure.

I've also tried it using reqwest in blocking mode and the same issues persist with an identical error.

@seanmonstar
Copy link
Owner

In the example you posted, there's 4 "w"s instead of the normal 3 "www". Could that be it?

@jamesmcl113
Copy link
Author

Apologies for that mistake, but sadly no ,that is not the error

@seanmonstar
Copy link
Owner

That does sound like a setup issue. Here's a simpler program using just libstd, which is essentially how reqwest is looking up DNS:

use std::net::ToSocketAddrs;

fn main() {
    let _ = dbg!(("www.google.com", 80).to_socket_addrs());
}

@maxcountryman
Copy link

I'm running into this same problem, but only intermittently (after running for several minutes or even hours) where I'm pinging the same URL every several minutes. I'm running macOS 13.1.

In my case I'm wondering if I should be using some additional error handling and retrying when this occurs?

@ta3pks
Copy link

ta3pks commented Dec 27, 2022

This seems to happen when there are too many lookups say in a loop. If you resolve the domain once and use the ip from the resolution in the requests things seems to work just fine

@maxcountryman
Copy link

That makes sense. Is there a pattern for doing so? Since I'm using reqwest the DNS resolution is somewhat opaque in my program.

@ta3pks
Copy link

ta3pks commented Dec 28, 2022

@maxcountryman before the loop I just get the addresses using to_socket_addrs and get the first ipv4 from the iter returned from to_socket_addrs.

@shenshouer
Copy link

shenshouer commented Mar 29, 2023

I met the same issue on all platform(MacOS, CentOS, Debian), the DNS like:

% dig bizlog-lab.xxx-inc.com

; <<>> DiG 9.10.6 <<>> bizlog-lab.xxx-inc.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51009
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1408
;; QUESTION SECTION:
;bizlog-lab.xxx-inc.com.                IN      A

;; ANSWER SECTION:
bizlog-lab.xxx-inc.com. 43      IN      CNAME   oif-orp1.xxx-inc.com.
oif-orp1.xxx-inc.com.   43      IN      A       10.0.2.1

;; Query time: 11 msec
;; SERVER: 10.0.0.68#53(10.0.0.68)
;; WHEN: Wed Mar 29 11:38:22 CST 2023
;; MSG SIZE  rcvd: 90

And I got error:

error sending request for url ([https://bizlog-lab.xxx-inc.com'](https://bizlog-lab.xxx-inc.com%27/))/s/default/api/saved_objects/_find?type=index-pattern&fields=title&per_page=10000): error trying to connect: dns error: failed to lookup address information: nodename nor servname provided, or not known

@seanmonstar
Copy link
Owner

The way reqwest does DNS, as mentioned above, it simply to use the method from libstd. Any issues there would be problems with your libc getaddrinfo.

You can try enabling the trust-dns feature of reqwest, and setting that on your ClientBuilder, to use a pure-Rust resolver.

I don't think there's anything reqwest can do otherwise, so I'm going to close this.

@seanmonstar seanmonstar closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2023
@shenshouer
Copy link

shenshouer commented Mar 30, 2023

I have already tried enabling the Trust-DNS feature, but the problem still exists on three platforms. Currently, I have rewritten my code using Golang to solve the problem.

@froody
Copy link

froody commented Jul 24, 2023

I solved this by caching the dns lookups in the client with resolve_to_addrs:

use std::net::ToSocketAddrs;
let sock_addrs = format!("{host}:443").to_socket_addrs()?.collect::<Vec<_>>();
let client = reqwest::Client::builder()
    .resolve_to_addrs(host, &sock_addrs)
    .build()?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants