Skip to content
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 during import #90

Open
insani7y opened this issue Oct 12, 2023 · 5 comments
Open

Memory leak during import #90

insani7y opened this issue Oct 12, 2023 · 5 comments

Comments

@insani7y
Copy link

insani7y commented Oct 12, 2023

Long story short

I use aiofile to check health of consumers. Some file is being touched during consumer process and then in k8s liveness probe I compare modified time.
Unfortunately I noticed a memory leak that was caused by liveness probe.

Expected behavior

I expect no memory leaks :)

Actual behavior

There is one.

Steps to reproduce

Here code for reproduction:

# liveness_probe/__main__.py
import asyncio


async def liveness_probe() -> None:
    import aiofile

    aiofile


if __name__ == "__main__":
    asyncio.run(liveness_probe())

Aiofile should be imported inside function.

    livenessProbe:
      initialDelaySeconds: 30
      periodSeconds: 10
      timeoutSeconds: 10
      exec:
        command:
          - python
          - -m
          - liveness_probe

Just probe inside k8s Deployment.

Environment info

k8s version: 1.21.8
aiofile version: 3.8.0 - 3.8.8 (didn't check other)
python version: 3.11.3 - 3.11.6 (didn't check other)

I have been produced this problem with implementations:
✅ export CAIO_IMPL=linux
✅ export CAIO_IMPL=thread
✅ export CAIO_IMPL=python

Additional info

Here are graphs of the increase in the amount of memory consumed in pod:
photo_2023-10-12 16 33 10
photo_2023-10-12 16 33 07

@egorsmkv
Copy link

Why did you import aiofile in a function?

I just finished to test it with taskiq's worker and do not see a memory leak because of aiofile, memory usage is not changing.

@insani7y
Copy link
Author

insani7y commented Oct 17, 2024

I didn't import it in a function. I launched a script several times, imported aiofile inside of it. It's just an example

@egorsmkv
Copy link

egorsmkv commented Oct 17, 2024

I meant why did you write the code like:

# liveness_probe/__main__.py
import asyncio


async def liveness_probe() -> None:
    import aiofile

    aiofile

but not like:

# liveness_probe/__main__.py
import asyncio
import aiofile

async def liveness_probe() -> None:
    aiofile

Also, are you seeing the memory leak in K8S only? Can you make a test script without K8S?

@insani7y
Copy link
Author

insani7y commented Oct 18, 2024

Got it. You're right, that would be more correct.

# liveness_probe/__main__.py
import asyncio
import aiofile

async def liveness_probe() -> None:
    aiofile

And that's how we use it on our side. But this script is loaded dynamically and executed, so every time we run it, we import aiofile again.
My original example is just a simplification, but the problem persists even in your example.

Additionally, memory leak is confirmed with memray.

@egorsmkv
Copy link

Can you attach the memray log?

I also tried to check this issue with memray, but haven't found the leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants