Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
96 changes: 64 additions & 32 deletions src/agents/extensions/memory/sqlalchemy_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

import asyncio
import json
from typing import Any
import threading
from typing import Any, ClassVar

from sqlalchemy import (
TIMESTAMP,
Expand All @@ -43,6 +44,7 @@
text as sql_text,
update,
)
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine

from ...items import TResponseInputItem
Expand All @@ -53,11 +55,29 @@
class SQLAlchemySession(SessionABC):
"""SQLAlchemy implementation of :pyclass:`agents.memory.session.Session`."""

_table_init_locks: ClassVar[dict[tuple[str, str, str], asyncio.Lock]] = {}
_table_init_locks_guard: ClassVar[threading.Lock] = threading.Lock()
Comment on lines +58 to +59
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard first-time table creation across worker processes

This lock table only exists inside one Python process. In a common deployment where several workers/processes call SQLAlchemySession.from_url(..., create_tables=True) against the same empty database, each process will still reach metadata.create_all() independently, so the original table already exists/dropped first write race can still happen on first access. The new tests only exercise threads/event loops within a single process, so this fix remains incomplete for multi-worker servers.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex For production deployments, create_tables=True is not recommended and this option is useful mainly for local development or demo purposes. So, I think this is fine for now. Can you review again to see if there are any other things to mention?

_metadata: MetaData
_sessions: Table
_messages: Table
session_settings: SessionSettings | None = None

@classmethod
def _get_table_init_lock(
cls, engine: AsyncEngine, sessions_table: str, messages_table: str
) -> asyncio.Lock:
lock_key = (
engine.url.render_as_string(hide_password=True),
sessions_table,
messages_table,
)
with cls._table_init_locks_guard:
lock = cls._table_init_locks.get(lock_key)
if lock is None:
lock = asyncio.Lock()
Comment thread
seratch marked this conversation as resolved.
Outdated
cls._table_init_locks[lock_key] = lock
return lock

def __init__(
self,
session_id: str,
Expand Down Expand Up @@ -86,6 +106,7 @@ def __init__(
self.session_settings = session_settings or SessionSettings()
self._engine = engine
self._lock = asyncio.Lock()
self._init_lock = self._get_table_init_lock(engine, sessions_table, messages_table)
Comment thread
seratch marked this conversation as resolved.
Outdated

self._metadata = MetaData()
self._sessions = Table(
Expand Down Expand Up @@ -182,7 +203,13 @@ async def _deserialize_item(self, item: str) -> TResponseInputItem:
# ------------------------------------------------------------------
async def _ensure_tables(self) -> None:
"""Ensure tables are created before any database operations."""
if self._create_tables:
if not self._create_tables:
return

async with self._init_lock:
if not self._create_tables:
return

async with self._engine.begin() as conn:
await conn.run_sync(self._metadata.create_all)
self._create_tables = False # Only create once
Expand Down Expand Up @@ -248,40 +275,45 @@ async def add_items(self, items: list[TResponseInputItem]) -> None:
if not items:
return

await self._ensure_tables()
payload = [
{
"session_id": self.session_id,
"message_data": await self._serialize_item(item),
}
for item in items
]

async with self._session_factory() as sess:
async with sess.begin():
# Ensure the parent session row exists - use merge for cross-DB compatibility
# Check if session exists
existing = await sess.execute(
select(self._sessions.c.session_id).where(
self._sessions.c.session_id == self.session_id
async with self._lock:
Comment thread
seratch marked this conversation as resolved.
Outdated
await self._ensure_tables()
payload = [
{
"session_id": self.session_id,
"message_data": await self._serialize_item(item),
}
for item in items
]

async with self._session_factory() as sess:
async with sess.begin():
# Avoid check-then-insert races on the first write while keeping
# the common path free of avoidable integrity exceptions.
existing = await sess.execute(
select(self._sessions.c.session_id).where(
self._sessions.c.session_id == self.session_id
)
)
)
if not existing.scalar_one_or_none():
# Session doesn't exist, create it
if not existing.scalar_one_or_none():
try:
async with sess.begin_nested():
await sess.execute(
insert(self._sessions).values({"session_id": self.session_id})
)
except IntegrityError:
# Another concurrent writer created the parent row first.
pass

# Insert messages in bulk
await sess.execute(insert(self._messages), payload)

# Touch updated_at column
await sess.execute(
insert(self._sessions).values({"session_id": self.session_id})
update(self._sessions)
.where(self._sessions.c.session_id == self.session_id)
.values(updated_at=sql_text("CURRENT_TIMESTAMP"))
)

# Insert messages in bulk
await sess.execute(insert(self._messages), payload)

# Touch updated_at column
await sess.execute(
update(self._sessions)
.where(self._sessions.c.session_id == self.session_id)
.values(updated_at=sql_text("CURRENT_TIMESTAMP"))
)

async def pop_item(self) -> TResponseInputItem | None:
"""Remove and return the most recent item from the session.

Expand Down
121 changes: 121 additions & 0 deletions tests/extensions/memory/test_sqlalchemy_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import json
from collections.abc import Iterable, Sequence
from contextlib import asynccontextmanager
Expand Down Expand Up @@ -203,6 +204,126 @@ async def test_add_empty_items_list():
assert len(items_after_add) == 0


async def test_add_items_concurrent_first_access_with_create_tables(tmp_path):
"""Concurrent first writes should not race table creation or drop items."""
db_url = f"sqlite+aiosqlite:///{tmp_path / 'concurrent_first_access.db'}"
session = SQLAlchemySession.from_url(
"concurrent_first_access",
url=db_url,
create_tables=True,
)
submitted = [f"msg-{i}" for i in range(25)]

async def worker(content: str) -> None:
await session.add_items([{"role": "user", "content": content}])

results = await asyncio.gather(
*(worker(content) for content in submitted),
return_exceptions=True,
)

assert [result for result in results if isinstance(result, Exception)] == []

stored = await session.get_items()
assert len(stored) == len(submitted)
stored_contents: list[str] = []
for item in stored:
content = item.get("content")
assert isinstance(content, str)
stored_contents.append(content)
assert sorted(stored_contents) == sorted(submitted)


async def test_add_items_concurrent_first_write_after_tables_exist(tmp_path):
"""Concurrent first writes should not race parent session creation."""
db_url = f"sqlite+aiosqlite:///{tmp_path / 'concurrent_first_write.db'}"
setup_session = SQLAlchemySession.from_url(
"concurrent_first_write",
url=db_url,
create_tables=True,
)
await setup_session.get_items()

session = SQLAlchemySession.from_url(
"concurrent_first_write",
url=db_url,
create_tables=False,
)
submitted = [f"msg-{i}" for i in range(25)]

async def worker(content: str) -> None:
await session.add_items([{"role": "user", "content": content}])

results = await asyncio.gather(
*(worker(content) for content in submitted),
return_exceptions=True,
)

assert [result for result in results if isinstance(result, Exception)] == []

stored = await session.get_items()
assert len(stored) == len(submitted)
stored_contents: list[str] = []
for item in stored:
content = item.get("content")
assert isinstance(content, str)
stored_contents.append(content)
assert sorted(stored_contents) == sorted(submitted)


async def test_add_items_concurrent_first_access_across_sessions_with_shared_engine(tmp_path):
"""Concurrent first writes should not race table creation across session instances."""
db_url = f"sqlite+aiosqlite:///{tmp_path / 'concurrent_shared_engine.db'}"
engine = create_async_engine(db_url)
try:
session_a = SQLAlchemySession("shared_engine_a", engine=engine, create_tables=True)
session_b = SQLAlchemySession("shared_engine_b", engine=engine, create_tables=True)

results = await asyncio.gather(
session_a.add_items([{"role": "user", "content": "one"}]),
session_b.add_items([{"role": "user", "content": "two"}]),
return_exceptions=True,
)

assert [result for result in results if isinstance(result, Exception)] == []

stored_a = await session_a.get_items()
assert len(stored_a) == 1
assert stored_a[0].get("content") == "one"

stored_b = await session_b.get_items()
assert len(stored_b) == 1
assert stored_b[0].get("content") == "two"
finally:
await engine.dispose()


async def test_add_items_concurrent_first_access_across_from_url_sessions(tmp_path):
"""Concurrent first writes should not race table creation across from_url sessions."""
db_url = f"sqlite+aiosqlite:///{tmp_path / 'concurrent_from_url.db'}"
session_a = SQLAlchemySession.from_url("from_url_a", url=db_url, create_tables=True)
session_b = SQLAlchemySession.from_url("from_url_b", url=db_url, create_tables=True)
try:
results = await asyncio.gather(
session_a.add_items([{"role": "user", "content": "one"}]),
session_b.add_items([{"role": "user", "content": "two"}]),
return_exceptions=True,
)

assert [result for result in results if isinstance(result, Exception)] == []

stored_a = await session_a.get_items()
assert len(stored_a) == 1
assert stored_a[0].get("content") == "one"

stored_b = await session_b.get_items()
assert len(stored_b) == 1
assert stored_b[0].get("content") == "two"
finally:
await session_a.engine.dispose()
await session_b.engine.dispose()


async def test_get_items_same_timestamp_consistent_order():
"""Test that items with identical timestamps keep insertion order."""
session_id = "same_timestamp_test"
Expand Down