Using ToSocketAddrs seems to remember EMFILE on the same thread #47955
Open
Description
opened on Feb 2, 2018
This was noticed in hyperium/hyper#1422, where a user tried to trigger more connections than their allowed max file descriptors, and saw the EMFILE error. It was then noticed that afterwards, every call to to_socket_addrs
that requires a DNS lookup would fail from then on. However, trying the same DNS lookup on a new thread would work fine.
I was able to reproduce this using just the standard library here:
use std::net::TcpStream;
fn main() {
let cnt = 30_000; // adjust for your system
let host = "localhost:3000"; // using "127.0.0.1:3000" doesn't have the same problem
let mut sockets = Vec::with_capacity(cnt);
for i in 0..cnt {
match TcpStream::connect(host) {
Ok(tcp) => sockets.push(tcp),
Err(e) => {
println!("error {} after {} connects", e, i);
break;
}
}
}
drop(sockets);
println!("closing all sockets");
// sleep because why not
::std::thread::sleep(::std::time::Duration::from_secs(5));
TcpStream::connect(host).unwrap();
println!("end");
}
Just start up a local server, and try to run this program against it. Also, notice that if you change from "localhost"
to "127.0.0.1"
, the issue doesn't show up.
Activity