aerospike-client-python icon indicating copy to clipboard operation
aerospike-client-python copied to clipboard

Memory Leak while getting data from aerospike

Open smartist1401 opened this issue 2 years ago • 15 comments

The tracemalloc pythonic tools shows that calling the get function on the aerospike client causes a memory leak. how I use the client: I connect to the aerospike cluster by passing a list of hosts, then in a loop (multiple iteration per second) I have multiple threads that simultaneously read some records from multiple sets.

Screenshot 2023-09-25 093254

Screenshot 2023-09-25 093756

smartist1401 avatar Sep 25 '23 06:09 smartist1401

Hi @smartist1401 I'll investigate this issue today and follow up with you soon.

juliannguyen4 avatar Sep 26 '23 16:09 juliannguyen4

Would you mind sharing the script so I can debug it?

juliannguyen4 avatar Sep 26 '23 19:09 juliannguyen4

test_aerospike_memoryleak.zip

Here is a sample python script that shows memory leak in line 44. Capture

smartist1401 avatar Sep 27 '23 09:09 smartist1401

Hi @juliannguyen4 Did you find anything? Thank you for checking faster, please. I have used your aerospike client in a serious project.

smartist1401 avatar Sep 30 '23 09:09 smartist1401

Hey @smartist1401, I have found a few leaks from get() caused by several different sources. I'll try to fix them and will give you a status update by tomorrow at 5 PM PST.

juliannguyen4 avatar Oct 02 '23 19:10 juliannguyen4

My notes about the get() leaks:

There’s 2 sources for memory leaks with get(): raise_exception() and record_to_pyobject(). I investigated the key_to_pyobject() leaks coming from record_to_pyobject() and I haven’t found the cause yet. The leaks are coming from the namespace, set, and digest of the key tuple.

gdb -args python3 -m pytest new_tests/
# Using Python client 13.0.0
b src/main/conversions.c:1840
cond 2 py_namespace->ob_refcnt > 1 || py_set->ob_refcnt > 1 || py_digest->ob_refcnt > 1
# Test succeeds without breaking

I also added a breakpoint to check the reference counts of the record tuple’s objects as well as the reference count of the record tuple itself. None of them exceeded 1 when being returned by record_to_pyobject() and AerospikeClient_GetInvoke() respectively.

Using sys.getrefcount() doesn’t show any memory leaks either:

>>> import aerospike
>>> config = {"hosts": [("127.0.0.1", 3000)]}
>>> client = aerospike.client(config).connect()
>>> key = ("test", "demo", 1)
>>> client.put(key, {"a": 1})
0
>>> rec = client.get(key)
>>> import sys
>>> sys.getrefcount(rec)
2
>>> sys.getrefcount(rec[2])
2
>>> sys.getrefcount(rec[1])
2
>>> sys.getrefcount(rec[0])
2

juliannguyen4 avatar Oct 02 '23 20:10 juliannguyen4

@smartist1401 When you ran that script and saw the memory leaks reported by tracemalloc, did the records that were being queried exist on the server?

juliannguyen4 avatar Oct 02 '23 21:10 juliannguyen4

Hey @smartist1401, I worked on fixing the memory leak today but I wasn't able to fully solve it. I'll let you know once I have found a solution

juliannguyen4 avatar Oct 04 '23 00:10 juliannguyen4

@smartist1401 When you ran that script and saw the memory leaks reported by tracemalloc, did the records that were being queried exist on the server?

Hi @juliannguyen4 Thanks for the investigation I get a record by key in a try section and return the record if exist, in except (not found record exception) I return an empty dict in except section.

some records exist and some other not.

smartist1401 avatar Oct 04 '23 05:10 smartist1401

Got it. As I mentioned in my comment above, there's a memory leak from raise_exception(), which should be called when calling get() on a record that does not exist. I'll try to fix this memory leak and provide you with a build with the fix.

juliannguyen4 avatar Oct 05 '23 18:10 juliannguyen4

Hi @juliannguyen4 I'm Waiting ... :)

smartist1401 avatar Oct 17 '23 08:10 smartist1401

I'm still getting to the bottom of it.

juliannguyen4 avatar Oct 19 '23 17:10 juliannguyen4

Hi @juliannguyen4 Recently, the new version 14.0.0 has been released. Has the memory leak problem been fixed in this version?

smartist1401 avatar Dec 03 '23 11:12 smartist1401