Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import threading
import time
import traceback
from collections.abc import Mapping
Expand Down Expand Up @@ -107,6 +108,7 @@


logger = get_logger(__name__)
_GIT_INIT_LOCK = threading.Lock()


class ConversationRunLimitExceeded(RateLimited, RuntimeError):
Expand Down Expand Up @@ -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.
Expand Down
57 changes: 57 additions & 0 deletions tests/agent_server/test_event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading