Skip to content

Commit e83c115

Browse files
Add test for memory leak
1 parent dbcab73 commit e83c115

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dev = [
3939
"sphinx-design",
4040
"tox-direct",
4141
"types-mock",
42+
"psutil",
4243
]
4344

4445
[project.scripts]

tests/test_cothread.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import multiprocessing as mp
44
import time
55

6+
import psutil
7+
68
# Add cothread onto file and import
79
import sys
810
import os
@@ -169,6 +171,33 @@ def target():
169171
after = time.time()
170172
assert float(after - before) > TIMEOUT
171173

174+
def test_threadstate_memory_leak():
175+
"""Test that the memory leak reported in issue #70 is fixed and does not
176+
reoccur"""
177+
178+
process = psutil.Process()
179+
180+
vms_start = process.memory_info().vms
181+
182+
def test():
183+
pass
184+
185+
# Arbitrary large number of spawns. On my machine with the memory leak
186+
# active,this results in a leak of approximately 2GiB.
187+
for i in range(500000):
188+
cothread.Spawn(test)
189+
cothread.Yield()
190+
191+
vms_end = process.memory_info().vms
192+
193+
print(f"VMS start {vms_start} end {vms_end} diff {vms_end-vms_start}")
194+
195+
memory_increase = vms_end - vms_start
196+
197+
# With the memory leak fixed, the memory increase on my machine is only
198+
# 16384 bytes. Added an order of magnitude for future safety.
199+
assert memory_increase < 200000
200+
172201

173202
if __name__ == '__main__':
174203
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)