From 2631b08ff109c433ea79bd8122e31cb0200ca483 Mon Sep 17 00:00:00 2001 From: Alexandru Matei Date: Thu, 10 Nov 2022 14:06:13 +0200 Subject: [PATCH] clh: don't try to stop clh multiple times Avoid executing StopVM concurrently when virtiofs dies as a result of clh being stopped in StopVM. Fixes: #5622 Signed-off-by: Alexandru Matei --- src/runtime/virtcontainers/clh.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/runtime/virtcontainers/clh.go b/src/runtime/virtcontainers/clh.go index a0f08e8cb7..4ea297cbdc 100644 --- a/src/runtime/virtcontainers/clh.go +++ b/src/runtime/virtcontainers/clh.go @@ -24,6 +24,8 @@ import ( "regexp" "strconv" "strings" + "sync" + "sync/atomic" "syscall" "time" @@ -256,6 +258,8 @@ type cloudHypervisor struct { vmconfig chclient.VmConfig state CloudHypervisorState config HypervisorConfig + stopped int32 + mu sync.Mutex } var clhKernelParams = []Param{ @@ -1081,9 +1085,21 @@ func (clh *cloudHypervisor) ResumeVM(ctx context.Context) error { // StopVM will stop the Sandbox's VM. func (clh *cloudHypervisor) StopVM(ctx context.Context, waitOnly bool) (err error) { + clh.mu.Lock() + defer func() { + if err == nil { + atomic.StoreInt32(&clh.stopped, 1) + } + clh.mu.Unlock() + }() span, _ := katatrace.Trace(ctx, clh.Logger(), "StopVM", clhTracingTags, map[string]string{"sandbox_id": clh.id}) defer span.End() clh.Logger().WithField("function", "StopVM").Info("Stop Sandbox") + if atomic.LoadInt32(&clh.stopped) != 0 { + clh.Logger().Info("Already stopped") + return nil + } + return clh.terminate(ctx, waitOnly) }