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
5 changes: 4 additions & 1 deletion src/opencode_a2a/execution/session_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import weakref

from ..invocation import call_with_supported_kwargs
from ..server.state_store import MemorySessionStateRepository, SessionStateRepository
Expand All @@ -24,7 +25,9 @@ def __init__(
)
self._lock = asyncio.Lock()
self._inflight_session_creates: dict[tuple[str, str], asyncio.Task[str]] = {}
self._session_locks: dict[str, asyncio.Lock] = {}
self._session_locks: weakref.WeakValueDictionary[str, asyncio.Lock] = (
weakref.WeakValueDictionary()
)

async def get_or_create_session(
self,
Expand Down
33 changes: 33 additions & 0 deletions tests/execution/test_session_lock_lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import gc
import weakref
from unittest.mock import AsyncMock

import pytest

from opencode_a2a.execution.session_manager import SessionManager
from opencode_a2a.opencode_upstream_client import OpencodeUpstreamClient


@pytest.mark.asyncio
async def test_session_manager_reuses_live_lock_for_same_session() -> None:
manager = SessionManager(client=AsyncMock(spec=OpencodeUpstreamClient))

lock1 = await manager.get_session_lock("session-1")
lock2 = await manager.get_session_lock("session-1")

assert lock1 is lock2


@pytest.mark.asyncio
async def test_session_manager_does_not_strongly_retain_idle_locks() -> None:
manager = SessionManager(client=AsyncMock(spec=OpencodeUpstreamClient))

lock = await manager.get_session_lock("session-1")
lock_ref = weakref.ref(lock)
assert "session-1" in manager._session_locks

del lock
gc.collect()

assert lock_ref() is None
assert "session-1" not in manager._session_locks