Skip to content

Fix Swoole auto worker count in containers#1101

Merged
taylorotwell merged 3 commits intolaravel:2.xfrom
mtawil:fix/container-aware-cpu-count
Mar 10, 2026
Merged

Fix Swoole auto worker count in containers#1101
taylorotwell merged 3 commits intolaravel:2.xfrom
mtawil:fix/container-aware-cpu-count

Conversation

@mtawil
Copy link
Contributor

@mtawil mtawil commented Mar 10, 2026

Summary

  • When running in containers (Kubernetes, Docker), swoole_cpu_num() reports the host node's CPU count instead of the container's cgroup CPU limit
  • This causes --workers=auto and --task-workers=auto to spawn far more processes than the container can handle, leading to OOMKills
  • The fix reads the container's cgroup CPU quota (v2 and v1) before falling back to swoole_cpu_num()

Example

A pod with 1 CPU limit on a 96-core node:

  • Before: 96 workers + 96 task workers (OOMKilled)
  • After: 1 worker + 1 task worker

Test plan

  • Existing SwooleExtensionTest::test_cpu_count still passes
  • Added tests for container CPU detection and fallback behavior
  • Verified cgroup v2 values on a real Kubernetes pod (cpu.max: 100000 100000 → 1 CPU)
  • Full test suite passes (174 tests)

@taylorotwell taylorotwell merged commit 37b6175 into laravel:2.x Mar 10, 2026
28 checks passed
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.

2 participants