fix(scheduler-sidecar): host AsyncIOScheduler inside a real event loop#104
Merged
Conversation
Prod crash-loop: RuntimeError: no running event loop from apscheduler/schedulers/asyncio.py, raised inside start_scheduler() -> AsyncIOScheduler.start(). It ran fine in-process before because it bound to uvicorn's already-running loop; the sidecar's sync _run() called it with no loop at all, so nothing scheduled in prod and the container restarted forever. DB wait stays synchronous (plain blocking I/O, no loop needed). Everything from start_scheduler() onward now runs inside asyncio.run(_amain()): signal handlers install via loop.add_signal_handler (falling back to signal.signal + call_soon_threadsafe where add_signal_handler isn't implemented), start_scheduler() runs with a loop bound, an asyncio.Event gates shutdown, then stop_scheduler(). core/scheduler.py is untouched. New regression test stubs start_scheduler() to call asyncio.get_running_loop() itself and asserts it doesn't raise — verified against the previous synchronous implementation that this reproduces the exact prod RuntimeError, and passes against the fix. 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.
#103's sidecar crash-loops on prod:
RuntimeError: no running event loop.core/scheduler.pyuses AsyncIOScheduler — inside the gunicorn workers it bound to uvicorn's loop; the standalone entrypoint was synchronous, sostart_scheduler()had no loop to bind. The unit tests had mockedstart_scheduler, hiding exactly this integration contract.scheduler_main.py: DB-wait stays sync, thenasyncio.run(_amain())— signal handlers vialoop.add_signal_handler(signal.signal fallback),start_scheduler()under the running loop, await shutdown event,stop_scheduler(), exit 0.core/scheduler.pyuntouched.start_schedulerstub callsasyncio.get_running_loop()itself — verified to FAIL against the previous sync implementation before the fix.Prod is currently scheduler-dark (workers gated off per #103, sidecar looping) — this restores it.
🤖 Generated with Claude Code