-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak if a test contains the built-in eval
#676
Comments
I'm curious if this is causing a problem for you? How did you discover it? |
To give more context, we detected this issue at DataDog in our tracer while running this test: This test validates that our tracer detects vulnerabilities (SQLi, CMDi, SSRF...) and ensures that our C++ code doesn’t cause memory leaks. We were trying to include Code Injection detection in the |
Does this happen only on pytest or on plain coverage as well ? Does it happen with |
@avara1986 what is your MALLOC_ARENA_MAX and core count on the test machine? Assuming you running with the defaults and glibc detects a high core count (doesn't matter what is actually available) there is going to be a lot of RSS memory wasted. |
This is an issue with coverage.py. @avara1986 I'm glad to hear that DataDog is getting good use from it. Would they consider some GitHub sponsorship to help with the fix? |
@ionelmc here is the data:
Thanks, @nedbat ! You're right, if I run the script this way, there's still a memory leak using coverage, even without pytest: def eval_loop():
for i in range(100_000):
r = eval(f"'a' + '1'")
assert r == 'a1'
current_rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
if i % 500 == 0:
print(
f"Round {i} Max RSS: {current_rss}"
)
if __name__ == "__main__":
eval_loop() ✅ OK: python tests_dummy.py Similarly, there’s a leak in these cases with my first pytest example, but not in these others. |
In your case the default for MALLOC_ARENA_MAX would be 128, which is excessive. My suggestion is to set that env var to 1 or 2, then retest. You can read more about it here: https://devcenter.heroku.com/articles/tuning-glibc-memory-behavior |
@avara1986 Any thoughts about sponsorship? |
This is fixed in coverage.py: nedbat/coveragepy@f85d9b7 |
Hey @nedbat , we’ve implemented a workaround by disabling coverage for that test since it’s not entirely necessary. However, we’re discussing the possibility of sponsorship internally for the near future. Thanks a lot! |
@avara1986 thanks! ;-) |
This is now released as part of coverage 7.6.11. |
Summary
If I run a test that contains the built-in
eval
function, a memory leak occurs when executed with pytest-cov.Reproducer
For example, if I run this code:
File
tests_dummy.py
When I execute it with
--cov=""
, the memory keeps increasing:However, if I pass
--no-cov
, the memory remains stable.Versions
Python 3.11.0/Python 3.10.5/Python 3.13.1
Config
No .coveragerc
No setup.cfg
The text was updated successfully, but these errors were encountered: