Skip to content

Commit

Permalink
rdctl: *nix: Do not wait for main process to exit before killing it
Browse files Browse the repository at this point in the history
When doing a factory reset, do no wait for the main process to exit
gracefully before terminating it, as that would not normally happen.
Instead, just kill its process group (including the main process itself)
without waiting.

Signed-off-by: Mark Yen <mark.yen@suse.com>
  • Loading branch information
mook-as committed Dec 16, 2024
1 parent c432955 commit 638cdc1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/go/rdctl/cmd/internalProcessWaitKill.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exit, and once it does, terminates all processes within the same process group.`
if err != nil {
return fmt.Errorf("failed to get process ID: %w", err)
}
return process.WaitForProcessAndKillGroup(pid)
return process.KillProcessGroup(pid, true)
},
}

Expand Down
13 changes: 7 additions & 6 deletions src/go/rdctl/pkg/process/process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,20 @@ func FindPidOfProcess(executable string) (int, error) {
return mainPid, nil
}

// Wait for the process identified by the given pid to exit, then kill all
// processes in the same process group. This blocks until the given process
// exits. If the given pid is 0, this is a no-op.
func WaitForProcessAndKillGroup(pid int) error {
// Kill the process group the given process belongs to. If wait is set, block
// until the target process exits first before doing so.
func KillProcessGroup(pid int, wait bool) error {
if pid == 0 {
return nil
}
pgid, err := unix.Getpgid(pid)
if err != nil {
return fmt.Errorf("failed to get process group id for %d: %w", pid, err)
}
if err = WaitForProcess(pid); err != nil {
return fmt.Errorf("failed to wait for process: %w", err)
if wait {
if err = WaitForProcess(pid); err != nil {
return fmt.Errorf("failed to wait for process: %w", err)
}
}
err = unix.Kill(-pgid, unix.SIGTERM)
if err != nil && !errors.Is(err, unix.ESRCH) {
Expand Down
9 changes: 4 additions & 5 deletions src/go/rdctl/pkg/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,10 @@ func FindPidOfProcess(executable string) (int, error) {
return mainPid, nil
}

// Wait for the process identified by the given pid to exit, then kill all
// processes in the same process group. This blocks until the given process
// exits.
func WaitForProcessAndKillGroup(pid int) error {
return errors.New("WaitForProcessAndKillGroup is not implemented on Windows")
// Kill the process group the given process belongs to. If wait is set, block
// until the target process exits first before doing so.
func KillProcessGroup(pid int, wait bool) error {
return errors.New("KillProcessGroup is not implemented on Windows")
}

// TerminateProcessInDirectory terminates all processes where the executable
Expand Down
2 changes: 1 addition & 1 deletion src/go/rdctl/pkg/shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func terminateRancherDesktopFunc(appDir string) func(context.Context) error {
if err != nil {
return err
}
return process.WaitForProcessAndKillGroup(pid)
return process.KillProcessGroup(pid, false)
})())

errors = multierror.Append(errors, process.TerminateProcessInDirectory(appDir, true))
Expand Down

0 comments on commit 638cdc1

Please sign in to comment.