fix: run scheduled jobs in background tasks to prevent bus blocking#177
Open
sakhnenkoff wants to merge 1 commit intoRichardAtCT:mainfrom
Open
fix: run scheduled jobs in background tasks to prevent bus blocking#177sakhnenkoff wants to merge 1 commit intoRichardAtCT:mainfrom
sakhnenkoff wants to merge 1 commit intoRichardAtCT:mainfrom
Conversation
AgentHandler.handle_scheduled now dispatches work via asyncio.create_task and returns immediately, preventing long Claude executions from blocking the event bus and causing missed heartbeats. A semaphore (max 2) caps concurrent Claude executions. handle_webhook remains unchanged. Co-Authored-By: Claude Opus 4.6 (1M context) <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.
Summary
Fixes #174.
AgentHandler.handle_scheduledcurrently awaitsclaude.run_command()directly, which blocks the event bus for the entire duration of a Claude execution (often 5-10+ minutes). This causes subsequent scheduled jobs to be delayed or missed entirely.Changes:
handle_schedulednow dispatches work viaasyncio.create_task()and returns immediately_run_scheduledmethod runs the actual Claude work under anasyncio.Semaphore(2)to cap concurrent executions_task_donecallback cleans up finished tasks and logs errors (prevents "Task exception was never retrieved" warnings)handle_webhookis unchanged — webhooks are fast and blocking is fineWhy not modify EventBus instead?
Changing the bus dispatcher to run events concurrently would break ordering guarantees, risk fire-and-forget exception leaks, and require verifying all handlers for concurrent safety. Scoping the fix to
AgentHandler.handle_scheduledis minimal and safe.Tests
4 new tests in
tests/unit/test_events/test_concurrent_scheduled.py:test_handle_scheduled_returns_immediately— verifies dispatch completes within 1s even when Claude would take 10stest_scheduled_semaphore_limits_concurrency— verifies semaphore caps at 2test_scheduled_task_errors_are_logged_not_raised— confirms background errors are logged, not propagatedtest_webhook_handler_still_blocks— verifies webhook handling still awaits directlyAll 538 existing tests continue to pass.