mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-20 14:11:11 +02:00
In UNIX, any zombies without parent process get re-parented to process with PID 1 (usually running init), and PID 1 process should take care of them (usually simply clean them up). Cleaning up zombies is important, as they still take kerner resources, and having enormous amount of zombie processes signifcantly degrades system performance. For Talos, PID 1 process is machined, and machined itself forks to run other processes in process runner and `pkg/cmd` one-time commands. Naive solution of running `wait()` loop doesn't work as it might race with `Process.Wait()` and clean up zombie which wasn't re-parented which leads to process execution false failure. After considering other solutions, we decided to go with the simple approach: machined runs global zombie process reaper which publishes information about reaped zombies. Any call to `Process.Wait()` (or `Command.Wait()` which calls it) should be replaced with listening to reaper's channel for notifications to catch info about the process which was created in this call. There are several changes in this PR: 1. Reaper implementation itself, started from machined. 2. Process runner and `pkg/cmd` can either use regular `Command.Wait()` or use reaper notifications depending on reaper status (running/not running). This allows using this code outside of machined. 3. Small bug fixes with process log which was affecting the tests. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"os/exec"
|
|
|
|
"github.com/armon/circbuf"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/talos-systems/talos/pkg/proc/reaper"
|
|
)
|
|
|
|
// MaxStderrLen is maximum length of stderr output captured for error message
|
|
const MaxStderrLen = 4096
|
|
|
|
// Run executes a command.
|
|
func Run(name string, args ...string) error {
|
|
cmd := exec.Command(name, args...)
|
|
|
|
stderr, err := circbuf.NewBuffer(MaxStderrLen)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmd.Stderr = stderr
|
|
|
|
notifyCh := make(chan reaper.ProcessInfo, 8)
|
|
usingReaper := reaper.Notify(notifyCh)
|
|
if usingReaper {
|
|
defer reaper.Stop(notifyCh)
|
|
}
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
return errors.Errorf("%s: %s", err, stderr.String())
|
|
}
|
|
|
|
if err = reaper.WaitWrapper(usingReaper, notifyCh, cmd); err != nil {
|
|
return errors.Errorf("%s: %s", err, stderr.String())
|
|
}
|
|
|
|
return nil
|
|
}
|