Commit 3eb14542 by Kevin Kirsche Committed by Bjorn Neergaard

fix: "ResourceWarning: unclosed SSLSocket"

Relates to: #2153
When running `poetry update`, during the download phase when `PYTHONWARNINGS="default"` environment variable is set, multiple different sessions trigger resource warnings. This fixes one of them that occurs during the download phase of the package update.

```
/Users/username/Library/Application Support/pypoetry/venv/lib/python3.10/site-packages/poetry/installation/executor.py:594: ResourceWarning: unclosed <ssl.SSLSocket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('REDACTED', 59025), raddr=('140.108.3.33', 80)>
```

This line references:
```
 593   │             try:
 594   │                 archive = self._download_archive(operation, link)
 595   │             except BaseException:
 596   │                 cache_directory = self._chef.get_cache_directory_for_link(link)
```

The issue is being caused because the session is being left open when the class is later cleaned up. By adding a destructor method, we can close the session if it exists, preventing this error.
parent 70670fa4
...@@ -83,6 +83,9 @@ class PyPiRepository(RemoteRepository): ...@@ -83,6 +83,9 @@ class PyPiRepository(RemoteRepository):
def session(self) -> CacheControl: def session(self) -> CacheControl:
return self._session return self._session
def __del__(self) -> None:
self._session.close()
def find_packages(self, dependency: Dependency) -> List[Package]: def find_packages(self, dependency: Dependency) -> List[Package]:
""" """
Find packages on the remote server. Find packages on the remote server.
......
...@@ -50,6 +50,10 @@ class Authenticator: ...@@ -50,6 +50,10 @@ class Authenticator:
return self._session return self._session
def __del__(self) -> None:
if self._session is not None:
self._session.close()
def request(self, method: str, url: str, **kwargs: Any) -> requests.Response: def request(self, method: str, url: str, **kwargs: Any) -> requests.Response:
request = requests.Request(method, url) request = requests.Request(method, url)
username, password = self.get_credentials_for_url(url) username, password = self.get_credentials_for_url(url)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment