Skip to content

Commit 31b2a0a

Browse files
committed
Auto merge of #41582 - jonhoo:reread-nameservers-on-lookup-fail, r=alexcrichton
Reload nameserver information on lookup failure As discussed in #41570, UNIX systems often cache the contents of `/etc/resolv.conf`, which can cause lookup failures to persist even after a network connection becomes available. This patch modifies lookup_host to force a reload of the nameserver entries following a lookup failure. This is in line with what many C programs already do (see #41570 for details). On systems with nscd, this should not be necessary, but not all systems run nscd. Fixes #41570. Depends on rust-lang/libc#585. r? @alexcrichton
2 parents b16c7a2 + db36fc8 commit 31b2a0a

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/libstd/sys_common/net.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,22 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
177177
};
178178
let mut res = ptr::null_mut();
179179
unsafe {
180-
cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints,
181-
&mut res))?;
182-
Ok(LookupHost { original: res, cur: res })
180+
match cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res)) {
181+
Ok(_) => {
182+
Ok(LookupHost { original: res, cur: res })
183+
},
184+
#[cfg(unix)]
185+
Err(e) => {
186+
// The lookup failure could be caused by using a stale /etc/resolv.conf.
187+
// See https://github.com/rust-lang/rust/issues/41570.
188+
// We therefore force a reload of the nameserver information.
189+
c::res_init();
190+
Err(e)
191+
},
192+
// the cfg is needed here to avoid an "unreachable pattern" warning
193+
#[cfg(not(unix))]
194+
Err(e) => Err(e),
195+
}
183196
}
184197
}
185198

0 commit comments

Comments
 (0)