Skip to content

fix(celery): stop the worker starving the viewer on 1 GB 64-bit Pi 3 - #3532

Merged
vpetersson merged 3 commits into
masterfrom
agent/anthias-claude/504a57b112ed
Nov 28, 2026
Merged

vpetersson merged 3 commits into
masterfrom
agent/anthias-claude/504a57b112ed

Conversation

@vpetersson-bot

Copy link
Copy Markdown
Contributor

Fixes #3528

What goes wrong

On a 1 GB Raspberry Pi 3 running the 64-bit image (balena fleet pi3-64, and the Raspberry Pi Imager release image, which is the balena image), 2026.11.x can leave the screen restart-looping. The viewer logs AnthiasViewer did not emit "Anthias service start" within 30s over and over, and some devices reboot.

It is memory, not the GPU. On a device in the loop:

  • RAM was exhausted, and balenaOS's swap (a zram device, so it also lives in RAM) was 100% used.
  • The kernel's global OOM killer had already killed AnthiasViewer.
  • The respawned AnthiasViewer sat in D state in filemap_fault, waiting to page its own code back in from the SD card. Celery, the supervisor and the engine were blocked the same way, which is why it misses the handshake and why SIGKILL appears not to work.
  • The largest consumer was the Celery worker: Celery's default pool is one prefork child per core, so on a Pi 3 that is four children plus beat and the parent, at about 90 MB each.

Importing the server app grew from about 76 MB to about 91 MB per process between 2026.10.0 and 2026.11.2 (measured on a Pi 3 with the published pi3-64 server images). Almost all of the growth is the cryptography stack, about 15 MB per process, which the multi-screen PKI pulls into every uvicorn and celery process whether or not the screen is managed. That growth, paid by every Celery process, is what tipped the 64-bit Pi 3 over. The 32-bit Pi 3 image (smaller armhf Python) and boards with more RAM are unaffected.

The change

1. Size the Celery pool to the board's RAM (celery_tasks.py, anthias_common/board.py)

  • Below LOW_RAM_THRESHOLD_KB (1.5 GiB, the existing cut between 1 GB and 2 GB boards), the worker runs 2 pool children instead of one per core.
  • worker_max_memory_per_child (200 MB) replaces a child that has grown too large after its task.
  • Why two and not one: controller.tasks.publish_slots() already sizes itself from worker_concurrency, so with two, one long publish or download still leaves a process for the once-a-minute display-schedule task.
  • MemTotal is read from /proc/meminfo, which a container sees unnamespaced, because balena runs no host agent to publish it to Redis. The host agent's value is the fallback. An explicit --concurrency still wins.

2. Keep cryptography out of processes that don't need it

  • lib/caddy.py now imports node_identity and web_certificate inside the functions that render or apply a config. Every Django process imports lib.caddy at startup to register the m2m site provider.
  • controller/tasks.py imports service, push, ca and join inside the tasks, after the controller-mode check. The hourly and per-minute controller tasks on an ordinary screen return without loading the PKI.
  • controller_enabled() moves to a new dependency-free controller/mode.py, since it was all those tasks needed up front. api.py, push.py, views.py and telemetry.py now import it from there.

How I know it's right

  • Real hardware: on a Pi 3B+ (64-bit, 2026.11.2 images) with this branch mounted into the server and celery containers:
    • The worker starts with concurrency: 2 (prefork).
    • Celery drops from 8 processes / ~564 MB (RSS + swap) to 6 / ~391 MB.
    • No cryptography is mapped into the celery processes.
    • Host swap use roughly halved.
    • Periodic tasks still succeed, the API answers, and the viewer is unaffected.
  • New tests/test_celery_memory_footprint.py:
    • Covers the /proc/meminfo reader and the low-RAM threshold.
    • Includes a guard that runs django.setup() and imports the worker in a fresh interpreter, then asserts that no cryptography.hazmat / x509 / fernet module is loaded.
    • The guard fails with the lib/caddy.py and controller/tasks.py changes reverted.
  • Full suite: pytest -m "not integration" passes (3359 passed, 3 skipped). ruff check, ruff format --check and mypy . are clean. mypy's only complaints are about tools/image_builder deps not installed locally.

Reviewer guide

Two commits, readable on their own: the pool sizing first, then the lazy imports with the regression test.

🤖 Generated with Claude Code

vpetersson-bot and others added 2 commits November 27, 2026 22:26
The worker ran Celery's default pool, one prefork child per core, on
every board. Each child carries the whole Django app (about 90 MB on a
64-bit Pi), so on a 1 GB Pi 3 the four children, beat and the parent
took about 600 MB. On balenaOS, where swap is zram and so lives in RAM
too, that left QtWebEngine nothing: the kernel OOM-killed AnthiasViewer,
and every respawn then sat in D state paging its own code back in from
the SD card, missed the 30 s startup handshake, and the viewer
container restart-looped.

Below LOW_RAM_THRESHOLD_KB the worker now runs two children and
replaces one that grows past 200 MB after its task. Two rather than
one keeps a long download or publish from holding up the
once-a-minute housekeeping tasks (publish_slots already leaves one
process free). MemTotal is read from /proc/meminfo, which a container
sees unnamespaced, because balena runs no host agent to publish it;
the host agent's Redis value is the fallback. A --concurrency on the
command line still wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Every process that sets up Django registers the Caddy m2m site
provider, which imported lib.caddy, which imported node_identity and
web_certificate at module level. The celery app also loads
controller.tasks, which imported controller.service and
controller.push. Between them that loaded the cryptography stack
(about 15 MB per process on a 64-bit Pi) into the parent, beat and
every pool child, on every screen, whether or not it is managed or a
controller.

lib.caddy now imports node_identity and web_certificate where a config
is rendered or applied. controller.tasks imports service, push, ca and
join inside the tasks, after the controller-mode check, so the
periodic housekeeping on an ordinary screen returns without them.
controller_enabled() moves to controller.mode, a module with no
dependencies, since it was the only thing those tasks needed up front.

tests/test_celery_memory_footprint.py pins both: it sets up Django and
imports the worker in a fresh interpreter and asserts no
cryptography.hazmat / x509 / fernet module is loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
@vpetersson-bot
vpetersson-bot requested a review from a team as a code owner November 27, 2026 22:27
Copilot AI lite review requested due to automatic review settings November 27, 2026 22:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error: Your billing is not configured or you have Copilot licenses from multiple standalone organizations or enterprises. To use premium requests, select a billing entity via the GitHub site, under Settings > Copilot > Features.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Copilot AI lite review requested due to automatic review settings November 27, 2026 22:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error: Your billing is not configured or you have Copilot licenses from multiple standalone organizations or enterprises. To use premium requests, select a billing entity via the GitHub site, under Settings > Copilot > Features.

@sonarqubecloud

Copy link
Copy Markdown

@vpetersson
vpetersson merged commit eef6efb into master Nov 28, 2026
9 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.

[BUG] 2026.11.2 RPiB+ Stuck in boot loop

3 participants