Skip to content

Commit f8a3eac

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 f8a3eac

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

mantle/platform/qemu.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,24 @@ 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+
const maxRetries = 12
651+
const retryDelay = 5 * time.Second
652+
for attempt := 1; attempt <= maxRetries; attempt++ {
653+
l, err = net.Listen("tcp", address)
654+
if err == nil {
655+
l.Close()
656+
break
657+
}
658+
659+
fmt.Printf("Failed to listen on %s: %v, retrying (%d/%d)...\n",
660+
address, err, attempt, maxRetries)
661+
time.Sleep(retryDelay)
662+
}
649663
if err != nil {
650664
return err
651665
}
652-
l.Close()
653666
builder.requestedHostForwardPorts[i].HostPort = l.Addr().(*net.TCPAddr).Port
654667
netdev += fmt.Sprintf(",hostfwd=tcp:127.0.0.1:%d-:%d",
655668
builder.requestedHostForwardPorts[i].HostPort,

0 commit comments

Comments
 (0)