workaround for garbage collection

This commit is contained in:
Niko authored and GitHub committed 2025-03-05 08:42:20 +01:00
1 parent 103d6873fa
commit 6bf645cba3
4 files changed
+8 -7

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ Add your changes below.
- Fixed scripts in examples directory that didn't run correctly - Fixed scripts in examples directory that didn't run correctly
- Updated documentation for `Client.current_user_top_artists` to indicate maximum number of artists limit - Updated documentation for `Client.current_user_top_artists` to indicate maximum number of artists limit
- Set auth cache file permissions to `600`: https://github.com/spotipy-dev/spotipy/security/advisories/GHSA-pwhh-q4h6-w599 - Set auth cache file permissions to `600`: https://github.com/spotipy-dev/spotipy/security/advisories/GHSA-pwhh-q4h6-w599
- Fixed `__del__` methods by preventing garbage collection for `requests.Session`
### Changed ### Changed
+2 -5
View File
@@ -11,7 +11,7 @@ from collections import defaultdict
import requests import requests
from spotipy.exceptions import SpotifyException from spotipy.exceptions import SpotifyException
from spotipy.util import Retry from spotipy.util import REQUESTS_SESSION, Retry
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -211,11 +211,8 @@ class Spotify:
def __del__(self): def __del__(self):
"""Make sure the connection (pool) gets closed""" """Make sure the connection (pool) gets closed"""
try: if getattr(self, "_session", None) and isinstance(self._session, REQUESTS_SESSION):
if isinstance(self._session, requests.Session):
self._session.close() self._session.close()
except AttributeError:
pass
def _build_session(self): def _build_session(self):
self._session = requests.Session() self._session = requests.Session()
+2 -2
View File
@@ -21,7 +21,7 @@ import requests
from spotipy.cache_handler import CacheFileHandler, CacheHandler from spotipy.cache_handler import CacheFileHandler, CacheHandler
from spotipy.exceptions import SpotifyOauthError, SpotifyStateError from spotipy.exceptions import SpotifyOauthError, SpotifyStateError
from spotipy.util import CLIENT_CREDS_ENV_VARS, get_host_port, normalize_scope from spotipy.util import CLIENT_CREDS_ENV_VARS, REQUESTS_SESSION, get_host_port, normalize_scope
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -122,7 +122,7 @@ class SpotifyAuthBase:
def __del__(self): def __del__(self):
"""Make sure the connection (pool) gets closed""" """Make sure the connection (pool) gets closed"""
if isinstance(self._session, requests.Session): if getattr(self, "_session", None) and isinstance(self._session, REQUESTS_SESSION):
self._session.close() self._session.close()
+3
View File
@@ -22,6 +22,9 @@ CLIENT_CREDS_ENV_VARS = {
"redirect_uri": "SPOTIPY_REDIRECT_URI", "redirect_uri": "SPOTIPY_REDIRECT_URI",
} }
# workaround for garbage collection
REQUESTS_SESSION = requests.Session
def prompt_for_user_token( def prompt_for_user_token(
username=None, username=None,