Skip to content

fix: use run_in_executor in LLMAgent.astep() to avoid blocking the ev…#141

Open
khansalman12 wants to merge 1 commit intomesa:mainfrom
khansalman12:fix/astep-blocking-event-loop
Open

fix: use run_in_executor in LLMAgent.astep() to avoid blocking the ev…#141
khansalman12 wants to merge 1 commit intomesa:mainfrom
khansalman12:fix/astep-blocking-event-loop

Conversation

@khansalman12
Copy link

Summary

astep() in LLMAgent was calling self.step() synchronously when a subclass only defines step(), blocking the event loop during parallel stepping.

Bug / Issue

Fixes #140

When astep() falls back to self.step() directly, any blocking LLM API call inside step() freezes the entire asyncio event loop. Other agents cannot run until the current one completes, making parallel execution no faster than sequential.

Implementation

In llm_agent.py, replaced the direct blocking call:

# Before
self.step()

# After
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, self.step)

run_in_executor offloads the sync call to a thread pool, keeping the event loop free.  
Using asyncio.get_running_loop() (not the deprecated get_event_loop()) since `astep()` is always called from within an async context.

Testing
-------
Added 3 regression tests in `test_llm_agent.py`:

1. `test_astep_fallback_runs_sync_step_without_blocking`
   - Verifies `step()` is called via executor fallback.

2. `test_astep_fallback_does_not_block_concurrent_agents`
   - Verifies both agents complete with `asyncio.gather`.

3. `test_astep_with_native_astep_not_affected`
   - Verifies agents with their own `astep()` are unaffected.

All 3 tests pass.

Additional Notes
----------------
None.

@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 39d93dab-ea88-4c0e-812b-738be14873d4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.89%. Comparing base (a8479d3) to head (63b305c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #141      +/-   ##
==========================================
+ Coverage   89.26%   89.89%   +0.62%     
==========================================
  Files          19       19              
  Lines        1472     1474       +2     
==========================================
+ Hits         1314     1325      +11     
+ Misses        158      149       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@khansalman12 khansalman12 force-pushed the fix/astep-blocking-event-loop branch from 7b28164 to 4a66664 Compare March 4, 2026 13:39
@khansalman12 khansalman12 force-pushed the fix/astep-blocking-event-loop branch from 4a66664 to 63b305c Compare March 4, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: LLMAgent.astep() blocks the event loop when subclass only defines step()

1 participant