Skip to content

feat: add shareProcessNamespace to pod spec (defaults to true) - #476

Closed
alexander-morris wants to merge 2 commits into
stubbi:mainfrom
OperatingSystem-1:feat/share-process-namespace
Closed

alexander-morris wants to merge 2 commits into
stubbi:mainfrom
OperatingSystem-1:feat/share-process-namespace

Conversation

@alexander-morris

Copy link
Copy Markdown
Contributor

Summary

Adds spec.shareProcessNamespace field to OpenClawInstance CRD, defaulting to true. When enabled, the Kubernetes pause container becomes PID 1 and automatically reaps zombie processes.

Problem

Long-running OpenClaw gateway pods spawn background processes (git, QMD memory, shells, plugin activity) which become defunct under a Node.js PID 1 that does not call waitpid(). Over time, zombies accumulate, degrading pod health and causing probe failures that kill the pod.

See #471 for the original report (28 zombies observed on a production pod).

Changes

  • api/v1alpha1/openclawinstance_types.go: Added ShareProcessNamespace *bool field with +kubebuilder:default=true
  • internal/resources/statefulset.go: Added shareProcessNamespace() helper, wired into PodSpec
  • Generated files: deepcopy, CRD YAML, Helm chart CRDs all regenerated

Testing

  • Unit tests pass (go test ./internal/resources/ -v)
  • Deployed to production cluster (28 agents), verified:
    • PID 1 = /pause on all pods
    • Zero zombie accumulation over 7+ hours
    • Zero restarts on 24/28 agents (remaining 4 had unrelated OOM issues)
    • Gateway healthy, health probes passing

Fixes #471

Enables PID namespace sharing so the Kubernetes pause container
becomes PID 1 and automatically reaps zombie processes.

Long-running OpenClaw gateway pods spawn background processes (git,
QMD memory, shells, plugin activity) which become defunct under a
Node.js PID 1 that does not call waitpid(). Over time, zombies
accumulate, degrading pod health and causing probe failures.

With shareProcessNamespace enabled, the pause container reaps all
orphaned processes automatically. Users can opt out with
`spec.shareProcessNamespace: false` if needed.

Fixes stubbi#471

Signed-off-by: Alexander Morris <[email protected]>

@stubbi stubbi left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for tackling this Alex - PID-namespace sharing is a clean k8s-native fix for the zombie-reaping issue in #471, and the production validation data is great to see. A few items before this can land:

Required changes

Docs (CLAUDE.md mandates this for every CRD field change):

  • README.md — add an entry to the feature table and ideally a short config snippet showing how to opt out
  • docs/api-reference.md — document spec.shareProcessNamespace: type, default, behavior, security implications

E2E test:

  • Add a case under test/e2e/e2e_suite_test.go verifying that the generated StatefulSet's PodSpec.ShareProcessNamespace defaults to true and can be overridden to false. The existing init-plugins/init-skills cases are good models.

Unit test:

  • Add a builder test in internal/resources/resources_test.go covering: (a) default true when unset, (b) explicit false, (c) explicit true. See the AutomountServiceAccountToken tests around line 1121 / 8268 / 9351 for the pattern.

.gitignore:

  • Remove the manager line. The standard build output is bin/manager, which is already covered by bin/. A bare manager at the repo root is a local artifact (likely from running go build outside Make) and shouldn't ship in the project gitignore - that belongs in your global gitignore.

Discussion / nice-to-have

  • Security implication of the default flip: with shareProcessNamespace: true, every container in the pod can see and signal every other container's processes. For users running sidecars (Tailscale, Ollama, browser), this means a compromised sidecar could kill the gateway, and vice versa. This is generally an acceptable tradeoff for the zombie-reaping benefit, but it should be called out in the api-reference doc, and we should make sure existing sidecar containers don't rely on PID-isolation assumptions.
  • Existing-instance behavior: changing the default to true will trigger a pod restart on every OpenClawInstance after upgrade. Worth mentioning in the PR description / changelog so users running production fleets aren't surprised.
  • Helper redundancy (minor): shareProcessNamespace() returning Ptr(true) when the field is nil is belt-and-suspenders, since the kubebuilder default fills nil -> true at the API server. Not a blocker - it's defensive against pre-existing instances stored before the CRD upgrade, which is reasonable.
  • Field placement: ShareProcessNamespace is currently appended after PodAnnotations in the spec. Consider grouping it near the other pod-level security/runtime fields (e.g. near SecurityContext-adjacent fields) for better discoverability. Not a blocker.

The core change looks correct and the production data is convincing. Once docs + tests are in and the gitignore line is dropped, this should be ready to merge.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 33.33333% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
api/v1alpha1/zz_generated.deepcopy.go 0.00% 4 Missing ⚠️
internal/resources/statefulset.go 60.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@stubbi

stubbi commented May 12, 2026

Copy link
Copy Markdown
Owner

Closing this one out in favor of #485, which picks up your production-validated shareProcessNamespace approach and adds the docs, unit tests, and e2e specs from my earlier review. I left you as Co-Authored-By on the commit so the production validation credit travels with the merge.

Thanks for the thorough writeup and for running the 28-agent / 7-hour soak - the data was what convinced me to make the field default to true rather than opt-in. Really appreciate the work.

Follow-up: please holler if you'd like to be a reviewer on #485, or if you spot anything in the new docs/tests that drifts from how you actually used the field in production.

@stubbi stubbi closed this May 12, 2026
stubbi added a commit that referenced this pull request May 12, 2026
## Summary

Adds `spec.shareProcessNamespace` (default `true`) to
`OpenClawInstance`. When enabled, all containers in the pod share a PID
namespace and the Kubernetes `pause` container becomes PID 1,
automatically reaping zombie processes that accumulate under the Node.js
gateway (which does not call `waitpid()` on orphaned children).

Closes #471.
Supersedes #476 — incorporates @alexander-morris's production-validated
approach with the docs, unit tests, e2e tests, and field placement
requested in [the review on
#476](#476 (review)).

## Background

[#471](#471)
reported 28 defunct processes accumulating under PID 1 on a production
OpenClaw pod (QMD, git, shells, plugin helpers). Image-level fixes
(`tini` in the entrypoint) are the most durable long-term solution and
are tracked upstream at openclaw/openclaw#74083, but operator users have
no clean way to wrap the entrypoint today. PID-namespace sharing is the
standard Kubernetes-native answer and stacks harmlessly with a future
upstream init process.

@alexander-morris validated the approach in production (28 agents, 7+
hours, zero zombie accumulation, zero unrelated restarts on 24/28 pods).

## Changes

**Core:**
- `api/v1alpha1/openclawinstance_types.go` — `ShareProcessNamespace
*bool` with `+kubebuilder:default=true`, placed next to `Security`
(pod-level runtime/security cluster) per review feedback on #476.
- `internal/resources/statefulset.go` — `shareProcessNamespace()`
helper; treats `nil` as `true` so instances stored before the field
existed still get the fix on next reconcile.

**Generated (do not hand-edit):**
- `api/v1alpha1/zz_generated.deepcopy.go`
- `config/crd/bases/openclaw.rocks_openclawinstances.yaml`
-
`charts/openclaw-operator/templates/crds/openclaw.rocks_openclawinstances.yaml`

**Tests:**
- `internal/resources/resources_test.go` — three new builder cases:
default-true (field unset), explicit-true, explicit-false. Pattern
matches the existing `AutomountServiceAccountToken` tests.
- `test/e2e/e2e_suite_test.go` — two new specs in the controller suite:
default-true and explicit-false override. Pattern matches the adjacent
`runtimeClassName` spec.

**Docs:**
- `README.md` — new feature-table row and a "Shared Process Namespace"
subsection with opt-out snippet and security trade-off.
- `docs/api-reference.md` — `spec.shareProcessNamespace` field reference
with type/default/behavior, security implication, and upgrade note about
pod-template rollout for existing instances.

## Security trade-off (called out in docs)

With sharing enabled, every container in the pod can see and signal
every other container's processes. A compromised sidecar (Tailscale,
Ollama, browser, custom) could send signals to the gateway and vice
versa. For most deployments the zombie-reaping benefit clearly outweighs
this; users with mutually-distrusting sidecars can opt out per instance
and reap inside their image (`tini`/`dumb-init`).

## Upgrade impact

Flipping the default to `true` means every existing `OpenClawInstance`
will get a one-time pod-template change on next reconcile, rolling pods.
Operators running large fleets should plan a maintenance window. Called
out in the api-reference upgrade note.

## Test plan

- [x] `go test ./internal/resources/` passes (3 new tests covering
default/true/false)
- [x] `make lint` passes (golangci-lint v1.64.5)
- [x] `go vet ./...` clean
- [x] `make generate manifests sync-chart-crds` regenerates cleanly (no
manual edits to generated files)
- [ ] E2E suite passes in CI on a real kind cluster (new specs follow
the existing `runtimeClassName` pattern)

## Credit

Production validation and original PR by @alexander-morris in #476.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: alexander-morris <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

bug: operator-managed gateway pods can accumulate zombie helper processes

3 participants