Fix for lockup - create the runtime in a background thread (#4412)

Co-authored-by: Robert Brennan <contact@rbren.io>
This commit is contained in:
tofarr
2024-10-15 17:52:21 -06:00
committed by GitHub
parent 6f2e678028
commit 8a93da51be
6 changed files with 74 additions and 27 deletions

View File

@@ -14,7 +14,7 @@ from pathspec.patterns import GitWildMatchPattern
from openhands.security.options import SecurityAnalyzers
from openhands.server.data_models.feedback import FeedbackDataModel, store_feedback
from openhands.storage import get_file_store
from openhands.utils.async_utils import sync_from_async
from openhands.utils.async_utils import call_sync_from_async
with warnings.catch_warnings():
warnings.simplefilter('ignore')
@@ -211,8 +211,8 @@ async def attach_session(request: Request, call_next):
content={'error': 'Invalid token'},
)
request.state.conversation = session_manager.attach_to_conversation(
request.state.sid
request.state.conversation = await call_sync_from_async(
session_manager.attach_to_conversation, request.state.sid
)
if request.state.conversation is None:
return JSONResponse(
@@ -441,7 +441,9 @@ async def list_files(request: Request, path: str | None = None):
)
runtime: Runtime = request.state.conversation.runtime
file_list = await sync_from_async(runtime.list_files, path)
file_list = await asyncio.create_task(
call_sync_from_async(runtime.list_files, path)
)
if path:
file_list = [os.path.join(path, f) for f in file_list]
@@ -490,7 +492,7 @@ async def select_file(file: str, request: Request):
file = os.path.join(runtime.config.workspace_mount_path_in_sandbox, file)
read_action = FileReadAction(file)
observation = await sync_from_async(runtime.run_action, read_action)
observation = await call_sync_from_async(runtime.run_action, read_action)
if isinstance(observation, FileReadObservation):
content = observation.content
@@ -687,7 +689,7 @@ async def save_file(request: Request):
runtime.config.workspace_mount_path_in_sandbox, file_path
)
write_action = FileWriteAction(file_path, content)
observation = await sync_from_async(runtime.run_action, write_action)
observation = await call_sync_from_async(runtime.run_action, write_action)
if isinstance(observation, FileWriteObservation):
return JSONResponse(

View File

@@ -14,6 +14,7 @@ from openhands.runtime import get_runtime_cls
from openhands.runtime.runtime import Runtime
from openhands.security import SecurityAnalyzer, options
from openhands.storage.files import FileStore
from openhands.utils.async_utils import call_sync_from_async
class AgentSession:
@@ -102,7 +103,13 @@ class AgentSession:
):
self.loop = asyncio.get_running_loop()
self._create_security_analyzer(config.security.security_analyzer)
self._create_runtime(runtime_name, config, agent, status_message_callback)
await call_sync_from_async(
self._create_runtime,
runtime_name=runtime_name,
config=config,
agent=agent,
status_message_callback=status_message_callback,
)
self._create_controller(
agent,
config.security.confirmation_mode,