Skip to content

Commit 1e49227

Browse files
committed
Fix VMM shutdown handler to always terminate shim process
When handleVMMShutdown is called and the VM is already stopped (CanStop() returns false), the handler previously did nothing to terminate the shim process, leaving it orphaned. Now we always call the context cancel function after attempting vm.Stop(), ensuring the process exits through the existing graceful shutdown flow regardless of VM state.
1 parent ced53bc commit 1e49227

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

cmd/vz-shim/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
slog.Info("VM started", "vcpus", config.VCPUs, "memory_mb", config.MemoryBytes/1024/1024)
6262

6363
// Create the shim server
64-
server := NewShimServer(vm, vmConfig)
64+
server := NewShimServer(vm, vmConfig, cancel)
6565

6666
// Start control socket listener (remove stale socket from previous run)
6767
os.Remove(config.ControlSocket)

cmd/vz-shim/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44

55
import (
66
"bufio"
7+
"context"
78
"encoding/json"
89
"fmt"
910
"io"
@@ -19,14 +20,16 @@ import (
1920
type ShimServer struct {
2021
vm *vz.VirtualMachine
2122
vmConfig *vz.VirtualMachineConfiguration
23+
cancel context.CancelFunc
2224
mu sync.RWMutex
2325
}
2426

2527
// NewShimServer creates a new shim server.
26-
func NewShimServer(vm *vz.VirtualMachine, vmConfig *vz.VirtualMachineConfiguration) *ShimServer {
28+
func NewShimServer(vm *vz.VirtualMachine, vmConfig *vz.VirtualMachineConfiguration, cancel context.CancelFunc) *ShimServer {
2729
return &ShimServer{
2830
vm: vm,
2931
vmConfig: vmConfig,
32+
cancel: cancel,
3033
}
3134
}
3235

@@ -151,7 +154,9 @@ func (s *ShimServer) handleVMMShutdown(w http.ResponseWriter, r *http.Request) {
151154
if s.vm.CanStop() {
152155
s.vm.Stop()
153156
}
154-
// Process will exit when VM stops (monitored in main)
157+
// Ensure process terminates even if VM is already stopped
158+
// (e.g. CanStop() returned false because the VM already stopped).
159+
s.cancel()
155160
}()
156161
}
157162

0 commit comments

Comments
 (0)