Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
qemu: wait on disconnected channel in qmp shutdown
Browse files Browse the repository at this point in the history
That is how govmm ensures us that the qmp channel has been cleaned
up entirely.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Jul 31, 2018
1 parent c8b4fab commit 44a3a44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 12 additions & 4 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
)

type qmpChannel struct {
ctx context.Context
path string
qmp *govmmQemu.QMP
ctx context.Context
path string
qmp *govmmQemu.QMP
disconn chan struct{}
}

// CPUDevice represents a CPU device which was hot-added in a running VM
Expand Down Expand Up @@ -514,14 +515,15 @@ func (q *qemu) waitSandbox(timeout int) error {
cfg := govmmQemu.QMPConfig{Logger: newQMPLogger()}

var qmp *govmmQemu.QMP
var disconnectCh chan struct{}
var ver *govmmQemu.QMPVersion
var err error

// clear any possible old state before trying to connect again.
q.qmpShutdown()
timeStart := time.Now()
for {
disconnectCh := make(chan struct{})
disconnectCh = make(chan struct{})
qmp, ver, err = govmmQemu.QMPStart(q.qmpMonitorCh.ctx, q.qmpMonitorCh.path, cfg, disconnectCh)
if err == nil {
break
Expand All @@ -534,6 +536,7 @@ func (q *qemu) waitSandbox(timeout int) error {
time.Sleep(time.Duration(50) * time.Millisecond)
}
q.qmpMonitorCh.qmp = qmp
q.qmpMonitorCh.disconn = disconnectCh
defer q.qmpShutdown()

qemuMajorVersion = ver.Major
Expand Down Expand Up @@ -619,14 +622,19 @@ func (q *qemu) qmpSetup() error {
return err
}
q.qmpMonitorCh.qmp = qmp
q.qmpMonitorCh.disconn = disconnectCh

return nil
}

func (q *qemu) qmpShutdown() {
if q.qmpMonitorCh.qmp != nil {
q.qmpMonitorCh.qmp.Shutdown()
// wait on disconnected channel to be sure that the qmp channel has
// been closed cleanly.
<-q.qmpMonitorCh.disconn
q.qmpMonitorCh.qmp = nil
q.qmpMonitorCh.disconn = nil
}
}

Expand Down
15 changes: 15 additions & 0 deletions virtcontainers/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,18 @@ func TestHotplugUnsupportedDeviceType(t *testing.T) {
_, err = q.hotplugRemoveDevice(&memoryDevice{0, 128}, fsDev)
assert.Error(err)
}

func TestQMPSetupShutdown(t *testing.T) {
assert := assert.New(t)

qemuConfig := newQemuConfig()
q := &qemu{
config: qemuConfig,
}

q.qmpShutdown()

q.qmpMonitorCh.qmp = &govmmQemu.QMP{}
err := q.qmpSetup()
assert.Nil(err)
}

0 comments on commit 44a3a44

Please sign in to comment.