From 5b3128255809e0e0a1f138c20377d77361fa6ef6 Mon Sep 17 00:00:00 2001 From: Yves Chan Date: Thu, 17 Oct 2019 23:53:08 +0800 Subject: [PATCH] vc/qemu: add mutex to qmp monitor channel in qmpSetup() Solve possible race condition in qmpSetup() and qmpShutdown() Fixes: #2139 Signed-off-by: Yves Chan --- virtcontainers/qemu.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index 948feba80b..95b0b0fa07 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -19,6 +19,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "syscall" "time" "unsafe" @@ -48,6 +49,7 @@ const romFile = "" const defaultDisableModern = false type qmpChannel struct { + sync.Mutex ctx context.Context path string qmp *govmmQemu.QMP @@ -921,6 +923,9 @@ func (q *qemu) togglePauseSandbox(pause bool) error { } func (q *qemu) qmpSetup() error { + q.qmpMonitorCh.Lock() + defer q.qmpMonitorCh.Unlock() + if q.qmpMonitorCh.qmp != nil { return nil } @@ -949,6 +954,9 @@ func (q *qemu) qmpSetup() error { } func (q *qemu) qmpShutdown() { + q.qmpMonitorCh.Lock() + defer q.qmpMonitorCh.Unlock() + if q.qmpMonitorCh.qmp != nil { q.qmpMonitorCh.qmp.Shutdown() // wait on disconnected channel to be sure that the qmp channel has