Skip to content

Commit ece4006

Browse files
committed
qemu.go: retry if there is a race condition to bind port 2049
There is a race condition for `ostree.sync` and `kdump.crash.nfs` to bind port 2049, add retry if there is, instead of retrun err immediately. Fixes: coreos/rhel-coreos-config#89
1 parent 82410e1 commit ece4006

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

mantle/platform/qemu.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,23 @@ func (builder *QemuBuilder) setupNetworking() error {
645645
address := fmt.Sprintf(":%d", builder.requestedHostForwardPorts[i].HostPort)
646646
// Possible race condition between getting the port here and using it
647647
// with qemu -- trade off for simpler port management
648-
l, err := net.Listen("tcp", address)
648+
var l net.Listener
649+
var err error
650+
maxRetries := 12
651+
for attempt := 1; attempt <= maxRetries; attempt++ {
652+
l, err = net.Listen("tcp", address)
653+
if err == nil {
654+
l.Close()
655+
break
656+
}
657+
658+
fmt.Printf("Failed to listen on %s: %v, retrying (%d/%d)...\n",
659+
address, err, attempt, maxRetries)
660+
time.Sleep(time.Duration(5) * time.Second)
661+
}
649662
if err != nil {
650663
return err
651664
}
652-
l.Close()
653665
builder.requestedHostForwardPorts[i].HostPort = l.Addr().(*net.TCPAddr).Port
654666
netdev += fmt.Sprintf(",hostfwd=tcp:127.0.0.1:%d-:%d",
655667
builder.requestedHostForwardPorts[i].HostPort,

0 commit comments

Comments
 (0)