fix(celery): survive a down balena supervisor in reboot/shutdown#3131
Merged
Conversation
The supervisor is routinely down right when these tasks fire (it restarts itself during an OTA apply), so the 5x1s retry window died in an unhandled RetryError and the command was silently dropped (Sentry ANTHIAS-3F, 60 events/day). - Retry with exponential backoff for up to 2 minutes - Bound each supervisor HTTP attempt (10s) so a hung socket can't pin the worker; add task soft/hard time limits as backstop - raise_for_status so a 5xx from a half-started supervisor retries instead of reading as success - Terminal failure logs a warning (expected transient), not an unhandled exception Fixes #3123 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR hardens the Balena supervisor-backed reboot_anthias / shutdown_anthias Celery tasks so they tolerate the supervisor being temporarily down (e.g., during OTA apply), bounding each HTTP attempt with a timeout and avoiding unhandled RetryError noise.
Changes:
- Replace fixed-count retries with a 2-minute retry window using exponential backoff, per-attempt HTTP timeouts, and task soft/hard time limits.
- Add a shared
_run_supervisor_command()helper that retriesraise_for_status()failures and logs a terminal warning instead of erroring. - Update Balena supervisor power-command helpers to accept and forward a
timeout=..., and add unit tests for retry and terminal-failure behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/anthias_server/celery_tasks.py |
Adds retry/time-limit constants, a shared supervisor-command retry helper, and applies it to reboot/shutdown tasks. |
src/anthias_common/utils.py |
Extends supervisor reboot/shutdown helpers to accept a forwarded request timeout. |
tests/test_celery_tasks.py |
Adds tests ensuring reboot/shutdown retry behavior and terminal failure warning behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Copilot review: an unfiltered Retrying loop would retry TypeError and SoftTimeLimitExceeded too, masking real bugs and defeating the soft limit. Also zero the backoff in the tests so they don't sleep. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot review round 2: include the last exception in the give-up warning, and set the test retry window to 0 so the zero-backoff loop can't spin thousands of attempts before stop_after_delay trips. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
celery_tasks_module.logging is not an explicitly exported attribute; string-target mock.patch matches the test_github.py pattern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
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.



Issues Fixed
Description
reboot_anthias/shutdown_anthiasretried the balena supervisor API 5 times, 1 second apart, with no HTTP timeout and no status check. The supervisor is routinely down exactly when these fire — it restarts itself during an OTA apply — so the tasks died in an unhandledRetryError(60 Sentry events in a day from one pi4-64) and the power command was silently dropped.Now:
raise_for_status()so a 5xx from a half-started supervisor retries instead of silently counting as "command accepted".Validation on real hardware
This path only runs under balenaOS (it talks to the balena supervisor API), and our testbeds are plain docker-compose devices — so there is no supervisor to exercise it against on the testbed. The behaviour is covered by unit tests (retry-until-recovery, non-transport-error propagation, terminal-failure-warns-not-raises for both reboot and shutdown). The non-balena branch (
r.publish('hostcmd', ...)) is unchanged. Real-device confirmation would need a balena fleet device.Checklist
🤖 Generated with Claude Code