Skip to content

fix(memory): prevent close() race and enforce closed state in AsyncSQLiteSession #3983

Description

@hsusul

Please read this first

  • Have you read the docs? Yes
  • Have you searched for related issues? Yes

Describe the bug

In AsyncSQLiteSession, calling close() concurrently or repeatedly causes an AttributeError: 'NoneType' object has no attribute 'close'.
This occurs because close() checks if self._connection is None: outside self._lock without re-verifying inside async with self._lock:. When two concurrent tasks invoke close(), both pass the initial None check; the first task acquires self._lock, closes self._connection, and sets it to None. When the second task subsequently acquires self._lock, it attempts await self._connection.close() on None.

In addition, AsyncSQLiteSession lacks a self._closed state flag (unlike SQLiteSession). Consequently, calling get_items(), add_items(), pop_item(), or clear_session() after close() re-initializes a new database connection instead of raising RuntimeError("AsyncSQLiteSession is closed").

Debug information

  • Agents SDK version: main (0.19.0+)
  • Python version: Python 3.13.5
  • Operating System: macOS

Repro steps

import asyncio
from agents.extensions.memory import AsyncSQLiteSession

async def main():
    session = AsyncSQLiteSession("test_close_race")
    await session.add_items([{"role": "user", "content": "hi"}])
    # Concurrent close calls raise AttributeError
    await asyncio.gather(session.close(), session.close())

asyncio.run(main())

Expected behavior

AsyncSQLiteSession.close() should be thread-safe, task-safe, and idempotent (subsequent and concurrent calls to close() should be safe no-ops).
Operations invoked on a closed AsyncSQLiteSession should raise RuntimeError("AsyncSQLiteSession is closed"), matching SQLiteSession behavior.

I intend to submit a pull request fixing this issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions