Skip to content

Commit

Permalink
Merge pull request rancher-sandbox#8009 from mook-as/linux/process-gr…
Browse files Browse the repository at this point in the history
…oups

Linux: renable use of process groups
  • Loading branch information
jandubois authored Jan 22, 2025
2 parents e61181d + 4f391f6 commit 55fc4da
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
10 changes: 10 additions & 0 deletions background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ let pendingRestartContext: CommandWorkerInterface.CommandContext | undefined;
let httpCommandServer: HttpCommandServer|null = null;
const httpCredentialHelperServer = new HttpCredentialHelperServer();

if (process.platform === 'linux') {
// On Linux, put Electron into a new process group so that we can more
// reliably kill processes we spawn from extensions.
try {
require('posix-node').setpgid(0, 0);
} catch (ex) {
console.error(`Ignoring error setting process group: ${ ex }`);
}
}

// Scheme must be registered before the app is ready
Electron.protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } },
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@
},
"optionalDependencies": {
"dmg-license": "1.0.11",
"fs-xattr": "0.3.1"
"fs-xattr": "0.3.1",
"posix-node": "0.12.0"
},
"browserslist": [
"node 18",
Expand Down
7 changes: 0 additions & 7 deletions src/go/rdctl/cmd/internalProcessWaitKill.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package cmd

import (
"fmt"
"runtime"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/process"
"github.com/spf13/cobra"
Expand All @@ -40,12 +39,6 @@ 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)
}
if runtime.GOOS == "linux" {
// TODO: We can't use the process group on Linux, because Electron does
// not always create a new one. But for now still wait for the
// process to exit
return process.WaitForProcess(pid)
}
return process.KillProcessGroup(pid, true)
},
}
Expand Down
11 changes: 10 additions & 1 deletion src/go/rdctl/pkg/process/process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -90,7 +91,8 @@ func FindPidOfProcess(executable string) (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.
// until the target process exits first before doing so. On Linux, the process
// group is only killed if the given pid is its own process group leader.
func KillProcessGroup(pid int, wait bool) error {
if pid == 0 {
return nil
Expand All @@ -104,6 +106,13 @@ func KillProcessGroup(pid int, wait bool) error {
return fmt.Errorf("failed to wait for process: %w", err)
}
}
if runtime.GOOS == "linux" && pid != pgid {
// On Linux, do not kill the process group if the pid is not the same as
// the process group id; this can happen when running from rpm/deb
// packaged builds (in which case killing the process group ends up
// killing the whole X11 session).
return nil
}
err = unix.Kill(-pgid, unix.SIGTERM)
if err != nil && !errors.Is(err, unix.ESRCH) {
return fmt.Errorf("failed to send SIGTERM: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion src/go/rdctl/pkg/process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ func FindPidOfProcess(executable string) (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.
// until the target process exits first before doing so. On Linux, the process
// group is only killed if the given pid is its own process group leader.
func KillProcessGroup(pid int, wait bool) error {
return errors.New("KillProcessGroup is not implemented on Windows")
}
Expand Down
26 changes: 11 additions & 15 deletions src/go/rdctl/pkg/shutdown/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,17 @@ func terminateRancherDesktopFunc(appDir string) func(context.Context) error {
return func(ctx context.Context) error {
var errors *multierror.Error

// TODO: We can't use the process group on Linux, because Electron does
// not always create a new one.
if runtime.GOOS != "linux" {
errors = multierror.Append(errors, (func() error {
mainExe, err := p.GetMainExecutable(ctx)
if err != nil {
return err
}
pid, err := process.FindPidOfProcess(mainExe)
if err != nil {
return err
}
return process.KillProcessGroup(pid, false)
})())
}
errors = multierror.Append(errors, (func() error {
mainExe, err := p.GetMainExecutable(ctx)
if err != nil {
return err
}
pid, err := process.FindPidOfProcess(mainExe)
if err != nil {
return err
}
return process.KillProcessGroup(pid, false)
})())

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

Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11101,6 +11101,13 @@ portfinder@^1.0.26:
debug "^3.2.7"
mkdirp "^0.5.6"

[email protected]:
version "0.12.0"
resolved "https://registry.yarnpkg.com/posix-node/-/posix-node-0.12.0.tgz#318f9de664cd532fe19d4d73c364077a7eaeb906"
integrity sha512-/CUfwwGM0lHcSaU3w/Sh8gQTFAzQ4ef1GUvOIfgcnvI0aWLp3NJpHhD14scbtRjQL6KOCqdaMgYdAzHyjImAig==
dependencies:
debug "^4.3.4"

possible-typed-array-names@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz#89bb63c6fada2c3e90adc4a647beeeb39cc7bf8f"
Expand Down

0 comments on commit 55fc4da

Please sign in to comment.