Skip to content

fix(processtree): retain cached host process ancestry - #1027

Merged
matthyx merged 1 commit into
mainfrom
fix/host-process-tree-ancestry
Oct 12, 2026
Merged

matthyx merged 1 commit into
mainfrom
fix/host-process-tree-ancestry

Conversation

@matthyx

@matthyx matthyx commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Short-lived processes observed by the standalone host agent (KubernetesMode: false), such as systemd generators, systemctl and ps, can reach alert enrichment after exiting. Although the process tree records their executable path and arguments at exec, GetPidBranch("host", ...) fails because container registration identifies PID 1's parent (PID 0) as its shim. PID 0 has no process-tree node, so callers fall back to procfs after the executable and arguments are already unavailable.

Resolve host ancestry against PID 1 and return a cached branch including init and the triggering process. Container branches continue to exclude their shim. Missing roots, targets or parents still return errors, preserving existing caller fallbacks to procfs.

This fixes standalone host-agent mode. Kubernetes mode deliberately skips creating host nodes from procfs and can reparent host exec nodes to the registered shim; that existing policy and the caller's procfs fallback remain unchanged. Retaining complete host ancestry in Kubernetes mode would require a separate change to the process creator's caching/reparenting policy.

Regression tests cover PID 1, direct and nested host children, missing/outside ancestry, branch copy isolation, and exec-to-exit enrichment with a PID that cannot exist in procfs. The host regression fails with shim process 0 not found in process tree before the fix.

Validation

  • go test -mod=readonly ./pkg/processtree/... -count=1
  • go test -mod=readonly -race -tags stacktrace -p 1 ./pkg/processtree/... -count=1
  • go vet -mod=readonly ./pkg/processtree/...
  • golangci-lint run --new-from-rev=HEAD ./pkg/processtree/... (run before committing): no new issues. Full package lint reports seven existing findings in unchanged code.
  • make binary BINARY_NAME=/tmp/node-agent-host-process-tree
  • Independent code review: no actionable findings.

The private host agent must consume a release containing this upstream fix for deployment.

Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e1f31319-af96-4cc9-8987-6fde7bbf3fdf

📥 Commits

Reviewing files that changed from the base of the PR and between 92b29d2 and a0459b0.


📒 Files selected for processing (3)
  • pkg/processtree/container/container_processtree.go
  • pkg/processtree/container/container_processtree_test.go
  • pkg/processtree/process_tree_manager_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.



📝 Walkthrough
📝 Walkthrough

Walkthrough

Host process-tree branches now use PID 1 as the ancestry root and PID 0 as the boundary. Tests cover host ancestry branches, error cases, and exited-process lookups.

Changes

Host process tree

Layer / File(s) Summary
Host ancestry and process retention
pkg/processtree/container/container_processtree.go, pkg/processtree/container/container_processtree_test.go, pkg/processtree/process_tree_manager_test.go
GetPidBranch uses host PID 1 as the root and PID 0 as the boundary. Branch construction includes nodes with PPID zero. Tests cover ancestry, missing or out-of-subtree processes, and retention of an exited host process in cached and uncached tree lookups.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix


Merge Risk: ⚪ Minimal · up to a0459

Host process alert enrichment now keeps ancestry for short-lived processes that have already exited. The change is narrowly scoped and has regression tests, so no merge-blocking risk remains.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving cached host process ancestry for process-tree enrichment.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Default Kubernetes-mode host reparenting prevents the new PID 1 ancestry path from succeeding.

1 open finding
What changed in this PR

Updates host process-tree ancestry to include PID 1 and retain exited-process metadata.

Changes:

  • Uses host init as the ancestry root.
  • Includes root nodes with PPID 0 when building branches.
  • Adds host ancestry and exit-retention regression tests.
File Description
pkg/​processtree/​container/​container_processtree.go Adds host-specific root handling.
pkg/​processtree/​container/​container_processtree_test.go Tests host branches and missing ancestry.
pkg/​processtree/​process_tree_manager_test.go Tests exited host-process enrichment.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +75 to +77
if utils.IsHost(containerID) {
rootPID, rootKind = 1, "host init"
shimPID = 0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KubernetesMode=true behavior described here is accurate, but it is pre-existing and outside this fix’s scope. This PR fixes standalone host-agent mode (KubernetesMode=false), which is the configuration used by the host-agent deployment example and host-agent integration suite; the new manager regression exercises that mode. In Kubernetes mode, handleProcfsEvent deliberately refuses to create host nodes (including PID 1), and handleExecEvent retains its existing reparenting policy. Those paths were not changed by this PR. Missing cached ancestry still returns an error so the caller can use its existing procfs fallback. Seeding PID 1 and retaining complete host ancestry in Kubernetes mode would change that caching/reparenting policy and warrants a separate change. I clarified the standalone-mode scope in the PR description; no code change is needed for this finding.

@github-actions

Copy link
Copy Markdown

Performance Benchmark Results

Node-Agent Resource Usage
Metric BEFORE AFTER Delta
Avg CPU (cores) 0.159 0.171 +7.8%
Peak CPU (cores) 0.171 0.178 +4.2%
Peak CPU p95 (cores) 0.166 0.178 +6.8%
Avg Memory (MiB) 375.889 311.353 -17.2%
Peak Memory (MiB) 386.129 316.715 -18.0%
Dedup Effectiveness

No data available.

@matthyx matthyx added the release Create release label Oct 12, 2026
@matthyx
matthyx merged commit c7a7393 into main Oct 12, 2026
40 checks passed
@matthyx
matthyx deleted the fix/host-process-tree-ancestry branch October 12, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release Create release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants