Repository navigation
fix(celery): stop the worker starving the viewer on 1 GB 64-bit Pi 3 - #3532
Merged
Merged
Conversation
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>
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
|
vpetersson
approved these changes
Nov 28, 2026
vpetersson
approved these changes
Nov 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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 logsAnthiasViewer did not emit "Anthias service start" within 30sover and over, and some devices reboot.It is memory, not the GPU. On a device in the loop:
AnthiasViewer.AnthiasViewersat in D state infilemap_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.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-64server images). Almost all of the growth is thecryptographystack, 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)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.controller.tasks.publish_slots()already sizes itself fromworker_concurrency, so with two, one long publish or download still leaves a process for the once-a-minute display-schedule task.MemTotalis 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--concurrencystill wins.2. Keep
cryptographyout of processes that don't need itlib/caddy.pynow importsnode_identityandweb_certificateinside the functions that render or apply a config. Every Django process importslib.caddyat startup to register the m2m site provider.controller/tasks.pyimportsservice,push,caandjoininside 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-freecontroller/mode.py, since it was all those tasks needed up front.api.py,push.py,views.pyandtelemetry.pynow import it from there.How I know it's right
concurrency: 2 (prefork).cryptographyis mapped into the celery processes.tests/test_celery_memory_footprint.py:/proc/meminforeader and the low-RAM threshold.django.setup()and imports the worker in a fresh interpreter, then asserts that nocryptography.hazmat/x509/fernetmodule is loaded.lib/caddy.pyandcontroller/tasks.pychanges reverted.pytest -m "not integration"passes (3359 passed, 3 skipped).ruff check,ruff format --checkandmypy .are clean. mypy's only complaints are abouttools/image_builderdeps 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