Bug #104569 Connector/Python >=8.0.22, MySQL <5.7.13 throws exception on close with pooling
Submitted: 9 Aug 2021 3:20 Modified: 2 Mar 2022 18:06
Reporter: Atsushi Suzuki Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:8.0.22, 8.0.23, 8.0.24, 8.0.25, 8.0.26 OS:Any
Assigned to: CPU Architecture:Any

[9 Aug 2021 3:20] Atsushi Suzuki
Description:
Connector/Python raises exception on connection close in the following circumstance:
- Connection/Python >= 8.0.22
- MySQL < 5.7.13
- have C extension
- uses connection pooling

How to repeat:
Run `docker compose run python` with following files.

test.py
```python
import contextlib
import mysql.connector

def main():
    with (
        contextlib.closing(mysql.connector.connect(host="mysql-5.6", user="root", pool_size=1)) as conn,
        contextlib.closing(conn.cursor(dictionary=True)) as cur
    ):
        cur.execute("SELECT 1")
        cur.fetchone()

if __name__ == "__main__":
    assert mysql.connector.HAVE_CEXT
    main()
```

docker-compose.yaml
```yaml
version: "3.7"
services:
  python:
    image: "python:3-slim"
    command: "bash -c 'apt-get update && apt-get install -y default-mysql-client && pip install mysql-connector-python==8.0.26 && python /app/test.py'"
    volumes:
      - ".:/app"
    links: ["mysql-5.6"]
  mysql-5.6:
    image: "mysql:5.6"
    command: "mysqld"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
```

Expected outout: No error

Actual output:
```
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 778, in reset_session
    self.cmd_reset_connection()
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 688, in cmd_reset_connection
    raise errors.NotSupportedError("MySQL version 5.7.2 and "
mysql.connector.errors.NotSupportedError: MySQL version 5.7.2 and earlier does not support COM_RESET_CONNECTION.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/test.py", line 14, in <module>
    main()
  File "/app/test.py", line 10, in main
    cur.fetchone()
  File "/usr/local/lib/python3.9/contextlib.py", line 303, in __exit__
    self.thing.close()
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/pooling.py", line 137, in close
    cnx.reset_session()
  File "/usr/local/lib/python3.9/site-packages/mysql/connector/connection_cext.py", line 785, in reset_session
    raise errors.NotSupportedError(
mysql.connector.errors.NotSupportedError: Reset session is not supported with MySQL server version 5.7.2 or earlier.
```

Suggested fix:
A commit (https://github.com/mysql/mysql-connector-python/commit/3665a4c7ecde6984642502902c28ae4370e...) introduced re-raising NotSupportedError. Removing that code will make same behavior as pure-python connection.

```
index 1b87cf7..5668af2 100644
--- a/lib/mysql/connector/connection_cext.py
+++ b/lib/mysql/connector/connection_cext.py
@@ -781,10 +781,6 @@ class CMySQLConnection(MySQLConnectionAbstract):
                 raise errors.NotSupportedError(
                     "Reset session is not supported with compression for "
                     "MySQL server version 5.7.2 or earlier.")
-            elif self._server_version < (5, 7, 3):
-                raise errors.NotSupportedError(
-                    "Reset session is not supported with MySQL server "
-                    "version 5.7.2 or earlier.")
             else:
                 self.cmd_change_user(self._user, self._password,
                                      self._database, self._charset_id)
```
[9 Aug 2021 6:48] MySQL Verification Team
Hello Suzuki-San,

Thank you for the report and feedback.
This sounds like doc issue to me but intended behavior as part of BUG27489937 fix but I'm unable to locate any change log which confirms version specific note.

regards,
Umesh
[9 Aug 2021 12:11] Atsushi Suzuki
Thank you for replying.

I believe this is implementation issue because
1) It breaks backward compatibility
2) It's not consistent with pure-python implementation behavior (https://github.com/mysql/mysql-connector-python/blob/c4729e84a6849fe584034663af39c0ccde92f...)
3) It's not consistent with other comments (https://github.com/mysql/mysql-connector-python/blob/c4729e84a6849fe584034663af39c0ccde92f... https://github.com/mysql/mysql-connector-python/blob/c4729e84a6849fe584034663af39c0ccde92f...)
[2 Mar 2022 18:06] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.29 release, and here's the proposed changelog entry from the documentation team:

When connecting to MySQL Server versions below 5.7.35, servers failed to
execute change_user commands due to the default collation used by
Connector/Python; a problem that affected connection pooling functionality
by raising an exception when closing a pooled connection. Now
Connector/Python reconnects instead of emitting the exception.

Thank you for the bug report.