Skip to content

Commit

Permalink
Run in a new process group when not using --foreground
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nirs committed Aug 31, 2024
1 parent 726f61d commit 47a3d7b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pkg/instance/ha_cmd_opts_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ import (
"syscall"
)

var SysProcAttr = &syscall.SysProcAttr{}
var (
ForegroundSysProcAttr = &syscall.SysProcAttr{}
BackgroundSysProcAttr = &syscall.SysProcAttr{Setpgid: true}
)
9 changes: 6 additions & 3 deletions pkg/instance/ha_cmd_opts_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
7 changes: 6 additions & 1 deletion pkg/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 47a3d7b

Please sign in to comment.