Bug #100825 CMySQLConnection gives incorrect AttributeError when closed
Submitted: 13 Sep 2020 3:37 Modified: 20 Oct 2020 20:47
Reporter: Jeremy Solbrig Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:8.0.21 OS:Any
Assigned to: CPU Architecture:Any

[13 Sep 2020 3:37] Jeremy Solbrig
Description:
It appears that CMySQLConnection is intended to return a result from most operations, even when it has been closed. For example, the following should be expected to return `None`, but raises an `AttributeError` instead:

```
> cnx = mysql.connector.connect(...)
> cnx.close()
> print(cnx.conneciton_id)

AttributeError                            Traceback (most recent call last)

/usr/local/lib/python3.8/site-packages/mysql/connector/connection_cext.py in connection_id(self)
    292         """MySQL connection ID"""
    293         try:
--> 294             return self._cmysql.thread_id()
    295         except MySQLInterfaceError:
    296             pass  # Just return None
```

How to repeat:
1. Connect to a database
2. Close the connection
3. Attempt to get the connection_id, database name, or likely many other attributes.

```
> cnx = mysql.connector.connect(...)
> cnx.close()
> print(cnx.conneciton_id)

AttributeError                            Traceback (most recent call last)

/usr/local/lib/python3.8/site-packages/mysql/connector/connection_cext.py in connection_id(self)
    292         """MySQL connection ID"""
    293         try:
--> 294             return self._cmysql.thread_id()
    295         except MySQLInterfaceError:
    296             pass  # Just return None
```

Suggested fix:
It looks like the `_cmysql` attribute is being deleted when `close()` is called. I don't think it is supposed to get deleted.
[14 Sep 2020 6:57] MySQL Verification Team
Hello Jeremy,

Thank you for the report and feedback.

regards,
Umesh
[20 Oct 2020 20:47] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.23 release, and here's the proposed changelog entry from the documentation team:

Fixed the AttributeError raised when getting the connection ID from a
closed CMySQLConnection.

Thank you for the bug report.