Skip to content

fix(processtree): drop cached branch when a tree refresh fails - #1047

Merged
matthyx merged 1 commit into
kubescape:feat/host-profile-gapsfrom
DeathGun44:fix/processtree-cache-stale-branch
Oct 24, 2026
Merged

matthyx merged 1 commit into
kubescape:feat/host-profile-gapsfrom
DeathGun44:fix/processtree-cache-stale-branch

Conversation

@DeathGun44

Copy link
Copy Markdown
Contributor

Overview

Targets #1043's branch, as discussed there.

When a tree refresh fails, GetContainerProcessTree kept the old cache entry, so a later cached read for a recycled PID returned the dead process's branch. #1043's comm check misses this when both processes have the same comm, like two python3 workers.

Fix: drop the cache entry when the lookup fails (one line). The new test TestManager_GetContainerProcessTree_FailedRefreshDropsCachedBranch fails on 4f207ac and passes with the fix.

The case is narrow: it needs PID reuse within the 1 min cache TTL and a parent the tree hasn't seen yet.

How to Test

go test -race -count=10 ./pkg/processtree/...

A failed refresh left the previous entry in the tree cache, so a later
cached read for a recycled pid returned the dead process's branch.

Signed-off-by: DeathGun44 <krishnamewara841@gmail.com>
@coderabbitai

coderabbitai Bot commented Oct 23, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d317325e-f085-43f5-86be-4022037c8bda

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • 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.

@matthyx matthyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: approve — the fix is needed, correct, and ready to merge into its current target. No blocking findings.

Reviewed node-agent#1047, head e3f05c402c65b52ad20343bb16edf81ecd033c2e, against feat/host-profile-gaps at 4f207ac6f20b9e814db815e3edf755c0726ae307.

The failure is real on that target: a process-event refresh can fail after same-command PID reuse with unknown replacement ancestry, leaving the predecessor's branch available to subsequent cached enrichment. pkg/containerwatcher/v2/event_enricher.go:34–42 refreshes after process events and permits cached reads for other events. Removing the exact container/PID entry at pkg/processtree/process_tree_manager.go:94 preserves the existing error and prevents reuse of that invalid branch. The pinned LRU synchronizes removal internally; the change adds no API, configuration, dependency, or data-format change. Independent code and architecture reviews found no introduced blocker.

Related history:

  • #1043 discussion documents this same-command gap; the maintainer explicitly invited the follow-up. Its command check does not supersede this fix.
  • Merged #876 repairs creator-node PID reuse and exit cleanup, and #1027 repairs host ancestry. Neither invalidates this separate manager cache on reconstruction failure. #936 optimized the manager without repairing this error path.
  • Open #1048 addresses the separate pre-existing creator-lock race in branch traversal; it complements this change.
  • #1014 proposes cache migration. #1018's decision keeps production on Hashicorp pending performance evidence; this PR respects that boundary. Closed #521 had approval and a suggestion to close, with no documented rejection of this approach.

Searches covered open/closed/merged PRs and issues using processtree, process tree, PID reuse, GetContainerProcessTree, process_tree_manager.go, cached branch, stale/process, and refresh/cache variants, capped at 50 results per query. No duplicate or superseding invalidation fix was identified; keyword search is not exhaustive. No applicable prior rejection was found.

Validation used a network-isolated Bubblewrap sandbox with cleared environment, no credentials, dropped capabilities, read-only source/dependencies, and temporary writable build storage:

  • Target implementation plus the new test, via Go overlay: fails as expected at test line 195 because the cached read incorrectly succeeds.
  • PR head: go test -mod=readonly -count=1 ./pkg/processtree/... passes.
  • go test -mod=readonly -race -run TestManager_GetContainerProcessTree_FailedRefreshDropsCachedBranch -count=10 ./pkg/processtree: passes, with no race reports.
  • go vet -mod=readonly ./pkg/processtree/...: passes.
  • Formatting and git diff --check: clean.

GitHub validation succeeded at the reviewed head, including the non-race test step; its race step was skipped. Component tests, benchmark and CodeQL also report success. No live PID-wrap/cluster reproduction or general concurrency-coherence guarantee is claimed. Gopls returned invalid imported types, so semantic caller analysis was unavailable; callers were inspected directly. Concurrent cache publication and broader PID identity handling remain outside this bounded fix.

@matthyx
matthyx merged commit 36a9a05 into kubescape:feat/host-profile-gaps Oct 24, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants