Skip to content

asyncpg and psycopg async connectors generate tokens synchronously on the event loop #645

Description

@cristiticu

Related to my other issue (#644) about client reuse, but this one stands on its own.

Both async connect paths call the synchronous token pipeline directly from the running loop:

  • aurora_dsql_asyncpg/connector.py line 118, ConnectionUtilities.parse_properties_and_set_token(...) inside async def connect
  • aurora_dsql_psycopg/connection_class.py line 104, same call in the async connect

That call does blocking work: boto3 session/client construction (disk I/O, JSON parsing) and credential resolution, which can be a network call. While it runs the whole event loop is stopped, every coroutine in the process, not just the one opening a connection.

On my machine it stalls the loop for roughly 100 ms per token:

import asyncio, os, time

os.environ["AWS_ACCESS_KEY_ID"] = "KEY HERE"
os.environ["AWS_SECRET_ACCESS_KEY"] = "KEY HERE"

from dsql_core.token_manager import TokenManager

params = {"host": "example.dsql.eu-west-1.on.aws", "region": "eu-west-1", "user": "admin",
          "profile": None, "token_duration_secs": None, "custom_credentials_provider": None}

async def main():
    lags = []

    async def ticker():
        while True:
            t0 = time.perf_counter()
            await asyncio.sleep(0.01)
            lags.append((time.perf_counter() - t0 - 0.01) * 1000)

    t = asyncio.create_task(ticker())
    await asyncio.sleep(0.1)
    for _ in range(5):
        TokenManager.get_token(params)
        await asyncio.sleep(0.02)
    t.cancel()
    print(f"max loop stall: {max(lags):.0f} ms")

asyncio.run(main())

Wrapping the token step in asyncio.to_thread(...) in the two async connectors would fix it. If the client also gets reused (other issue), the blocking section shrinks to almost nothing in steady state, but the thread hop still seems right for the occasional credential refresh.

Version: latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions