Skip to content

Commit 633e9e8

Browse files
committed
add test for memory leakage during deletion of value
1 parent 00e3803 commit 633e9e8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

requirements/pytest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ objgraph==3.6.2
22
pytest==8.4.0
33
pytest-codspeed==3.2.0
44
pytest-cov==6.1.0
5+
psutil==7.0.0

tests/isolated/multidict_pop.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Test for memory leaks surrounding deletion of values.
2+
# SEE: https://github.com/aio-libs/multidict/issues/1232
3+
4+
import gc
5+
import sys
6+
import psutil
7+
import os
8+
from multidict import MultiDict
9+
10+
def get_memory_usage():
11+
process = psutil.Process(os.getpid())
12+
memory_info = process.memory_info()
13+
return memory_info.rss / (1024 * 1024)
14+
15+
keys = [f"X-Any-{i}" for i in range(1000)]
16+
headers = {key: key * 2 for key in keys}
17+
18+
19+
def _run_isolated_case() -> None:
20+
for _ in range(10):
21+
for _ in range(1000):
22+
result = MultiDict()
23+
result.update(headers)
24+
for k in keys:
25+
result.pop(k)
26+
# popitem() currently is unaffected but the others all have memory leaks...
27+
# result.popone(k)
28+
# result.popall(k)
29+
del result
30+
gc.collect()
31+
usage = get_memory_usage()
32+
# We should never go over 100MB
33+
if usage > 100:
34+
sys.exit(1)
35+
sys.exit(0)
36+
37+
if __name__ == "__main__":
38+
_run_isolated_case()
39+

tests/test_leaks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"multidict_extend_multidict.py",
1616
"multidict_extend_tuple.py",
1717
"multidict_update_multidict.py",
18+
"multidict_pop.py"
1819
),
1920
)
2021
@pytest.mark.leaks

0 commit comments

Comments
 (0)