Skip to content

supervisor: SH_WORKERS default reads host CPU count via cpus().length, blind to container/cgroup CPU limits #341

Closed

Description

@pdettori

Summary

readConfig()'s default for SH_WORKERS is defaultWorkers(cpus().length) (packages/supervisor/src/config.ts:90, cpus imported from node:os at :1). os.cpus() enumerates the host's physical CPU list and does not consult cgroup CPU quotas. Under a container or pod with a CPU limit smaller than the host (docker run --cpus=2, a Kubernetes CPU limit, any cgroup-quota-based cap), SH_WORKERS silently defaults to the host's core count, not the deployment's actual allocation.

This is not hypothetical for this repo. P6's whole thesis is measuring density against a fixed worker pool size W (P6 spec §5.1; E8/E9 — rossoctl/moca-experiments#11, rossoctl/moca-experiments#12). If W silently tracks host cores instead of the container's real CPU allocation, any density number gathered in a CPU-limited container describes the host, not the deployment being measured — and an operator who deploys P6 in a CPU-limited container gets an oversized, thrashing worker pool with no error and no warning. That's the same failure shape defaultWorkers's own docstring already worries about for the os.cpus() === [] case (:52-59), just from the opposite direction (too many, not too few).

The env template already documents the current (buggy) behavior as fact: deploy/vm/env/supervisor.env.example says "SH_WORKERS defaults to os.cpus().length" — that comment needs to change with the fix.

Current behavior (verified in this tree)

  • packages/supervisor/src/config.ts:90: workers: readInt(env, 'SH_WORKERS', defaultWorkers(cpus().length), { min: 1 })
  • packages/supervisor/src/config.ts:61-63: defaultWorkers only clamps its input to ≥ 1 — it has no opinion about where the count comes from, so the fix is entirely at the call site, not in defaultWorkers itself
  • packages/supervisor/test/config.test.ts:12: the existing default-value test asserts c.workers === defaultWorkers(cpus().length) — it currently encodes the buggy source as the expected behavior and must change with the fix
  • Confirmed in this environment (Node v25.2.1, pinned in this repo): node:os's availableParallelism() exists (added Node 19.4/20) and is backed by libuv's uv_available_parallelism(), which — unlike uv_cpu_info() (os.cpus()'s backing call) — checks cpu.max / cpu.cfs_quota_us under cgroup v2/v1 on Linux and clamps to it

Fix

Swap the call site's source from cpus().length to availableParallelism() (same node:os import). No change to defaultWorkers's signature or clamping behavior — it stays a pure function of a count, so its existing 0/1/8 clamp tests (:62-64) are untouched.

Test plan (write these first)

  1. packages/supervisor/test/config.test.ts: vi.mock('node:os', ...) so availableParallelism() and cpus() return different values, then assert readConfig({...}).workers === defaultWorkers(<availableParallelism mock value>) and explicitly !== defaultWorkers(<cpus().length mock value>). This fails against current code (which uses cpus().length) and passes once the call site switches — the two mocked values must differ, or the test would pass vacuously without the fix.
  2. Update the pre-existing assertion at :12 to use availableParallelism() instead of cpus().length.
  3. defaultWorkers's own unit tests (:62-64) need no change — its contract (clamp to ≥ 1) is unchanged.
  4. Update the comment in deploy/vm/env/supervisor.env.example ("SH_WORKERS defaults to os.cpus().length") to describe the new source.

Acceptance

  • SH_WORKERS's default is computed from a cgroup-aware source (availableParallelism()), not cpus().length
  • A test fails on pre-fix code and passes on post-fix code via two mocked sources that differ (not a vacuous pass)
  • defaultWorkers's existing clamp behavior and tests are untouched
  • No behavior change when SH_WORKERS is set explicitly by an operator — this only touches the unset-default path
  • deploy/vm/env/supervisor.env.example's comment updated to match

Out of scope

  • Verifying against a real CPU-limited container (docker run --cpus=N) — worth doing once a Docker Compose deployment for P6 lands (companion issue, filed alongside this one), but the mocked unit test above is sufficient to fix and pin this bug today.
  • Any other config knob (SH_TURNS_PER_WORKER, ports, routing policy, etc.) — untouched.

Related: P6 spec (docs/specs/2026-09-08-p6-vm-process-manager-design.md), ADR-0034, rossoctl/moca-experiments#11 (E8), rossoctl/moca-experiments#12 (E9).

Activity

  1. added a commit that references this issue on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions