Skip to content

fix: add sqlite wal mode and timeout#101

Merged
QueryPlanner merged 2 commits into
mainfrom
fix/sqlite-lock-timeout
May 31, 2026
Merged

fix: add sqlite wal mode and timeout#101
QueryPlanner merged 2 commits into
mainfrom
fix/sqlite-lock-timeout

Conversation

@QueryPlanner
Copy link
Copy Markdown
Owner

What

Add WAL mode and connection timeout to SQLite for ADK sessions.

Why

Rapid Telegram messages trigger concurrent database access. When a turn is cancelled mid-flight, its database connection is not fully released before a new turn starts writing, causing a database is locked OperationalError.

How

  • Update build_session_db_kwargs to inject connect_args={"timeout": 15} for SQLite connections
  • Add a SQLAlchemy connect event listener in create_session_service that executes PRAGMA journal_mode=WAL and PRAGMA synchronous=NORMAL
  • Update unit tests in test_adk_runtime.py to assert the new kwargs config

Tests

  • Run uv run pytest tests/test_adk_runtime.py

- Update kwargs to inject timeout for SQLite
- Add SQLAlchemy event listener for WAL mode
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request configures SQLite connection settings for the ADK runtime, introducing a 15-second connection timeout and setting the journal_mode to WAL and synchronous to NORMAL upon connection. It also updates the corresponding unit tests. The reviewer recommended wrapping the SQLite cursor operations in a try...finally block to ensure proper resource cleanup in case of exceptions, and suggested enabling PRAGMA foreign_keys=ON to enforce referential integrity.

Comment thread src/blacki/adk_runtime.py
Comment on lines +119 to +122
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure proper resource cleanup even if an exception occurs during the execution of the PRAGMA statements, wrap the cursor operations in a try...finally block. Additionally, consider enabling PRAGMA foreign_keys=ON to enforce referential integrity constraints, keeping it consistent with how SQLite is configured in src/blacki/storage/sqlite.py.

Suggested change
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.close()
cursor = dbapi_connection.cursor()
try:
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.execute("PRAGMA foreign_keys=ON")
finally:
cursor.close()

- Add mocked tests for SQLite event listener
- Add mocked test for PostgreSQL session instantiation
@QueryPlanner QueryPlanner merged commit c391aa3 into main May 31, 2026
1 check passed
@QueryPlanner QueryPlanner deleted the fix/sqlite-lock-timeout branch May 31, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant