Skip to content

fix(envd): only raise oom_score_adj when the inherited score is below the target - #3840

Open
yjjnls wants to merge 1 commit into
e2b-dev:mainfrom
yjjnls:fix/oom-score-adj-inherited
Open

yjjnls wants to merge 1 commit into
e2b-dev:mainfrom
yjjnls:fix/oom-score-adj-inherited

Conversation

@yjjnls

@yjjnls yjjnls commented Nov 22, 2026

Copy link
Copy Markdown

Fixes #3775.

Problem

When envd inherits an oom_score_adj above 100 and does not hold CAP_SYS_RESOURCE — e.g. a container started with --oom-score-adj=993, or a Kubernetes Burstable pod — every command fails. The process wrapper prepends echo 100 > /proc/$$/oom_score_adj && exec ..., the shell runs after SysProcAttr.Credential has dropped to the target user, and an unprivileged process may not lower its OOM score (proc(5)). The rejected write aborts the && chain, so even true fails with echo: I/O error. This is a total process-spawn outage, not just a lost OOM reset, and it is present in current main and all affected tags.

Change

The decision moves into Go at spawn time: envd reads /proc/self/oom_score_adj (children inherit it) and prepends the write only when the inherited score is below the target (100).

Inherited score Wrapper prefix Result
< 100 (envd's systemd -1000, a Guaranteed pod's -997, or 0) echo 100 > /proc/$$/oom_score_adj && unchanged from today; the #2028 race fix and the OOM protection reset still apply
== 100 omitted identical outcome to writing 100
> 100 (Burstable/BestEffort pod, Docker --oom-score-adj) omitted spawn succeeds; the runtime-assigned (higher) score is preserved
/proc read fails echo 100 > ... && kept falls back to today's behavior rather than silently dropping the reset

Raising an OOM score is always permitted for an unprivileged process, and preserving a higher runtime-assigned score is also the semantically correct outcome — the runtime deliberately made these processes more OOM-killable than 100.

Considerations

  • Why in Go and not in the shell: $(<file) is not POSIX; dash/busybox ash would need cat or read, adding a fork per spawn on the hot path. Reading in Go costs one small /proc read per process start and no extra process. The wrapper stays a one-liner.
  • Read/fork race: benign. The score is read just before the fork; if it changed in between, the worst case is today's behavior or a preserved higher score — never a failed spawn.
  • Read-failure fallback: /proc/self/oom_score_adj is always readable by the process on Linux, so the fallback matters only where procfs is absent (where the write would fail anyway) and for non-Linux builds of the unix file. Keeping the write preserves the previous behavior and the OOM protection reset; skipping it would be a silent behavior change in the normal case.
  • Existing sandbox behavior is untouched: inside the VM envd runs with oom_score_adj=-1000, which is below the target, so the wrapper still resets children and grandchildren to 100 before exec exactly as fix(envd): eliminate OOM score and nice priority race in child processes #2028 intended.
  • Residual limitation (same as discussed in fix(envd): eliminate OOM score and nice priority race in child processes #2028): os/exec.Cmd cannot run a hook between the credential drop and exec, so an envd that itself runs unprivileged with a score above 100 cannot have its children corrected by any in-child write. With this change that residual case degrades to "children run at the inherited score" instead of "every spawn fails". The nice/ionice legs of the wrapper are unaffected — their deltas only ever move the child to an equal-or-worse priority, which an unprivileged process may always do.
  • Performance: no extra process or fork per spawn; one /proc read (a few microseconds) on the existing per-process start path, and one fewer write syscall in the child when the skip branch applies. No change to steady-state or concurrency behavior.

Tests

New unit tests in packages/envd/internal/services/process/handler/spawn_unix_test.go pin the wrapper-prefix semantics at the boundaries: below the target raises (-1000, 0, 99), at or above the target keeps the inherited score (100, 993), unreadable falls back to the unconditional write.

Ran locally:

  • go test -race ./internal/services/process/... — pass
  • go test ./... in packages/envd — pass; the only failures are pre-existing, environment-dependent filesystem tests that require the bindfs binary, unrelated to this change
  • golangci-lint run (pinned v2.15.0) on the package — 0 issues; formatters clean

No integration test added: exercising this end to end requires a container/VM spawned with a restricted oom_score_adj and no CAP_SYS_RESOURCE; the unit tests pin the generated wrapper, which is what decides the behavior.

The process wrapper unconditionally ran
`echo 100 > /proc/$$/oom_score_adj && exec ...` before every command.
An unprivileged process may not lower its OOM score (proc(5)), so in a
container that inherits a score above 100 (Docker
`--oom-score-adj=993`, a Kubernetes Burstable pod) the write is
rejected and the `&&` chain fails the spawn: every command fails with
an `echo: I/O error`.

Read /proc/self/oom_score_adj in envd -- children inherit it -- and
prepend the write only when the inherited score is below the target.
Raising is always permitted, and preserving a higher score assigned by
the container runtime keeps its OOM protection intact. When the score
cannot be read, fall back to the unconditional write, matching the
previous behavior.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

envd commands fail when inherited oom_score_adj exceeds 100 in restricted containers

1 participant