Skip to content

connection.close() returns None when called from async function - await fails #1001

@GameDevEng

Description

@GameDevEng

Describe the bug

see code to reproduce -

  1. define async function
  2. call connection = await aiomysql.connect(...)
  3. execute some operation (SELECT 1)
  4. call await connection.close()
  5. app crashed

To Reproduce

Execute attached python script

Expected behavior

connection.close() should be returning a waitable object for await and not None

Logs/tracebacks

python test-aiomysql-close
2025-07-31 10:53:50,009 INFO : Attempting to connect to maria:3306...
2025-07-31 10:53:50,017 INFO : Successfully connected.
2025-07-31 10:53:50,018 INFO : Dummy query executed successfully.
2025-07-31 10:53:50,018 INFO : Type of conn.close() result (before await): <class 'NoneType'>
2025-07-31 10:53:50,018 INFO : Value of conn.close() result (before await): None
2025-07-31 10:53:50,018 INFO : Attempting to await conn.close()...
2025-07-31 10:53:50,018 ERROR : An error occurred: object NoneType can't be used in 'await' expression
Traceback (most recent call last):
  File "/home/phil/temp/test-aiomysql-close", line 42, in test_close_behavior
    await close_awaitable
TypeError: object NoneType can't be used in 'await' expression

Python Version

$ python --version
Python 3.12.3

aiomysql Version

$ python -m pip show aiomysql
Name: aiomysql
Version: 0.2.0
Summary: MySQL driver for asyncio.
Home-page: https://github.com/aio-libs/aiomysql
Author: Nikolay Novik
Author-email: nickolainovik@gmail.com
License: MIT
Location: /home/phil/dev/archiver/venv/lib/python3.12/site-packages
Requires: PyMySQL
Required-by: async-sqlalchemy

PyMySQL Version

$ python -m pip show PyMySQL
Name: PyMySQL
Version: 1.1.1
Summary: Pure Python MySQL Driver
Home-page: 
Author: 
Author-email: Inada Naoki <songofacandy@gmail.com>, Yutaka Matsubara <yutaka.matsubara@gmail.com>
License: MIT License
Location: /home/phil/dev/archiver/venv/lib/python3.12/site-packages
Requires: 
Required-by: aiomysql

SQLAlchemy Version

$ python -m pip show sqlalchemy
Name: SQLAlchemy
Version: 2.0.40
Summary: Database Abstraction Library
Home-page: https://www.sqlalchemy.org
Author: Mike Bayer
Author-email: mike_mp@zzzcomputing.com
License: MIT
Location: /home/phil/dev/archiver/venv/lib/python3.12/site-packages
Requires: greenlet, typing-extensions
Required-by: async-sqlalchemy

OS

cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo

Database type and version

SELECT VERSION();
'10.11.11-MariaDB-0ubuntu0.24.04.2'

Additional context

Add code to application to ensure the db is created before sqlalchemy creates tables

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions