Fix race in multi_bind_in_range (#9493)
(cherry picked from commit ee72714c08
)
This commit is contained in:
@ -307,13 +307,32 @@ pub fn multi_bind_in_range(
|
|||||||
}
|
}
|
||||||
let mut sockets = Vec::with_capacity(num);
|
let mut sockets = Vec::with_capacity(num);
|
||||||
|
|
||||||
let port = {
|
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)?;
|
let (port, _) = bind_in_range(ip_addr, range)?;
|
||||||
port
|
port
|
||||||
}; // drop the probe, port should be available... briefly.
|
}; // drop the probe, port should be available... briefly.
|
||||||
|
|
||||||
for _ in 0..num {
|
for _ in 0..num {
|
||||||
sockets.push(bind_to(ip_addr, port, true)?);
|
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))
|
Ok((port, sockets))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user