Skip to content

Commit 26873ec

Browse files
committed
Adds timeout when waiting for IP and stopped
Signed-off-by: Zhongcheng Lao <[email protected]>
1 parent 8fadcca commit 26873ec

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

drivers/hyperv/hyperv.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hyperv
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"net"
78
"os"
@@ -42,6 +43,7 @@ const (
4243
defaultVLanID = 0
4344
defaultDisableDynamicMemory = false
4445
defaultSwitchID = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444"
46+
defaultTimeout = time.Minute * 10
4547
)
4648

4749
// NewDriver creates a new Hyper-v driver with default settings.
@@ -340,12 +342,17 @@ func (d *Driver) chooseVirtualSwitch() (string, error) {
340342
func (d *Driver) waitForIP() (string, error) {
341343
log.Infof("Waiting for host to start...")
342344

345+
start := time.Now()
343346
for {
344347
ip, _ := d.GetIP()
345348
if ip != "" {
346349
return ip, nil
347350
}
348351

352+
if time.Since(start) >= defaultTimeout {
353+
return "", errors.New("timeout waiting for IP")
354+
}
355+
349356
time.Sleep(1 * time.Second)
350357
}
351358
}
@@ -354,6 +361,7 @@ func (d *Driver) waitForIP() (string, error) {
354361
func (d *Driver) waitStopped() error {
355362
log.Infof("Waiting for host to stop...")
356363

364+
start := time.Now()
357365
for {
358366
s, err := d.GetState()
359367
if err != nil {
@@ -364,6 +372,10 @@ func (d *Driver) waitStopped() error {
364372
return nil
365373
}
366374

375+
if time.Since(start) >= defaultTimeout {
376+
return errors.New("timeout waiting for stopped")
377+
}
378+
367379
time.Sleep(1 * time.Second)
368380
}
369381
}

0 commit comments

Comments
 (0)