Bug #115082 connection_cext has a memory leak in the python mysql-connector
Submitted: 22 May 2024 12:50 Modified: 17 Dec 2024 23:46
Reporter: Shaun Hirst Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:8.4.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: memory leak

[22 May 2024 12:50] Shaun Hirst
Description:
The get_rows function of connection_cext.pt leaks memory

How to repeat:
Easy to repeat: perform a select * from table
[22 May 2024 12:51] Shaun Hirst
set version
[22 May 2024 12:53] MySQL Verification Team
HI Mr. Hirst,

Thank you for your bug report.

Can you post us a short Python script which will show a memory leak.

Also, which memory checking tool have you used and can you publish the output from that tool ????
[22 May 2024 14:39] Shaun Hirst
I'm using tracemalloc

output

/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=40.1 KiB, count=774, average=53 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=67.1 KiB, count=1303, average=53 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=93.1 KiB, count=1812, average=53 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=116 KiB, count=2255, average=53 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=138 KiB, count=2691, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=159 KiB, count=3115, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=181 KiB, count=3551, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=203 KiB, count=3984, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=225 KiB, count=4408, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=247 KiB, count=4836, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=269 KiB, count=5272, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=291 KiB, count=5701, average=52 B
/home/shaun/.local/lib/python3.12/site-packages/mysql/connector/connection_cext.py:506: size=313 KiB, count=6133, average=52 B
[22 May 2024 14:41] Shaun Hirst
python test file showing leak

Attachment: sql_leak.py (text/x-python-script), 2.11 KiB.

[22 May 2024 14:43] Shaun Hirst
I'm running a python flask server in a docker container it receives 10000's of calls a day in around 20 hours it docker memory usage for that container had grown 800MB
[22 May 2024 15:48] MySQL Verification Team
HI Mr. Hirst,

Do both Python script and MySQL server run in the same docker ???

Have you ran `top` or 'ps aux' or some third tool to see which process grows in size ????

Can you send us snapshots of the list of processes, so that we could see which process is growing ???
[22 May 2024 16:23] Shaun Hirst
the mysql server is in a separate docker container

the test script is being run on host system in vs code

running top when the script is running I see the python3 process growing

>94.1  0.9   973M  591M   441483 shaun          4:40 5     0 R    0 0    python3 
>93.0  1.0   1001M 621M   441483 shaun          4:55 5     0 R    0 0    python3
>93.1  1.1   1.05G 700M   441483 shaun          5:34 5     0 R    0 0    python3
[22 May 2024 16:35] Shaun Hirst
the flask server is also running in it own docker container

not sure if it helps but if I limit the records returned it does not leak. wether that be with a where or a LIMIT 1

seeing the issue in both python 3.9 and 3.12 on. ubuntu 22.04 and 24.04

mysql server version is 8.0.31
[22 May 2024 17:33] MySQL Verification Team
HI,

It is possible that Connector (or Python interpreter !!!!) itself always keep memory for the rows returned.

We shall investigate it ........
[4 Jun 2024 8:38] Shaun Hirst
Hi,

is there any update on this?
[5 Jun 2024 16:23] Shaun Hirst
this is serious for us. causing outages across a number of services
[6 Jun 2024 4:55] MySQL Verification Team
Hello Shaun,

Thank you for the details.
Verified as described.

Regards,
Ashwini Patil
[7 Jun 2024 6:55] Shaun Hirst
Hi,

Thanks for confirming is there an ETA on a fix?
[2 Jul 2024 15:55] Shaun Hirst
Hi,

is there any update on an ETA for a fix. this is causing issues with our services causing us to restart the API.  

I did find https://pypi.org/project/PyMySQL/ and I've done some initial testing it does not appear to have the same memory leak. not sure if it helps you resolve you issue.
[14 Aug 2024 14:33] Oscar Pacheco
Hi Shaun,

We'll look into it. At this time I cannot provide an ETA, but be mindful this report will be triaged with high priority.

Regarding https://pypi.org/project/PyMySQL/
it does not help to resolve the issue because it is a pure-python implementation, not a C-based one.

mysql-connector also includes a pure-python implementation. An attractive workaround is to use the pure-python implementation instead by setting `use_pure` to `True` as part of the connection options.

Regards.
[29 Oct 2024 11:01] Shaun Hirst
the problem with the pure-python implementation is that is a lot slower, and we are handling millions of calls
[16 Dec 2024 19:55] Oscar Pacheco
Posted by developer:
 
Hello Shaun,

With this fix, the connection API command `get_rows()`
of the C-extension implementation stops leaking memory
when working with and without prepared statements.

During the fix of this issue, another less severe
leak was detected - only happens when working with
prepared statements. See [BUG#116990](https://bugs.mysql.com/bug.php?id=116990)
for more details.

The reported issue has been mitigated, closing report.

Thanks for the report,
Regards.
[17 Dec 2024 23:46] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 9.2.0 release, and here's the proposed changelog entry from the documentation team:

 Fixed two memory leaks: one in the C-extension implementation,
 and another with prepared statements.

Thank you for the bug report.