fix: gate background loops behind enable_background_workers - #298
Open
nishant-uxs wants to merge 2 commits into
Open
fix: gate background loops behind enable_background_workers#298nishant-uxs wants to merge 2 commits into
nishant-uxs wants to merge 2 commits into
Conversation
Uvicorn runs lifespan once per worker, so multi-worker deployments were starting N schedulers/dispatchers/watchdogs against the same DB. Add AUTOMATION_ENABLE_BACKGROUND_WORKERS (default true) so request-serving replicas can leave the loops to a single dedicated process. Fixes OpenHands#286
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.
Problem
lifespanstarts the scheduler, dispatcher, and watchdog unconditionally. Uvicorn runs lifespan once per worker process, so multi-worker / multi-replica deployments runreplicas × --workerscopies of each loop against the same DB. Watchdog copies fan out sandbox-API verifies (contributing to HTTP 429 → falseFAILED, see #285) and put background CPU on webhook-serving processes.Root cause
Ownership of background work was tied to process startup, not to a deployment-level singleton. There was no way for a request-serving worker to skip the loops.
Solution
Implement the preferred fix from #286:
AUTOMATION_ENABLE_BACKGROUND_WORKERS(defaulttrue)false, this process starts zero of scheduler / dispatcher / watchdogtrue, starts exactly one of each (prior single-process behavior)Extracted
start_background_worker_tasks()so lifespan shutdown only awaits tasks that were actually started.Operator model
true(no change)truefalseThis is an explicit single-owner gate, not leader election — enabling the flag on every worker still fans out (covered by a regression test so the invariant stays documented).
Why this approach
Risks
Testing
uv run pytest tests/test_background_workers.py tests/test_config.py tests/test_health.py— 57 passedtest_disabled_*andtest_multi_worker_simulation_single_ownerfail (3 tasks started when disabled)ruff check/ruff formatclean on touched filespyright— 0 errors onapp.py/config.pyRegression coverage
true; envfalserespectedFixes #286