Repository navigation
Closed
Description
I'm using version 0.4.2, and it seems like the default http server, started via start_http_server leaks memory on every request.
Repro script:
#!/usr/bin/env python3
import gc
import time
import tracemalloc
from random import randint
from prometheus_client import Summary, start_http_server
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds',
'Time spent processing request')
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
"""A dummy function that takes some time."""
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
tracemalloc.start()
snapshot = tracemalloc.take_snapshot()
# Generate some requests.
while True:
process_request(randint(1, 5))
gc.collect()
snapshot2 = tracemalloc.take_snapshot()
top_stats = snapshot2.compare_to(snapshot, 'lineno')
print("[ Top 5 differences ]")
for stat in top_stats[:5]:
print(stat)If you kick off the above script and then do:
while true; do curl localhost:8000; doneyou can observe its memory footprint growing on every iteration:
[ Top 5 differences ]
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:799: size=946 KiB (+946 KiB), count=1682 (+1682), average=576 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:365: size=868 KiB (+868 KiB), count=1683 (+1683), average=528 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:500: size=422 KiB (+422 KiB), count=6753 (+6753), average=64 B
/Users/jose/code/prometheus-mem-leak/.venv/lib/python3.7/site-packages/prometheus_client/core.py:802: size=354 KiB (+354 KiB), count=5027 (+5027), average=72 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:899: size=263 KiB (+263 KiB), count=3366 (+3366), average=80 B
[ Top 5 differences ]
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:799: size=992 KiB (+992 KiB), count=1763 (+1763), average=576 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:365: size=910 KiB (+910 KiB), count=1764 (+1764), average=528 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:500: size=443 KiB (+443 KiB), count=7078 (+7078), average=64 B
/Users/jose/code/prometheus-mem-leak/.venv/lib/python3.7/site-packages/prometheus_client/core.py:802: size=370 KiB (+370 KiB), count=5266 (+5266), average=72 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:899: size=276 KiB (+276 KiB), count=3528 (+3528), average=80 B
[ Top 5 differences ]
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:799: size=1063 KiB (+1063 KiB), count=1889 (+1889), average=576 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:365: size=974 KiB (+974 KiB), count=1889 (+1889), average=528 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:500: size=474 KiB (+474 KiB), count=7584 (+7584), average=64 B
/Users/jose/code/prometheus-mem-leak/.venv/lib/python3.7/site-packages/prometheus_client/core.py:802: size=397 KiB (+397 KiB), count=5639 (+5639), average=72 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:899: size=295 KiB (+295 KiB), count=3778 (+3778), average=80 B
[ Top 5 differences ]
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:799: size=1115 KiB (+1115 KiB), count=1982 (+1982), average=576 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:365: size=1022 KiB (+1022 KiB), count=1983 (+1983), average=528 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:500: size=497 KiB (+497 KiB), count=7956 (+7956), average=64 B
/Users/jose/code/prometheus-mem-leak/.venv/lib/python3.7/site-packages/prometheus_client/core.py:802: size=416 KiB (+416 KiB), count=5922 (+5922), average=72 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:899: size=310 KiB (+310 KiB), count=3966 (+3966), average=80 B
[ Top 5 differences ]
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:799: size=1151 KiB (+1151 KiB), count=2046 (+2046), average=576 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:365: size=1055 KiB (+1055 KiB), count=2047 (+2047), average=528 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:500: size=514 KiB (+514 KiB), count=8213 (+8213), average=64 B
/Users/jose/code/prometheus-mem-leak/.venv/lib/python3.7/site-packages/prometheus_client/core.py:802: size=430 KiB (+430 KiB), count=6108 (+6108), average=72 B
/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py:899: size=320 KiB (+320 KiB), count=4094 (+4094), average=80 B
it's clearly coming from the server, because as soon as you stop the curl, memory usage stops growing
Metadata
Metadata
Assignees
Labels
No labels
Activity
brian-brazil commentedon Nov 10, 2018
That code shouldn't even be calling the line it's pointing to, as there's no http requests being made.
josecv commentedon Nov 10, 2018
i'm assuming a continuous stream of http requests, external to the script (the
while true curl)hipyhop commentedon Nov 19, 2018
I'm seeing this behaviour too, though it's slow enough that it's not really a problem and our orchestrator is restarting the service when it hits 100Mi of memory.
Scraping 125 metrics every 30 seconds (~10-20 custom metrics, the rest default metrics)
sbrandtb commentedon Dec 10, 2018
I found the issue and created a merge request. I hope it will get merged soon.
Fix thread leak in Python 3.7 prometheus#340
sbrandtb commentedon Dec 10, 2018
Until then, there is a quick fix:
Fix thread leak in Python 3.7 #340 (#356)
sbrandtb commentedon Dec 18, 2018
@brian-brazil since this is a rather bad thing in microservice environments, would you mind releasing this soon?
tusharmakkar08 commentedon Jan 31, 2019
@brian-brazil @sbrandtb I am facing this issue with prometheus-client==0.5.0 and Python 3.5.2 and it's not getting fixed by applying the fix which was mentioned by @sbrandtb.
I am not only facing this issue when I am using start_http_server but also when I am trying to use flask's server with prometheus end point monitored. Can you guys help me out?
To be precise my close_wait connections keep on increasing till my server stops serving requests.
x@ip-10-0-0-0:~$ lsof -i | grep 8002
python 10601 x 3u IPv4 78413488 0t0 TCP *:8002 (LISTEN)
python 10601 x 55u IPv4 82060563 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45114 (CLOSE_WAIT)
python 10601 x 56u IPv4 82068773 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45142 (CLOSE_WAIT)
python 10601 x 57u IPv4 82069308 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45170 (CLOSE_WAIT)
python 10601 x 58u IPv4 82070804 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45198 (CLOSE_WAIT)
python 10601 x 59u IPv4 82071589 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45298 (CLOSE_WAIT)
python 10601 x 60u IPv4 82060912 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45328 (CLOSE_WAIT)
python 10601 x 61u IPv4 82073179 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45356 (CLOSE_WAIT)
python 10601 x 62u IPv4 82073397 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45386 (CLOSE_WAIT)
python 10601 x 63u IPv4 82075572 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45414 (CLOSE_WAIT)
python 10601 x 64u IPv4 82077191 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45442 (CLOSE_WAIT)
python 10601 x 65u IPv4 82076370 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45476 (CLOSE_WAIT)
python 10601 x 66u IPv4 82079364 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45504 (CLOSE_WAIT)
python 10601 x 67u IPv4 82077842 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45532 (CLOSE_WAIT)
python 10601 x 68u IPv4 82079997 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45560 (CLOSE_WAIT)
python 10601 x 69u IPv4 82081405 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45588 (CLOSE_WAIT)
python 10601 x 70u IPv4 82083436 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45616 (CLOSE_WAIT)
python 10601 x 71u IPv4 82084046 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45644 (CLOSE_WAIT)
python 10601 x 72u IPv4 82080465 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45672 (CLOSE_WAIT)
python 10601 x 73u IPv4 82084792 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45700 (CLOSE_WAIT)
python 10601 x 74u IPv4 82085875 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45728 (CLOSE_WAIT)
python 10601 x 75u IPv4 82088002 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45758 (CLOSE_WAIT)
python 10601 x 76u IPv4 82089044 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45788 (CLOSE_WAIT)
python 10601 x 77u IPv4 82086871 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45816 (CLOSE_WAIT)
python 10601 x 78u IPv4 82088321 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45844 (CLOSE_WAIT)
python 10601 x 79u IPv4 82090850 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45944 (CLOSE_WAIT)
python 10601 x 80u IPv4 82089551 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:45976 (CLOSE_WAIT)
python 10601 x 81u IPv4 82088475 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:46006 (CLOSE_WAIT)
python 10601 x 82u IPv4 82089875 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:46034 (CLOSE_WAIT)
python 10601 x 83u IPv4 82092200 0t0 TCP ip-10-0-0-0.ap-south-1.compute.internal:8002->ip-172-00-00-00.ap-south-1.compute.internal:46064 (CLOSE_WAIT)
sbrandtb commentedon Feb 1, 2019
@tusharmakkar08 Please check out the comment in my merge request: This issue is very specific to Python >= 3.7
Since you experience an issue in 3.5, I doubt it's the same cause. Also, the shape of the curve on Grafana does not match the behaviour of this bug at all: It does not suddenly start (7:something) nor suddenly decrease again, granted there are no other effects and your metrics endpoint is called regularly (also, that makes the graph go up very linearly, while yours is everything else than that)
However, without any more information like code we can't tell anything more.
tusharmakkar08 commentedon Feb 15, 2019
I think my issue is more similar to prometheus/jmx_exporter#327 and prometheus/jmx_exporter#352.
sbrandtb commentedon Feb 16, 2019
@tusharmakkar08 I think you should open a new issue with code how to reproduce, because you won't get much audience here or help without reproducible code.
Upgrade Prometheus client to fix memory leak with Python 3.7
yaverhussain commentedon Feb 9, 2021
Any one resolved this?
sbrandtb commentedon Feb 10, 2021
@yaverhussain The issue has been fixed and merged by me two years ago in #356
yaverhussain commentedon Feb 10, 2021
sbrandtb commentedon Feb 11, 2021
@yaverhussain >= 0.6.0, see badges here
yaverhussain commentedon Feb 12, 2021
csmarchbanks commentedon Feb 12, 2021
Hello, rather than continuing to comment on this issue, would you make a new issue if you believe there is a problem with this client? If you have a general usage question, I recommend reaching out to the prometheus-users mailing list. Thanks in advance!
That said, if you are adding distinct URLs as label values, such as having ids/uuids in the label value, then you will use a lot of memory as metrics will need to be stored for each URL. Usually people will template urls like
"/my/path/:id"to avoid this issue.