-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Memory leak: warnings module #97557
Description
Bug report
Memory leak in warnings module
Steps to reproduce:
- Create test script
`$ cat > memory_leak_warnings.py << EOF
import warnings
import random
while True:
warn_text = str(random.random())
warnings.warn(warn_text, DeprecationWarning)
EOF`
-
Run script
docker run -it --rm --name memory-leak-warnings -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3.9 python memory_leak_warnings.py -
Monitor memory usage of container with running script
$ docker stats memory-leak-warnings
And you can see MEM USAGE constantly increased

https://docs.python.org/3/library/warnings.html
Warnings is a standard module, anyone can use it.
Having an application with a lot of dependencies - you have no idea which module use warnings module, so in production you will have memory leak.
For example aiomysql use it https://github.com/aio-libs/aiomysql/blob/master/aiomysql/cursors.py#L479 and seems like it is ok to use warnings standard module.
I have to disable warnings to get rid such kind of memory leaks. Is it ok? Or maybe warnings must be disabled on production enviroments? If so - this should be noticed in documentaion.
Thanks
Your environment
- CPython versions tested on: 3.6, 3.7, 3.9, 3.10
- Operating system and architecture: https://hub.docker.com/_/python