[CONPY-275] mariadb Connector/Python 1.1.8 has memory leak when using python 'with' statementfor connection Created: 2023-11-28  Updated: 2023-12-26  Resolved: 2023-12-12

Status: Closed
Project: MariaDB Connector/Python
Component/s: Generic
Affects Version/s: 1.1.8
Fix Version/s: N/A

Type: Bug Priority: Critical
Reporter: Martin Reinhardt Assignee: Georg Richter
Resolution: Not a Bug Votes: 0
Labels: Connector
Environment:

Rocky8 x86_64


Attachments: File memleak-with.py     File memleak-withfix.py    
Python Version: 3

 Description   

When called as follows, the mariadb-1.1.8 connector has a memory lead due to leaking connection and cursor objects:

with mariadb.Connection( host='localhost',  user='root', password=db_pass, unix_socket=db_sock, connect_timeout=5 ) as db_conn:
    my_cur = db_conn.cursor()
    ...
except mariadb.Error as ex:
    print(str(ex))

Apparently the mariadb.Connection class does not handle (or properly) handle the special _enter_ and _exit_ methods to handle entry and exit from with statements.

Attachments:
memleak-with.py can be run (modify the db_pass to root password) to show the memory growth
memleak-withfix.py contains a wrapper class which implements the _enter_ and _exit_ methods to close the connection and cursor, which does NOT have a memory leak.



 Comments   
Comment by Georg Richter [ 2023-12-01 ]

I don't think that closing the connection should also close the cursor. If a resource is not needed anymore, it should be closed immediately: Either by cursor.close() or using cursor context.

See also:

Comment by Georg Richter [ 2023-12-12 ]

The reason for the memory leak is not in MariaDB Connector/Python, but rather because the cursor was not closed properly.

If the context is exited, only the connection itself is released, but not objects returned by the connection. This is not just the case in MariaDB Connector/Python, but in all modules.

See also PEP 343 - The "with" Statement

Generated at Thu Feb 08 03:31:33 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.