Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 68a7478

Browse files
authored
Merge pull request #515 from laijs/watchHyperstart-cancellable
make watchHyperstart() cancellable
2 parents 29e043d + 3c792ae commit 68a7478

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

hypervisor/context.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ type VmContext struct {
3333
PauseState int
3434
Boot *BootConfig
3535

36-
vmHyperstartAPIVersion uint32
37-
3836
// Communication Context
3937
Hub chan VmEvent
4038
client chan *types.VmResponse
@@ -68,6 +66,8 @@ type VmContext struct {
6866
current string
6967
timer *time.Timer
7068

69+
cancelWatchHyperstart chan struct{}
70+
7171
logPrefix string
7272

7373
lock sync.RWMutex //protect update of context
@@ -147,6 +147,8 @@ func InitContext(id string, hub chan VmEvent, client chan *types.VmResponse, dc
147147
networks: NewNetworkContext(),
148148
vmExec: make(map[string]*hyperstartapi.ExecCommand),
149149
logPrefix: fmt.Sprintf("SB[%s] ", id),
150+
151+
cancelWatchHyperstart: make(chan struct{}),
150152
}
151153
ctx.networks.sandbox = ctx
152154

@@ -241,10 +243,14 @@ func (ctx *VmContext) Close() {
241243
ctx.Log(INFO, "VmContext Close()")
242244
ctx.lock.Lock()
243245
defer ctx.lock.Unlock()
246+
select {
247+
case ctx.cancelWatchHyperstart <- struct{}{}:
248+
default:
249+
}
250+
ctx.hyperstart.Close()
244251
ctx.unsetTimeout()
245252
ctx.networks.close()
246253
ctx.DCtx.Close()
247-
ctx.hyperstart.Close()
248254
close(ctx.client)
249255
os.Remove(ctx.ShareDir)
250256
ctx.handler = nil

hypervisor/hypervisor.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ func (ctx *VmContext) loop() {
3232
}
3333

3434
func (ctx *VmContext) watchHyperstart() {
35+
next := time.NewTimer(10 * time.Second)
3536
timeout := time.AfterFunc(60*time.Second, func() {
36-
if ctx.PauseState == PauseStateUnpaused {
37-
ctx.Log(ERROR, "watch hyperstart timeout")
38-
ctx.Hub <- &InitFailedEvent{Reason: "watch hyperstart timeout"}
39-
ctx.hyperstart.Close()
40-
}
37+
ctx.Log(ERROR, "watch hyperstart timeout")
38+
ctx.Hub <- &InitFailedEvent{Reason: "watch hyperstart timeout"}
39+
ctx.hyperstart.Close()
4140
})
4241
ctx.Log(DEBUG, "watch hyperstart")
42+
loop:
4343
for {
4444
ctx.Log(TRACE, "issue VERSION request for keep-alive test")
4545
_, err := ctx.hyperstart.APIVersion()
@@ -52,9 +52,15 @@ func (ctx *VmContext) watchHyperstart() {
5252
if !timeout.Stop() {
5353
<-timeout.C
5454
}
55-
time.Sleep(10 * time.Second)
55+
select {
56+
case <-ctx.cancelWatchHyperstart:
57+
break loop
58+
case <-next.C:
59+
}
60+
next.Reset(10 * time.Second)
5661
timeout.Reset(60 * time.Second)
5762
}
63+
next.Stop()
5864
timeout.Stop()
5965
}
6066

@@ -107,7 +113,9 @@ func VmAssociate(vmId string, hub chan VmEvent, client chan *types.VmResponse, p
107113

108114
context.Become(stateRunning, StateRunning)
109115

110-
go context.watchHyperstart()
116+
if !paused {
117+
go context.watchHyperstart()
118+
}
111119
go context.loop()
112120
return context, nil
113121
}

hypervisor/vm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ func (vm *Vm) Pause(pause bool) error {
652652
if ctx.PauseState != pauseState {
653653
/* FIXME: only support pause whole vm now */
654654
if pause {
655+
ctx.cancelWatchHyperstart <- struct{}{}
655656
err = ctx.hyperstart.PauseSync()
656657
}
657658
if err != nil {
@@ -668,6 +669,7 @@ func (vm *Vm) Pause(pause bool) error {
668669

669670
if !pause {
670671
err = ctx.hyperstart.Unpause()
672+
go ctx.watchHyperstart()
671673
}
672674
if err != nil {
673675
vm.Log(ERROR, "%s sandbox failed: %v", command, err)

0 commit comments

Comments
 (0)