Skip to content

Memory leak if a test contains the built-in eval #676

Closed
@avara1986

Description

@avara1986

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

import resource


def test_dummy():
    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}"
            )

When I execute it with --cov="", the memory keeps increasing:

pytest -s --cov="" tests_dummy.py 
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.11.0, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/.../projects/TEST
plugins: cov-6.0.0
collected 1 item                                                                                                                                                                                                  

tests_dummy.py Round 0 Max RSS: 45.87109375
Round 500 Max RSS: 46.12890625
Round 1000 Max RSS: 46.38671875
Round 1500 Max RSS: 46.64453125
Round 2000 Max RSS: 46.64453125
Round 2500 Max RSS: 46.90234375
Round 3000 Max RSS: 47.16015625
Round 3500 Max RSS: 47.41796875
Round 4000 Max RSS: 47.67578125
Round 4500 Max RSS: 47.93359375

However, if I pass --no-cov, the memory remains stable.

pytest -s --no-cov tests_dummy.py
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.11.0, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/.../projects/TEST
plugins: cov-6.0.0
collected 1 item                                                                                                                                                                                                  

tests_dummy.py Round 0 Max RSS: 43.359375
Round 500 Max RSS: 43.359375
Round 1000 Max RSS: 43.359375
Round 1500 Max RSS: 43.359375
Round 2000 Max RSS: 43.359375
Round 2500 Max RSS: 43.359375
Round 3000 Max RSS: 43.359375
Round 3500 Max RSS: 43.359375
Round 4000 Max RSS: 43.359375
Round 4500 Max RSS: 43.359375
Round 5000 Max RSS: 43.359375
...
Round 1647500 Max RSS: 779.58984375

Versions

Python 3.11.0/Python 3.10.5/Python 3.13.1

python -m pip freeze                        
coverage==7.6.10
iniconfig==2.0.0
packaging==24.2
pluggy==1.5.0
pytest==8.3.4
pytest-cov==6.0.0

Config

No .coveragerc
No setup.cfg

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions