Skip to content

linux: do not treat every PID namespace as a container - #2140

Open
ravi-arnan wants to merge 1 commit into
htop-dev:mainfrom
ravi-arnan:fix-hide-container
Open

ravi-arnan wants to merge 1 commit into
htop-dev:mainfrom
ravi-arnan:fix-hide-container

Conversation

@ravi-arnan

Copy link
Copy Markdown
Contributor

Closes #2053

Problem

"Hide processes running in containers" (O / Display options) compares each process's /proc/PID/ns/pid inode with htop's own and treats any difference as a container, falling back to counting NSpid: entries when that stat is not permitted.

A PID namespace is not necessarily a container. Chromium's sandbox, bubblewrap, flatpak runners, and plain unshare(1) all create one inside an ordinary desktop session. On a desktop machine with a browser open, enabling the option hides most of the process list even though nothing is containerized.
Measured on a host with no containers running: 903 of 2,739 tasks hidden, every one of them a Brave or Chrome sandbox task.

The change

Confirm the namespace difference against the cgroup, which is the signal that actually separates real containers from desktop process sandboxes:

  1. CGroup_looksLikeContainer(const char* cgroup):
    • Checks if the cgroup path carries a container label recognised by CGroup_filterContainer() (docker, podman, LXC, systemd-nspawn, including rootless variants under user.slice).
    • If unlabelled, checks if the cgroup path sits outside user.slice (such as Kubernetes under kubepods, or system services/containers under system.slice).
    • Browser sandboxes stay in the session's own user.slice with no container label, so they are not treated as containers.
  2. Consistency: The hide option and the CONTAINER column now agree with each other.
  3. Performance: /proc/PID/cgroup is read only for the few processes that showed a namespace difference (unless a cgroup column is already enabled in the screen settings), so the common case costs no extra file operations.

Verification

Tested locally on Linux (NixOS, Linux 6.17.0, x86_64):

  • Host with no containers running and multiple browser windows (Brave / Chromium sandbox processes):
    • Before: 900+ desktop and sandbox processes hidden; process list almost empty.
    • After: All ordinary desktop and sandbox processes remain visible.
  • Running Docker container (docker run --rm -d alpine sleep 60):
    • After: The containerized sleep 60 process has its cgroup identified (docker-...) and is correctly hidden when "Hide processes running in containers" is enabled.
  • Compiles cleanly with GCC, -std=c99 -pedantic -Wall -Wextra, 0 errors.

"Hide processes running in containers" compares each process's
/proc/PID/ns/pid inode with htop's own and treats any difference as a
container, falling back to counting NSpid: entries when that stat is not
permitted. A PID namespace is not a container. Chromium's sandbox,
bubblewrap and plain unshare(1) all create one inside an ordinary desktop
session, so on a machine with a browser open the option hides most of the
process list even though nothing is containerized. Measured on a host with
no containers running: 903 of 2739 tasks hidden, every one of them a Brave
or Chrome sandbox task.

Confirm the namespace difference against the cgroup, which is the signal
that actually separates the two cases. htop already derives a container
name from the cgroup for the CONTAINER column, so the hide option and that
column now agree with each other. Container runtimes either label the
cgroup, which CGroup_filterContainer() already recognises for docker,
podman, LXC and systemd-nspawn including their rootless variants under
user.slice, or they place the payload outside user.slice, as Kubernetes and
the CRI runtimes do. A sandbox stays in the session's own user.slice cgroup
with no label.

The cgroup file is read only for the processes that showed a namespace
difference, unless a cgroup column is displayed anyway, so the common case
costs nothing.

Closes htop-dev#2053

Assisted-by: Claude:opus-5
@coderabbitai

coderabbitai Bot commented Oct 24, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 56939959-179b-4d75-89cb-a5d7ea14baa7
Commits

Reviewing files that changed from the base of the PR and between 394b4a3 and 396247e.

Files selected for processing: 3
  • linux/CGroupUtils.c
  • linux/CGroupUtils.h
  • linux/LinuxProcessTable.c

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

Walkthrough

The change adds CGroup_looksLikeContainer to classify cgroup paths. The process table now reads cgroup data for processes in different PID namespaces and clears their container status when the cgroup is not classified as a container.

Objective Addressed Explanation
Ensure enabling “Hide processes running in containers” does not hide processes on a system without containers [#2053] ✅

Suggested reviewers: cgzones, benbe

Priority: Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Low · up to 39624

Some container processes with mixed cgroup-v1 paths may remain visible when container hiding is enabled. This bounded case does not appear to block merging.

- [ ] 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

A cgroup path enters the test
A user slice is set apart
Container labels guide the check
Namespace flags receive a review
Ordinary processes stay in view

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Hide processes running in containers" hides nearly all processes when no containers are running

1 participant