Skip to content

fix: wrap PythonExecute's blocking multiprocessing in run_in_executor to unblock event loop#1338

Open
programming-pupil wants to merge 1 commit into
FoundationAgents:mainfrom
programming-pupil:fix_01
Open

fix: wrap PythonExecute's blocking multiprocessing in run_in_executor to unblock event loop#1338
programming-pupil wants to merge 1 commit into
FoundationAgents:mainfrom
programming-pupil:fix_01

Conversation

@programming-pupil
Copy link
Copy Markdown

## Summary

PythonExecute.execute() is an async method but internally calls multiprocessing.Process.start() + proc.join(timeout) synchronously. proc.join() is a blocking call that halts the entire asyncio event loop for up to timeout seconds (default 5s), preventing all other concurrent coroutines (LLM calls, web fetches, other tool executions) from making progress.

This PR wraps the blocking multiprocessing work in asyncio.get_event_loop().run_in_executor() so it runs in a thread pool, keeping the event loop free.

## Changes

  • app/tool/python_execute.py: Extracted the synchronous multiprocessing.Manager + Process.join block into a private _run_in_process() method, and called it via run_in_executor(None, ...) from the async execute() method

## Why this matters

In a typical OpenManus session, multiple tools may be active concurrently — the LLM is streaming, web searches are in flight, browser actions are pending. When PythonExecute blocks the event loop for 5 seconds (or longer with a custom timeout), all of these stall completely. This is especially painful when chained with other async tools in a planning flow.

## Test plan

  • python -m py_compile app/tool/python_execute.py passes
  • await PythonExecute().execute(code="print('hello')") returns {"observation": "hello\n", "success": True}
  • await PythonExecute().execute(code="import time; time.sleep(10)", timeout=2) returns timeout error without blocking the event loop for 10s
  • Other async tasks continue to run while PythonExecute is waiting for its subprocess

@programming-pupil
Copy link
Copy Markdown
Author

Hi @appleboy @zhoupeng @Shellmode @cnJasonZ , just a gentle ping on this PR when you have some free cycles. All checks have passed. Please let me know if you need any additional context or changes from my end. Thanks!

@programming-pupil
Copy link
Copy Markdown
Author

@appleboy @zhoupeng @Shellmode @cnJasonZ Hi, I’m a big fan of OpenManus and would love to see it keep evolving. Since it’s been a while since the last update, I wanted to offer my help. Beyond this PR, I’m very much open to helping with issue triaging or ongoing maintenance if you’re looking for a co-maintainer. Let me know your thoughts!

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.

1 participant