requests: Requests memory leak
Summary.
Expected Result
Program running normally
Actual Result
Program consuming all ram till stops working
Reproduction Steps
Pseudocode:
def function():
proxies = {
'https': proxy
}
session = requests.Session()
session.headers.update({'User-Agent': 'user - agent'})
try: #
login = session.get(url, proxies=proxies) # HERE IS WHERE MEMORY LEAKS
except: #
return -1 #
return 0
System Information
$ python -m requests.help
{
"chardet": {
"version": "3.0.4"
},
"cryptography": {
"version": ""
},
"idna": {
"version": "2.6"
},
"implementation": {
"name": "CPython",
"version": "3.6.3"
},
"platform": {
"release": "10",
"system": "Windows"
},
"pyOpenSSL": {
"openssl_version": "",
"version": null
},
"requests": {
"version": "2.18.4"
},
"system_ssl": {
"version": "100020bf"
},
"urllib3": {
"version": "1.22"
},
"using_pyopenssl": false
}
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 9
- Comments: 25 (2 by maintainers)
Commits related to this issue
- added calls to response.close() to fix a memory leak see https://github.com/psf/requests/issues/4601#issuecomment-603326738 for details — committed to coreGreenberet/dashboard-api-python by coreGreenberet 4 years ago
- Close response objects in an attempt to fix memory leakage. See https://github.com/psf/requests/issues/4601 — committed to ecederstrand/exchangelib by ecederstrand 4 years ago
Call
Session.close()andResponse.close()can avoid the memory leak. And ssl will consume more memory so the memory leak will more remarkable when request https urls.First I make 4 test cases:
Pseudo code:
Memory usage graph(y-axis: MB, x-axis: time), requests use lots of memory and memory increase very fast, while aiohttp memory usage is stable:
Then I add
Session.close()and test again:Memory usage significant decreased, but memory usage still increase over time:
Finally I add
Response.close()and test again:Memory usage decreased again, and not increase over time:
Compare aiohttp and requests shows memory leak is not caused by ssl, itβs caused by connection resources not closed.
Useful scripts:
System Information:
Similar issue. Requests eats memory when running in thread. Code to reproduce here:
In the code given above I pass a session object around, but if I replace it with just running
requests.getnothing changes.Output is:
And Pipfile looks like this:
Same here, any work around?
It seems Requests is still in beta stage having memory leaks like that. Come on, guys, patch this up! ππ
Here is my memory leaking issue, anyone can help ? https://stackoverflow.com/questions/59746125/memory-keep-growing-when-using-mutil-thread-download-file
Ok, so this is, by the looks of it still an issue. Using
requests.Session()to make requests to anhttpsurl leads to constantly increasing memory usage and ultimately an OOM condition and a crash. The requests are being made usingproxiesto some proxyHereβs a shot of the monotonously increasing mem usage :
This is from a production system running (in a docker container):
But we see the same behavior on Windows:
AND in WSL2:
Iβm observing a memory increase every time a
requests.Session()is instantiated, never to be reclaimed. ASessionthat is actually wrapped in a CloudScraper but thereβs nothing special done to how the requests and sessions are handled there. And due to using cloudscraper, I canβt test the code using onlyrequests.get()as opposed to usingrequests.Session(), btw.Higher up in this thread thereβs this comment by @VeNoMouS , but since the issues tracker of his repo was disabled, I canβt see what he said there, and neither google nor archive.org has a copy of the comment. But whatever it says, I can see the memory leaking every time a new session is created and later discarded.
While investigating whatβs going on I stumbled on this original Python issue, migrated to GitHub here, and seems to imply that this is a Windows only problem but that does not seem to be the case. Both are closed as resolved, btw.
Then I went on to memray the thing. Hereβs the summary view:
and the memory stack with
util/ssl_.pyLooking around at the solutions to memory leaks in various systems mentioning and linked to this issue, I changed our code to βforce closeβ the sessions using:
cloudscraper.create_scraperinstantiates arequests.Session()essentially.for making the requests I changed it to
This seems to have improved the situation a bit, as at least now itβs not monotonously growing but also has some [slight] reductions
But this is still not what I think it should be looking like.
Currently, we are keeping it under control by setting a memory limit on the container itβs running on in productions and we are also passing
...--max-requests 750 --max-requests-jitter 50...togunicornSo, what would you suggest the next logical step to be?
Thanks!
Same for me
Same for me⦠leakage while on threadpool execution is on Windows python38 too. requests 2.22.0
Any update on this? Simple POST request with a file upload also creates the similar issue of the memory leak.
Hey @munroc, a couple quick questions about your threading implementation since itβs not included in the pseudo code.
Are you creating a new session for every thread and what size is the threadpool youβre using?
What tool are you using to determine where the leak is coming from? Would you mind sharing the results?
Weβve had hints of memory leaks around sessions for a while now, but Iβm not sure weβve found a smoking gun or truly confirmed impact.
Please provide us with the output of
If that is unavailable on your version of Requests please provide some basic information about your system (Python version, operating system, etc).