vbox: fix race with getting a tcp port

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-03-19 15:30:02 -04:00
parent 407f75d109
commit 4e10204b8e
No known key found for this signature in database
GPG Key ID: A519480096146526
1 changed files with 20 additions and 10 deletions

View File

@ -628,15 +628,25 @@ func zeroFill(w io.Writer, n int64) error {
}
func getAvailableTCPPort() (int, error) {
// FIXME: this has a race condition between finding an available port and
// virtualbox using that port. Perhaps we should randomly pick an unused
// port in a range not used by kernel for assigning ports
ln, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
return 0, err
port := 0
for {
ln, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
return 0, err
}
defer ln.Close()
addr := ln.Addr().String()
addrParts := strings.SplitN(addr, ":", 2)
p, err := strconv.Atoi(addrParts[1])
if err != nil {
return 0, err
}
if p != 0 {
port = p
break
}
time.Sleep(1)
}
defer ln.Close()
addr := ln.Addr().String()
addrParts := strings.SplitN(addr, ":", 2)
return strconv.Atoi(addrParts[1])
return port, nil
}