diff --git a/openhands-agent-server/openhands/agent_server/event_service.py b/openhands-agent-server/openhands/agent_server/event_service.py index cbd95be216f..6388c65de43 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 @@ -107,6 +108,7 @@ logger = get_logger(__name__) +_GIT_INIT_LOCK = threading.Lock() class ConversationRunLimitExceeded(RateLimited, RuntimeError): @@ -1262,7 +1264,10 @@ def _prepare_workspace_dir(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 b44fc7cb7e8..dccebd6ef9b 100644 --- a/tests/agent_server/test_event_service.py +++ b/tests/agent_server/test_event_service.py @@ -4020,6 +4020,63 @@ 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._prepare_workspace_dir + + 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, + "_prepare_workspace_dir", + staticmethod(recording_check), + ), + ): + await service.start() + + assert len(threads) == 1 + 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._prepare_workspace_dir(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"),