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

Do not reuse requests sessions #3230

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Do not reuse requests sessions
When running under certain environment, reuse of request sessions maybe
causing network connectivity issues. This change ensures that request
sessions are not reused across requests.

Relates-to: #3219
abn committed Oct 17, 2020
commit 4917d7b732fae84929d98ed4e8f6c05901fe7dac
6 changes: 1 addition & 5 deletions poetry/installation/authenticator.py
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@ class Authenticator(object):
def __init__(self, config, io=None): # type: (Config, Optional[IO]) -> None
self._config = config
self._io = io
self._session = None
self._credentials = {}
self._password_manager = PasswordManager(self._config)

@@ -45,10 +44,7 @@ def _log(self, message, level="debug"): # type: (str, str) -> None

@property
def session(self): # type: () -> requests.Session
if self._session is None:
self._session = requests.Session()

return self._session
return requests.Session()

def request(
self, method, url, **kwargs
37 changes: 20 additions & 17 deletions poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
@@ -187,22 +187,10 @@ def __init__(
self._authenticator = Authenticator(
config=config or Config(use_environment=True)
)

self._session = CacheControl(
self._authenticator.session, cache=FileCache(str(self._cache_dir / "_http"))
)

self._basic_auth = None
username, password = self._authenticator.get_credentials_for_url(self._url)
if username is not None and password is not None:
self._authenticator.session.auth = requests.auth.HTTPBasicAuth(
username, password
)

if self._cert:
self._authenticator.session.verify = str(self._cert)

if self._client_cert:
self._authenticator.session.cert = str(self._client_cert)
self._basic_auth = requests.auth.HTTPBasicAuth(username, password)

self._disable_cache = disable_cache

@@ -214,17 +202,32 @@ def cert(self): # type: () -> Optional[Path]
def client_cert(self): # type: () -> Optional[Path]
return self._client_cert

@property
def session(self):
session = self._authenticator.session

if self._basic_auth:
session.auth = self._basic_auth

if self._cert:
session.verify = str(self._cert)

if self._client_cert:
session.cert = str(self._client_cert)

return CacheControl(session, cache=FileCache(str(self._cache_dir / "_http")))

@property
def authenticated_url(self): # type: () -> str
if not self._session.auth:
if not self._basic_auth:
return self.url

parsed = urlparse.urlparse(self.url)

return "{scheme}://{username}:{password}@{netloc}{path}".format(
scheme=parsed.scheme,
username=quote(self._session.auth.username, safe=""),
password=quote(self._session.auth.password, safe=""),
username=quote(self._basic_auth.username, safe=""),
password=quote(self._basic_auth.password, safe=""),
netloc=parsed.netloc,
path=parsed.path,
)
6 changes: 1 addition & 5 deletions poetry/repositories/pypi_repository.py
Original file line number Diff line number Diff line change
@@ -70,15 +70,11 @@ def __init__(self, url="https://pypi.org/", disable_cache=False, fallback=True):
)

self._cache_control_cache = FileCache(str(release_cache_dir / "_http"))
self._session = CacheControl(
requests.session(), cache=self._cache_control_cache
)

self._name = "PyPI"

@property
def session(self):
return self._session
return CacheControl(requests.session(), cache=self._cache_control_cache)

def find_packages(self, dependency): # type: (Dependency) -> List[Package]
"""