Fix race in multi_bind_in_range (#9493)

(cherry picked from commit ee72714c08)
This commit is contained in:
sakridge
2020-04-14 13:34:41 -07:00
committed by Michael Vines
parent 8021d368fe
commit 6f95524be3

View File

@ -307,13 +307,32 @@ pub fn multi_bind_in_range(
}
let mut sockets = Vec::with_capacity(num);
let port = {
let (port, _) = bind_in_range(ip_addr, range)?;
port
}; // drop the probe, port should be available... briefly.
const NUM_TRIES: usize = 100;
let mut port = 0;
let mut error = None;
for _ in 0..NUM_TRIES {
port = {
let (port, _) = bind_in_range(ip_addr, range)?;
port
}; // drop the probe, port should be available... briefly.
for _ in 0..num {
sockets.push(bind_to(ip_addr, port, true)?);
for _ in 0..num {
let sock = bind_to(ip_addr, port, true);
if let Ok(sock) = sock {
sockets.push(sock);
} else {
error = Some(sock);
break;
}
}
if sockets.len() == num {
break;
} else {
sockets.clear();
}
}
if sockets.len() != num {
error.unwrap()?;
}
Ok((port, sockets))
}