From 95963633d4bcd80d8b669df3625ef62cfb8cff48 Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Sun, 15 Nov 2026 20:04:25 +0100 Subject: [PATCH 1/2] perf(agent-server): check the workspace repository off the event loop EventService.start ran git rev-parse and, for a new workspace, git init as subprocesses on the event loop, for every conversation start. On macOS, where starting a process is slow, that froze every request and socket for about 60 ms per create. Run the check with asyncio.to_thread. On a Mac the worst /alive latency during a warm create went from 59-71 ms to 5-18 ms; the create itself is unchanged. --- .../openhands/agent_server/event_service.py | 2 +- tests/agent_server/test_event_service.py | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/openhands-agent-server/openhands/agent_server/event_service.py b/openhands-agent-server/openhands/agent_server/event_service.py index 089ad03c586..474af31612c 100644 --- a/openhands-agent-server/openhands/agent_server/event_service.py +++ b/openhands-agent-server/openhands/agent_server/event_service.py @@ -1295,7 +1295,7 @@ async def start(self): assert isinstance(workspace, LocalWorkspace) working_dir = Path(workspace.working_dir) working_dir.mkdir(parents=True, exist_ok=True) - self._ensure_workspace_is_git_repo(working_dir) + await asyncio.to_thread(self._ensure_workspace_is_git_repo, working_dir) # base_state.json is the single source of truth for the agent. On resume # (base_state exists) pass ``agent=None`` so LocalConversation keeps the # persisted agent. On a new conversation the creating caller supplied the diff --git a/tests/agent_server/test_event_service.py b/tests/agent_server/test_event_service.py index b44fc7cb7e8..de51ba46211 100644 --- a/tests/agent_server/test_event_service.py +++ b/tests/agent_server/test_event_service.py @@ -4020,6 +4020,39 @@ async def test_event_service_creates_lease_with_custom_ttl(tmp_path: Path) -> No assert (tmp_path / stored.id.hex / LEASE_FILE_NAME).exists() +@pytest.mark.asyncio +async def test_event_service_checks_the_workspace_repo_off_the_event_loop( + tmp_path: Path, +) -> None: + stored = _make_stored(tmp_path) + service = EventService( + stored=stored, agent=_sample_agent(), conversations_dir=tmp_path + ) + loop_thread = threading.get_ident() + threads: list[int] = [] + real_check = EventService._ensure_workspace_is_git_repo + + def recording_check(working_dir: Path) -> None: + threads.append(threading.get_ident()) + real_check(working_dir) + + with ( + patch( + "openhands.agent_server.event_service.LocalConversation", + return_value=_make_mock_conv(), + ), + patch.object( + EventService, + "_ensure_workspace_is_git_repo", + staticmethod(recording_check), + ), + ): + await service.start() + + assert len(threads) == 1 + assert threads[0] != loop_thread + + @pytest.mark.asyncio @pytest.mark.parametrize( ("condenser", "message"), From d08e9af0770a7072cda3d9f3d2319dafb0026c48 Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Mon, 16 Nov 2026 11:16:56 +0100 Subject: [PATCH 2/2] fix: run one git init at a time Off the event loop, two conversations starting on the same new directory ran git init at once; the losers failed on .git/config.lock and logged an error, although the directory ended up a repository. --- .../openhands/agent_server/event_service.py | 7 +++++- tests/agent_server/test_event_service.py | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/openhands-agent-server/openhands/agent_server/event_service.py b/openhands-agent-server/openhands/agent_server/event_service.py index 474af31612c..fb0804f4f3e 100644 --- a/openhands-agent-server/openhands/agent_server/event_service.py +++ b/openhands-agent-server/openhands/agent_server/event_service.py @@ -1,4 +1,5 @@ import asyncio +import threading import time import traceback from collections.abc import Mapping @@ -106,6 +107,7 @@ logger = get_logger(__name__) +_GIT_INIT_LOCK = threading.Lock() class ConversationRunLimitExceeded(RateLimited, RuntimeError): @@ -1266,7 +1268,10 @@ def _ensure_workspace_is_git_repo(working_dir: Path) -> None: ) try: - run_git_command(["git", "init"], working_dir) + # Conversations starting on the same new directory would otherwise + # race on .git/config.lock; a repeated `git init` is harmless. + with _GIT_INIT_LOCK: + run_git_command(["git", "init"], working_dir) except GitCommandError as e: # Don't block conversation startup if git is missing or init # fails — the git router is defensive and will return [] anyway. diff --git a/tests/agent_server/test_event_service.py b/tests/agent_server/test_event_service.py index de51ba46211..859705d68a9 100644 --- a/tests/agent_server/test_event_service.py +++ b/tests/agent_server/test_event_service.py @@ -4053,6 +4053,30 @@ def recording_check(working_dir: Path) -> None: assert threads[0] != loop_thread +def test_concurrent_repo_checks_on_a_new_directory_all_succeed( + tmp_path: Path, caplog: pytest.LogCaptureFixture +) -> None: + directories = [tmp_path / f"workspace-{i}" for i in range(10)] + for directory in directories: + directory.mkdir() + + def check_all(barrier: threading.Barrier) -> None: + barrier.wait() + for directory in directories: + EventService._ensure_workspace_is_git_repo(directory) + + barrier = threading.Barrier(8) + workers = [threading.Thread(target=check_all, args=(barrier,)) for _ in range(8)] + with caplog.at_level(logging.WARNING): + for worker in workers: + worker.start() + for worker in workers: + worker.join() + + assert "Failed to initialize git repository" not in caplog.text + assert all((directory / ".git").is_dir() for directory in directories) + + @pytest.mark.asyncio @pytest.mark.parametrize( ("condenser", "message"),