-
Notifications
You must be signed in to change notification settings - Fork 8
fix(instances): reconcile orphaned runtime processes #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hiroTamada
wants to merge
1
commit into
main
Choose a base branch
from
fix/orphan-runtime-reconciler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package instances | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/kernel/hypeman/lib/logger" | ||
| ) | ||
|
|
||
| const ( | ||
| runtimeOrphanGCInterval = 60 * time.Second | ||
| runtimeOrphanMinAge = 5 * time.Minute | ||
| ) | ||
|
|
||
| type orphanRuntimeProcess struct { | ||
| PID int | ||
| InstanceID string | ||
| Age time.Duration | ||
| Command string | ||
| } | ||
|
|
||
| // StartRuntimeOrphanReconciler adopts or removes hypervisor runtimes left behind | ||
| // after hypeman-api restarts. This protects hosts running systemd KillMode=process, | ||
| // where qemu/firecracker children can survive the API process and become PPID=1. | ||
| func (m *manager) StartRuntimeOrphanReconciler(ctx context.Context) { | ||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
| m.runtimeOrphanGCOnce.Do(func() { | ||
| go func() { | ||
| m.reconcileRuntimeOrphans(ctx) | ||
| ticker := time.NewTicker(runtimeOrphanGCInterval) | ||
| defer ticker.Stop() | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-ticker.C: | ||
| m.reconcileRuntimeOrphans(ctx) | ||
| } | ||
| } | ||
| }() | ||
| }) | ||
| } | ||
|
|
||
| func (m *manager) reconcileRuntimeOrphans(ctx context.Context) { | ||
| log := logger.FromContext(ctx) | ||
|
|
||
| orphaned, err := scanOrphanRuntimeProcesses(m.paths.GuestsDir()) | ||
| if err != nil { | ||
| log.WarnContext(ctx, "runtime orphan GC: failed to scan processes", "error", err) | ||
| return | ||
| } | ||
| for _, proc := range orphaned { | ||
| meta, err := m.loadMetadata(proc.InstanceID) | ||
| if err == nil { | ||
| if meta.HypervisorPID == nil || *meta.HypervisorPID != proc.PID { | ||
| pid := proc.PID | ||
| meta.HypervisorPID = &pid | ||
| if saveErr := m.saveMetadata(meta); saveErr != nil { | ||
| log.WarnContext(ctx, "runtime orphan GC: failed to adopt runtime process", | ||
| "instance_id", proc.InstanceID, | ||
| "pid", proc.PID, | ||
| "error", saveErr, | ||
| ) | ||
| continue | ||
| } | ||
| log.InfoContext(ctx, "runtime orphan GC: adopted runtime process", | ||
| "instance_id", proc.InstanceID, | ||
| "pid", proc.PID, | ||
| ) | ||
| } | ||
| continue | ||
| } | ||
| if !errors.Is(err, ErrNotFound) { | ||
| log.WarnContext(ctx, "runtime orphan GC: failed to load metadata", | ||
| "instance_id", proc.InstanceID, | ||
| "pid", proc.PID, | ||
| "error", err, | ||
| ) | ||
| continue | ||
| } | ||
| if proc.Age < runtimeOrphanMinAge { | ||
| continue | ||
| } | ||
| if err := terminateRuntimeProcess(proc.PID); err != nil { | ||
| log.WarnContext(ctx, "runtime orphan GC: failed to terminate unowned runtime process", | ||
| "instance_id", proc.InstanceID, | ||
| "pid", proc.PID, | ||
| "age", proc.Age, | ||
| "error", err, | ||
| ) | ||
| continue | ||
| } | ||
| log.InfoContext(ctx, "runtime orphan GC: terminated unowned runtime process", | ||
| "instance_id", proc.InstanceID, | ||
| "pid", proc.PID, | ||
| "age", proc.Age, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| //go:build linux | ||
|
|
||
| package instances | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
| ) | ||
|
|
||
| func scanOrphanRuntimeProcesses(guestsDir string) ([]orphanRuntimeProcess, error) { | ||
| entries, err := os.ReadDir("/proc") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| bootTime, _ := linuxBootTime() | ||
| now := time.Now() | ||
| var out []orphanRuntimeProcess | ||
| for _, entry := range entries { | ||
| if !entry.IsDir() { | ||
| continue | ||
| } | ||
| pid, err := strconv.Atoi(entry.Name()) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| comm, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "comm")) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| name := strings.TrimSpace(string(comm)) | ||
| if name != "qemu-system-x86" && name != "firecracker" { | ||
| continue | ||
| } | ||
| ppid, startTime, err := readProcStatusAndStart(pid) | ||
| if err != nil || ppid != 1 { | ||
| continue | ||
| } | ||
| cmdlineBytes, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline")) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| cmdline := string(bytes.ReplaceAll(cmdlineBytes, []byte{0}, []byte{' '})) | ||
| instanceID := instanceIDFromRuntimeCmdline(guestsDir, cmdline) | ||
| if instanceID == "" { | ||
| continue | ||
| } | ||
| age := time.Duration(0) | ||
| if !bootTime.IsZero() && startTime > 0 { | ||
| age = now.Sub(bootTime.Add(startTime)) | ||
| } | ||
| out = append(out, orphanRuntimeProcess{ | ||
| PID: pid, | ||
| InstanceID: instanceID, | ||
| Age: age, | ||
| Command: cmdline, | ||
| }) | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| func instanceIDFromRuntimeCmdline(guestsDir, cmdline string) string { | ||
| prefix := filepath.Clean(guestsDir) + string(filepath.Separator) | ||
| idx := strings.Index(cmdline, prefix) | ||
| if idx < 0 { | ||
| return "" | ||
| } | ||
| rest := cmdline[idx+len(prefix):] | ||
| end := strings.IndexAny(rest, string(filepath.Separator)+" ") | ||
| if end < 0 { | ||
| return rest | ||
| } | ||
| return rest[:end] | ||
| } | ||
|
|
||
| func readProcStatusAndStart(pid int) (ppid int, start time.Duration, err error) { | ||
| status, err := os.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "status")) | ||
| if err != nil { | ||
| return 0, 0, err | ||
| } | ||
| for _, line := range strings.Split(string(status), "\n") { | ||
| if strings.HasPrefix(line, "PPid:") { | ||
| fields := strings.Fields(line) | ||
| if len(fields) == 2 { | ||
| ppid, _ = strconv.Atoi(fields[1]) | ||
| } | ||
| break | ||
| } | ||
| } | ||
| stat, err := os.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "stat")) | ||
| if err != nil { | ||
| return ppid, 0, nil | ||
| } | ||
| fields := strings.Fields(string(stat)) | ||
| if len(fields) > 21 { | ||
| ticks, _ := strconv.ParseInt(fields[21], 10, 64) | ||
| hz := int64(100) | ||
| start = time.Duration(ticks) * time.Second / time.Duration(hz) | ||
| } | ||
| return ppid, start, nil | ||
| } | ||
|
|
||
| func linuxBootTime() (time.Time, error) { | ||
| data, err := os.ReadFile("/proc/stat") | ||
| if err != nil { | ||
| return time.Time{}, err | ||
| } | ||
| for _, line := range strings.Split(string(data), "\n") { | ||
| if !strings.HasPrefix(line, "btime ") { | ||
| continue | ||
| } | ||
| fields := strings.Fields(line) | ||
| if len(fields) != 2 { | ||
| break | ||
| } | ||
| sec, err := strconv.ParseInt(fields[1], 10, 64) | ||
| if err != nil { | ||
| return time.Time{}, err | ||
| } | ||
| return time.Unix(sec, 0), nil | ||
| } | ||
| return time.Time{}, fmt.Errorf("btime not found") | ||
| } | ||
|
|
||
| func terminateRuntimeProcess(pid int) error { | ||
| if err := syscall.Kill(pid, syscall.SIGTERM); err != nil { | ||
| if err == syscall.ESRCH { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
| deadline := time.Now().Add(10 * time.Second) | ||
| for time.Now().Before(deadline) { | ||
| if err := syscall.Kill(pid, 0); err != nil { | ||
| if err == syscall.ESRCH { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
| time.Sleep(100 * time.Millisecond) | ||
| } | ||
| if err := syscall.Kill(pid, syscall.SIGKILL); err != nil && err != syscall.ESRCH { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //go:build linux | ||
|
|
||
| package instances | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestInstanceIDFromRuntimeCmdline(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| require.Equal(t, | ||
| "abc123", | ||
| instanceIDFromRuntimeCmdline("/var/lib/hypeman/guests", "/usr/bin/qemu-system-x86_64 -chardev socket,path=/var/lib/hypeman/guests/abc123/qemu.sock"), | ||
| ) | ||
| require.Equal(t, | ||
| "fc456", | ||
| instanceIDFromRuntimeCmdline("/var/lib/hypeman/guests", "/var/lib/hypeman/system/binaries/firecracker --api-sock /var/lib/hypeman/guests/fc456/fc.sock"), | ||
| ) | ||
| require.Empty(t, | ||
| instanceIDFromRuntimeCmdline("/var/lib/hypeman/guests", "/usr/bin/qemu-system-x86_64 -monitor /tmp/qemu.sock"), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //go:build !linux | ||
|
|
||
| package instances | ||
|
|
||
| func scanOrphanRuntimeProcesses(string) ([]orphanRuntimeProcess, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func terminateRuntimeProcess(int) error { | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QEMU comm name exceeds TASK_COMM_LEN, never matches
High Severity
The filter checks
name != "qemu-system-x86"(16 characters), but Linux'sTASK_COMM_LENis 16 bytes including the null terminator, so/proc/PID/commtruncates process names to 15 characters. The actual QEMU binary isqemu-system-x86_64(perqemuBinaryName()inprocess.go), which the kernel truncates to"qemu-system-x8"incomm. Since"qemu-system-x86"is 16 chars and can never appear in/proc/PID/comm, the reconciler silently skips all orphaned QEMU processes — it can neither adopt nor terminate them.Reviewed by Cursor Bugbot for commit 11739f4. Configure here.