talos/pkg/proc/reaper/reaper.go
Andrey Smirnov c0698c1815 chore(machined): implement process reaper for PID 1 machined process
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>
2019-09-05 10:01:02 -07:00

40 lines
976 B
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 reaper implements zombie process reaper with notifications.
package reaper
import "syscall"
// Run launches loop for the zombie process reaper.
func Run() {
hunter.Run()
}
// Shutdown stops the process reaper.
func Shutdown() {
hunter.Shutdown()
}
// ProcessInfo describes reaped zombie process.
type ProcessInfo struct {
Pid int
Status syscall.WaitStatus
}
// Notify causes reaper to deliver notifications about reaped zombies.
//
// If Notify returns false, reaper is not running, and Notify does nothing.
func Notify(ch chan<- ProcessInfo) bool {
return hunter.Notify(ch)
}
// Stop sending notifications to the channel.
func Stop(ch chan<- ProcessInfo) {
hunter.Stop(ch)
}
// Singleton instance of zombieHunter
var hunter = &zombieHunter{}