Skip to content

Async (asyncio) support for the wrapper β€” a community contribution we'd love your guidance onΒ #1251

Closed

Description

@AhmadMasry

Describe the feature

Hi! πŸ‘‹ First off, thank you for this project β€” it's been a joy to build on. We'd love to contribute an asyncio counterpart of the wrapper, under a new aws_advanced_python_wrapper.aio subpackage, that aims for feature parity with the sync wrapper across the shipped plugins, backed by async DBAPI drivers (psycopg v3 async, aiomysql) and async SQLAlchemy (create_async_engine).

The async support lives in its own new aio subpackage, and we were careful not to remove or rename any existing public symbol β€” current sync code should keep working unchanged. We'll be honest, though: it isn't purely additive. While building it we bumped into a few sync-side things that we thought could be improved and took the liberty of suggesting changes for (detailed below). Those are entirely our subjective opinion, and we'd genuinely welcome you pushing back, reshaping them, or dropping them β€” we're very happy to split them out or leave them for you to decide.

Use Case

The wrapper is currently sync-only (mysql.connector / psycopg sync). What motivated us is the rise of agentic applications: agents and LLM-backed services are overwhelmingly built on asyncio β€” concurrent tool calls, token streaming, many in-flight model/DB requests per user request β€” and that's driven a big expansion in async Python usage (FastAPI, async SQLAlchemy + AsyncSession, aiohttp). Today those stacks can't really use the wrapper's Aurora features (failover, read/write splitting, IAM auth, EFM, …) without blocking the event loop, so teams end up choosing between Aurora-aware connectivity and non-blocking async I/O. We'd love to help remove that trade-off and bring the wrapper's full feature set to the async ecosystem.

Proposed Solution

We've put together a complete, working reference implementation (branch link below). It's been in active development for about 2 months, and we'd be delighted to shape it however suits your roadmap and review process.

Async parity scope:

  • Connectivity/HA: Failover v2, read/write splitting, simple read/write splitting, EFM (host monitoring), Aurora connection tracker, cluster topology monitor (with panic mode), custom endpoint, stale DNS, Aurora initial-connection strategy, blue/green deployment, Limitless, fastest-response strategy, developer plugin.
  • Auth: IAM, AWS Secrets Manager, Federated (SAML) + Okta.
  • Infra: AsyncConnectionProvider / AsyncConnectionProviderManager, AsyncSessionStateService (autocommit/read-only capture+restore across failover/RWS), async IdP factory registry, auto-detection of MultiAz / GlobalAurora host-list providers.
  • Drivers: psycopg v3 async, aiomysql.

SQLAlchemy (sync and async). We actually started this before the library had any SQLAlchemy support β€” and when you shipped SQLAlchemy ORM for MySQL (#1224), we happily folded your change into our branch and filled in the bits our version also covered (your mysql_orm_dialect.py was relocated/rewritten, but the mysql+aws_wrapper_mysqlconnector:// URL still works exactly as before). We also added SQLAlchemy support for PostgreSQL (sync and async), which we don't think is in the library yet β€” hopefully a useful bonus. Async dialects resolve via SQLAlchemy's get_async_dialect_cls hook (chosen by create_async_engine, no URL flag) using the dialect+driver URL convention, and ORM works on the default driver (no use_pure needed).

Validation: 1986 unit tests, plus the full integration suite against real Aurora (PostgreSQL + MySQL) on Python 3.13 and 3.14 β€” failover, RWS, IAM, Secrets Manager, EFM, and SQLAlchemy ORM all passing, with no connection leaks observed.

Other Information

A few sync-side suggestions (very much our opinion πŸ™)

This is the part we're least sure about, so please treat all of it as suggestions, not assertions β€” you know this codebase and its intended behavior far better than we do, and we may simply be wrong about some of these. While building the async side we ran into a handful of sync-side things that we felt could be improved, so we drafted changes for them. They happen to be default-on, which is exactly why we want to flag them loudly rather than slip them in. No public symbol was removed or renamed, but existing sync users would notice:

What we changed Why we thought it'd help Can it be turned off?
Query-timeout / connection-abort / MySQL liveness-ping paths now shut sockets down thread-safely instead of freeing them while a worker is still using them We saw this lead to a cross-thread use-after-free (process-level SIGSEGV) under failover/timeout races β€” but please verify; it may not reproduce in your environments not currently β€” but easy to gate if you'd prefer
errors.py gains PEP 249 mixin bases, so except OperationalError/InterfaceError/NotSupportedError (and SQLAlchemy/Django) classify these errors as expected felt more PEP 249-conformant, but we recognize this changes matching semantics not gated β€” happy to reconsider
Opt-out connection retry at the SQLAlchemy-pool / Django-backend / reader-failover / topology-monitor-login layers (to ride out Aurora's post-failover boot window) smoothed over transient connect rejections in our testing yes β€” connection_retry_max_attempts / _max_backoff_s, default-on
SQLAlchemy pool invalidation on failover so a dead connection isn't handed back avoided a rollback-on-dead-socket error for SA users only affects SQLAlchemy pool connections; no-op otherwise
RWS seeds its role cache on connect, and (default-on) rechecks a picked reader's actual role against topology lag tried to avoid landing on the writer when topology lags a failover recheck via rws_recheck_reader_role (default True); seeding always on
MySQL exception classifier recognizes pymysql-shaped errors + a guard for a TypeError we hit in the classifier needed for the async path; the guard also seemed safer for sync not gated

We'd be more than happy to pull any/all of these into separate, smaller PRs (the socket/SIGSEGV ones could stand alone), tweak the defaults to opt-in, or simply remove them if you'd rather own that area yourselves. Your call entirely. πŸ™‚

A few other notes

  1. PR shape β€” whatever's easiest for you. The async diff is large (~222 files, +44.5k/βˆ’2.7k). We can send it as one PR, stage it (core async β†’ drivers/SQLAlchemy β†’ auth β†’ HA plugins), and/or split out the sync-side bits above. Totally flexible.
  2. aiomysql IAM TLS posture. aiomysql doesn't auto-negotiate TLS, so the async IAM path builds the SSL context explicitly but matches the sync driver's default β€” encrypt the cleartext token, don't verify the cert by default, with verification opt-in via ssl_ca (no CA bundled). Flagging in case you'd prefer verify-by-default upstream.
  3. EFM v2 on async MySQL. Sync MySQL dropped EFM v2 (chore: add initial connection to the default plugin list and remove efm2 for mysqlΒ #1242 β€” couldn't abort a connection). The async aiomysql path keeps it, since asyncio cancellation makes monitor-driven teardown feasible β€” but we'd defer to you if you'd rather keep parity with the sync decision.
  4. Built with AI assistance. In the interest of full transparency: this was developed with Claude (AI) assistance over the ~2 months. Glad to share the project's CLAUDE.md (the guardrails/conventions we worked under) if it's useful for your review.
  5. We've tentatively targeted 3.1.0 in the CHANGELOG [Unreleased] section, but that's just a placeholder β€” whatever you prefer.

Reference branch: AhmadMasry/aws-advanced-python-wrapper @ feat/async-parity β€” or view the full diff vs main (no PR opened yet β€” happy to open one in whatever shape you prefer).

Thank you so much for reading this far, and for considering it! We're not attached to any particular approach here β€” we'd love to do this with you, in whatever shape works best for the project. Happy to hop on anything, answer questions, or rework as needed. πŸ™

Acknowledgements

  • I may be able to implement this feature request (reference implementation is complete and validated β€” and we'd love to collaborate on shaping it)
  • This feature might incur a breaking change (no public symbol removed/renamed; a few default-on sync behavior changes we've flagged above for your review)
Field Value
Wrapper version 3.1.0 (branch)
Python version 3.13 & 3.14
OS Amazon Linux 2023 (Graviton) + macOS

Activity

  1. karenc-bq commented on Jun 17, 2026

    @karenc-bq
    Contributor

    Hi @AhmadMasry, thank you for taking an interest in our project!

    We appreciate your contribution and we would be more than happy to collaborate with you on the proposed changes.

    In terms of PR shape, could you split out the sync-side bits you mentioned above, and then stage the rest? That will allow the team to better look through and roll out your changes.

    Thank you for flagging the other items as well, we'll look further into those when reviewing the code.

    Thank you!

  2. AhmadMasry commented on Jun 18, 2026

    @AhmadMasry
    ContributorAuthor

    Hi @karenc-bq , you really made my day by considering to look into this.
    Here you go, this is phase 1 (sync) PR.
    Please inform me if you want me to close it and send a compare only URL.
    Please note that the PR also includes python 3.14 support, some safe dependencies bump.
    Thanks

  3. added
    enhancement
    on Jun 25, 2026
  4. AhmadMasry commented on Jul 2, 2026

    @AhmadMasry
    ContributorAuthor

    Thank you @karenc-bq for your help and valuable review.
    Before I start with the Async PR, can you have a look at this sync part followup PR #1255.
    It has a suggested implementation that completes the abort-and-drain mechanism in the failover plugins' timeout paths and makes the connection tracker invalidate (rather than re-pool) internally-pooled connections after writer failover.
    And I want your green light after to start working on the Async PR.
    Thank you again.

  5. AhmadMasry commented on Jul 3, 2026

    @AhmadMasry
    ContributorAuthor

    @karenc-bq , while doing parity an feature comparison between sync and async, we found a couple of fixes that I really appreciate your feedback on in #1256. they are not a blocker for the async PR, but if you find them valid fixes, I prefer have them merged before opening the async PR.
    Thanks

  6. AhmadMasry commented on Jul 6, 2026

    @AhmadMasry
    ContributorAuthor

    Hi @karenc-bq
    The async PR is ready #1257 .
    I did another integration testing and I faced the sync flakiness I told you before, please find the attached sync-flakiness-report.md for the details. I did not do anything to fix, but only documented them, I also approached the full logs of the runs used to generate the reports.
    It is worth mentioning that I used a m8g.xlarge EC2 instance in us-east-2 with al2023 using the latest version, and kernel 6.18.35-68.129.amzn2023.aarch64 and the java version is

    openjdk 17.0.19 2026-04-21 LTS
    OpenJDK Runtime Environment Corretto-17.0.19.10.1 (build 17.0.19+10-LTS)
    OpenJDK 64-Bit Server VM Corretto-17.0.19.10.1 (build 17.0.19+10-LTS, mixed mode, sharing)
    
    

    And we also used the bundled docker version Docker version 25.0.14, build 0bab007

    Kindly see the attachments and do not hesitate to ping me for any modification.
    Thanks

    sync-flakiness-report.md
    phase2-sync-failure-report.tar.gz

  7. AhmadMasry commented on Jul 9, 2026

    @AhmadMasry
    ContributorAuthor

    Hi @karenc-bq
    Is there any blocker for the above PRs?
    Thanks in advance

  8. karenc-bq commented on Jul 9, 2026

    @karenc-bq
    Contributor

    Hi @AhmadMasry, thank you for the PRs and the attached reports! I am currently reviewing them.

  9. AhmadMasry commented on Jul 10, 2026

    @AhmadMasry
    ContributorAuthor

    Really appreciate you help.
    Thanks

  10. AhmadMasry commented on Jul 30, 2026

    @AhmadMasry
    ContributorAuthor

    @karenc-bq thank you for the AWSome work here. I really appreciate you help in this.
    When is it expected to have a new version of the library?
    Thank you very much.

  11. added
    pending release
    on Jul 30, 2026
  12. karenc-bq commented on Aug 27, 2026

    @karenc-bq
    Contributor

    Hi @AhmadMasry, thank you for your contributions to the wrapper! asyncio support is now officially available in v3.1.0. We really appreciate you carrying this through over several months, contributing features and bug fixes to make the wrapper more robust. Your reports, PR descriptions and comments have been consistently detailed, making the review process so much easier.

    We hope to collaborate with you more in the future. Please let us know if you encounter any issues.

  13. AhmadMasry commented on Sep 6, 2026

    @AhmadMasry
    ContributorAuthor

    Your efforts are very much appreciated @karenc-bq
    Thank you for your help on making this happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpending release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions