From 47a3d7b9e771bc2b6aae8694852c0b414a160bde Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Sat, 31 Aug 2024 04:12:20 +0300 Subject: [PATCH] Run in a new process group when not using --foreground Previously the hostagenet process was running in the foreground even when not using the --foreground option. It was using the same pgid of limactl process. If the guest was running, but limactl was interrupted the hostagent received the signal and was killed, stopping the VM. A simple way to fix this issue is to start the hostagent process in a new process group. This way it will be killed sending signals to the limactl process group. It looks like this is already implemented for Windows based on the docs for syscall.CREATE_NEW_PROCESS_GROUP[1]. Example run with this change: % ps -o pid,pgid,ppid,command PID PGID PPID COMMAND 39442 39442 39440 -zsh 63233 63233 39442 _output/bin/limactl start --vm-type vz --tty=false 63299 63299 63233 /Users/nsoffer/src/lima/_output/bin/limactl hostagent ... We can improve this later by adding an option to daemonize the hostagent process (like qemu --daemonize). [1] https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags Fixes #2573 Signed-off-by: Nir Soffer --- pkg/instance/ha_cmd_opts_others.go | 5 ++++- pkg/instance/ha_cmd_opts_windows.go | 9 ++++++--- pkg/instance/start.go | 7 ++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/instance/ha_cmd_opts_others.go b/pkg/instance/ha_cmd_opts_others.go index 1cd5f8a8d29..56ec240b159 100644 --- a/pkg/instance/ha_cmd_opts_others.go +++ b/pkg/instance/ha_cmd_opts_others.go @@ -6,4 +6,7 @@ import ( "syscall" ) -var SysProcAttr = &syscall.SysProcAttr{} +var ( + ForegroundSysProcAttr = &syscall.SysProcAttr{} + BackgroundSysProcAttr = &syscall.SysProcAttr{Setpgid: true} +) diff --git a/pkg/instance/ha_cmd_opts_windows.go b/pkg/instance/ha_cmd_opts_windows.go index 601626308c8..6b7c97adbfb 100644 --- a/pkg/instance/ha_cmd_opts_windows.go +++ b/pkg/instance/ha_cmd_opts_windows.go @@ -4,6 +4,9 @@ import ( "syscall" ) -var SysProcAttr = &syscall.SysProcAttr{ - CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP, -} +var ( + ForegroundSysProcAttr = &syscall.SysProcAttr{} + BackgroundSysProcAttr = &syscall.SysProcAttr{ + CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP, + } +) diff --git a/pkg/instance/start.go b/pkg/instance/start.go index 67e8665c6d1..00dd9ede04e 100644 --- a/pkg/instance/start.go +++ b/pkg/instance/start.go @@ -189,7 +189,12 @@ func Start(ctx context.Context, inst *store.Instance, launchHostAgentForeground } args = append(args, inst.Name) haCmd := exec.CommandContext(ctx, self, args...) - haCmd.SysProcAttr = SysProcAttr + + if launchHostAgentForeground { + haCmd.SysProcAttr = ForegroundSysProcAttr + } else { + haCmd.SysProcAttr = BackgroundSysProcAttr + } haCmd.Stdout = haStdoutW haCmd.Stderr = haStderrW