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

ResourceWarning: unclosed <ssl.SSLSocket... #710

Closed
jedie opened this issue May 30, 2023 · 13 comments
Closed

ResourceWarning: unclosed <ssl.SSLSocket... #710

jedie opened this issue May 30, 2023 · 13 comments
Labels
help required replicating This issue would appreciate having a toy repo that potential contributors could clone to replicate

Comments

@jedie
Copy link

jedie commented May 30, 2023

I get some warnings about unclosed SSL socket, looks like:

...
.../utilities/test_utils.py:110: in __exit__
	self.vcr_mock.__exit__(exc_type, exc_val, exc_tb)
site-packages/vcr/cassette.py:105: in __exit__
	next(self.__finish, None)
site-packages/vcr/cassette.py:65: in _patch_generator
	with contextlib.ExitStack() as exit_stack:
/usr/local/lib/python3.10/contextlib.py:561: in __exit__
	if cb(*exc_details):
site-packages/vcr/patch.py:402: in __exit__
	connection = pool.pool.get()
/usr/local/lib/python3.10/warnings.py:109: in _showwarnmsg
	sw(msg.message, msg.category, msg.filename, msg.lineno,
.../settings/warning_handler.py:47: in __call__
	frames = traceback.extract_stack()
WARNING from "test_sdk_client.py" in test_get_category_by_key():
"/usr/local/lib/python3.10/contextlib.py ResourceWarning: unclosed <ssl.SSLSocket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=...>"

Any advise to fix that?

@hartwork
Copy link
Collaborator

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.

@jedie
Copy link
Author

jedie commented Jun 1, 2023

Sorry, here Version information:

  • Python 3.10
  • VCR.py v4.3.1
  • requests v2.31.0
  • urllib3 v1.26.16

@hartwork
Copy link
Collaborator

hartwork commented Jun 1, 2023

Thanks! Is this behavior new or does it happen across all versions of vcrpy?
File test_sdk_client.py was making requests using requests directly or with some layer in between? If so which one?

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.

@hartwork
Copy link
Collaborator

Three weeks without a reproducer, closing as "cannot reproduce". Happy to re-open if someone can provide a minimal producible example. Thanks!

@hartwork hartwork added the help required replicating This issue would appreciate having a toy repo that potential contributors could clone to replicate label Jun 22, 2023
@jedie
Copy link
Author

jedie commented Jun 22, 2023

Sorry, i have currently no time to investigate this further... I currently just ignore the warning ;)

@admirito
Copy link

admirito commented Aug 7, 2023

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 sys:1: ResourceWarning: unclosed <socket.socket fd=6, family=2, type=1, proto=6, laddr=('192.168.1.114', 64833), raddr=('34.223.124.45', 80)>. Even closing the session explicitly with session.close() will not stop the ResourceWarning.

admirito pushed a commit to admirito/vcrpy that referenced this issue Aug 7, 2023
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)
```
@palfrey
Copy link

palfrey commented Aug 23, 2023

here is an even simpler one without pytest:

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.

@jedie
Copy link
Author

jedie commented Aug 23, 2023

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...

@palfrey
Copy link

palfrey commented Aug 23, 2023

I'm able to reproduce this reliably with current vcrpy master. Adding the connection.close() line from #758 resolves the issue.

Now trying to isolate a test case that isn't my entire closed-source app...

@palfrey
Copy link

palfrey commented Aug 23, 2023

Reproduction steps

  1. Checkout current vcrpy master
  2. pip install pytest-vcr
  3. Dump the following into "test.py"
import warnings
import pytest
import requests

@pytest.mark.vcr
def test_main():
    warnings.filterwarnings("error", category=ResourceWarning)
    session = requests.Session()
    session.get("http://neverssl.com")
  1. rm -Rf cassettes && pytest test.py -vvv -s

That last step reliably gets the following

platform linux -- Python 3.8.14, pytest-7.4.0, pluggy-1.2.0 -- /home/palfrey/.virtualenvs/vcrpy/bin/python
cachedir: .pytest_cache
rootdir: /home/palfrey/src/vcrpy
configfile: pyproject.toml
plugins: vcr-1.0.2, cov-4.1.0, httpbin-2.0.0
collected 1 item

test.py::test_main PASSED

================================================================ warnings summary =================================================================
test.py::test_main
  /home/palfrey/.virtualenvs/vcrpy/lib/python3.8/site-packages/_pytest/unraisableexception.py:78: PytestUnraisableExceptionWarning: Exception ignored in: <socket.socket fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>

  Traceback (most recent call last):
    File "/home/palfrey/.pyenv/versions/3.8.14/lib/python3.8/contextlib.py", line 510, in __exit__
      if cb(*exc_details):
  ResourceWarning: unclosed <socket.socket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.25', 52936), raddr=('34.223.124.45', 80)>

    warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

The pytest-vcr could be the issue, but given the connection.close() patch seems to fix this, I doubt it!

@admirito
Copy link

here is an even simpler one without pytest:

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.

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

@graingert
Copy link
Collaborator

Looks like this was reproduced and there's a PR to fix it. I'm also having the same issue on my test suite

@graingert graingert reopened this Dec 10, 2023
@graingert graingert changed the title contextlib.py ResourceWarning: unclosed <ssl.SSLSocket... ResourceWarning: unclosed <ssl.SSLSocket... Dec 10, 2023
@graingert
Copy link
Collaborator

resolved in #811

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help required replicating This issue would appreciate having a toy repo that potential contributors could clone to replicate
Projects
None yet
Development

No branches or pull requests

5 participants