Skip to content

Commit 9bde4ba

Browse files
Eagerly establish TCP connection in async dialect
Call await_only(raw_conn.connect()) during connect() so connection errors surface at connect-time rather than on the first query, matching the pattern used by SQLAlchemy's aiosqlite dialect. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3d52c64 commit 9bde4ba

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/sqlalchemydqlite/aio.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,13 @@ def import_dbapi(cls) -> Any:
146146
return aio
147147

148148
def connect(self, *cargs: Any, **cparams: Any) -> Any:
149-
"""Create and wrap an async connection."""
149+
"""Create and wrap an async connection.
150+
151+
Eagerly establishes the TCP connection so errors surface at
152+
connect-time rather than on the first query.
153+
"""
150154
raw_conn = self.loaded_dbapi.connect(*cargs, **cparams)
155+
await_only(raw_conn.connect())
151156
return AsyncAdaptedConnection(raw_conn)
152157

153158
def get_driver_connection(self, connection: Any) -> Any:

tests/test_dialect.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,36 @@ def test_sync_dialect_does_not_use_nullpool(self) -> None:
392392
)
393393

394394

395+
class TestAsyncConnect:
396+
def test_connect_calls_await_only_on_raw_connect(self) -> None:
397+
"""Async dialect connect() should eagerly establish the TCP connection."""
398+
import ast
399+
import inspect
400+
import textwrap
401+
402+
from sqlalchemydqlite.aio import DqliteDialect_aio
403+
404+
source = textwrap.dedent(inspect.getsource(DqliteDialect_aio.connect))
405+
tree = ast.parse(source)
406+
407+
# Look for await_only(raw_conn.connect()) or similar eager connect call
408+
has_eager_connect = False
409+
for node in ast.walk(tree):
410+
if isinstance(node, ast.Call):
411+
func = node.func
412+
if isinstance(func, ast.Name) and func.id == "await_only" and node.args:
413+
arg = node.args[0]
414+
if isinstance(arg, ast.Call):
415+
inner = arg.func
416+
if isinstance(inner, ast.Attribute) and inner.attr == "connect":
417+
has_eager_connect = True
418+
419+
assert has_eager_connect, (
420+
"DqliteDialect_aio.connect() should eagerly establish TCP with "
421+
"await_only(raw_conn.connect())"
422+
)
423+
424+
395425
class TestURLParsing:
396426
def test_parse_basic_url(self) -> None:
397427
url = URL.create("dqlite", host="localhost", port=9001, database="test")

0 commit comments

Comments
 (0)