-
Notifications
You must be signed in to change notification settings - Fork 390
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
ResourceWarning: unclosed <ssl.SSLSocket... #710
Comments
Hi @jedie there are many details missing from your report. Python version seems to be 3.10, version of vcrpy is unclear, user level code doing the requests is unclear, could be requests, urllib3 v1 or v2 etc, is this behavior new or does it happen across all versions of vcrpy etc. Please add more details. |
Sorry, here Version information:
|
Thanks! Is this behavior new or does it happen across all versions of vcrpy? I think vcrpy does not open SSL connections by itself so I would assume it's either a bug in vcrpy's connection pool code or the library outside of vcrpy that does open SSL connections. I'm guessing here, any help with debugging is appreciated. |
Three weeks without a reproducer, closing as "cannot reproduce". Happy to re-open if someone can provide a minimal producible example. Thanks! |
Sorry, i have currently no time to investigate this further... I currently just ignore the warning ;) |
Hi @hartwork, This repo created by @mezhaka has a minimal reproducible example with pytest and here is an even simpler one without pytest: import warnings
import requests
import vcr
def main():
session = requests.Session()
with vcr.use_cassette(path="/dev/null"):
session.get("http://neverssl.com")
# session.close()
if __name__ == "__main__":
warnings.filterwarnings("always", category=ResourceWarning)
main() In Python 3.8, with vcrpy 5.1.0 I get a |
This PR fixes issue kevin1024#710 by properly closing the underlying socket. It first uses `pool._put_conn` to keep the connection in the pool, and later removes and closes it when the context manager exits. I was unsure about the exact purpose of the `ConnectonRemove` class, so I made minimal changes to minimize the risk of breaking the code and there may be better solutions for fixing this issue. For example, the `urllib3.connectionpool.HTTPConnectionPool` will utilize a weakref to terminate pool connections. By appending our connection to it, it will also take care of closing our connection. So another solution could be to modify the `__exit__` in `patch.ConnectionRemover` method and add our connection to the pool: ```py class ConnectionRemover: ... def __exit__(self, *args): for pool, connections in self._connection_pool_to_connections.items(): for connection in connections: if isinstance(connection, self._connection_class): pool._put_conn(connection) ```
I've been trying to make this one fail locally to try and fix this (and also provide a new unit test for vcrpy) but can't seem to do so. Is there anything else that needs installing? It just seems to work with no failure for me. |
I haven't seen this error for a while. So i assume it's fixed. But i have no more details, sorry. EDIT: I still use vcrpy only with normal unittests (with Django) and not via pytest. Works fine... |
I'm able to reproduce this reliably with current vcrpy master. Adding the Now trying to isolate a test case that isn't my entire closed-source app... |
Reproduction steps
That last step reliably gets the following
The |
Here are the steps to reproduce the problem in a container to make sure you have exactly the same environment: $ docker run --rm -it python:3.11 bash
root@container:/# pip install vcrpy==5.1.0 requests==2.31.0
root@container:/# cat > test.py
# copy/paste the code from my previous comment (https://github.com/kevin1024/vcrpy/issues/710#issuecomment-1667240075) and press ctrl-d
root@container:/# python test.py
sys:1: ResourceWarning: unclosed <socket.socket fd=3, family=2, type=1, proto=6, laddr=('172.17.0.2', 50746), raddr=('34.223.124.45', 80)>
ResourceWarning: Enable tracemalloc to get the object allocation traceback |
Looks like this was reproduced and there's a PR to fix it. I'm also having the same issue on my test suite |
resolved in #811 |
I get some warnings about unclosed SSL socket, looks like:
Any advise to fix that?
The text was updated successfully, but these errors were encountered: