Skip to content

Commit 1309652

Browse files
committed
Reload nameserver information on lookup failure
As discussed in rust-lang#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 rust-lang#41570 for details). On systems with nscd, this should not be necessary, but not all systems run nscd. Fixes rust-lang#41570. Depends on rust-lang/libc#585.
1 parent 4961d72 commit 1309652

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/libstd/sys_common/net.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,20 @@ 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+
e => e,
193+
}
183194
}
184195
}
185196

0 commit comments

Comments
 (0)