Skip to content

Commit

Permalink
Patch urllib3 to mark the retry warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 committed Jul 15, 2024
1 parent 02b57f2 commit b129049
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
20 changes: 7 additions & 13 deletions src/pip/_internal/network/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,15 @@ def __init__(self, *, timeout: Optional[float], verbose: bool) -> None:
self.verbose = verbose

def filter(self, record: logging.LogRecord) -> bool:
# Attempt to "sniff out" the retrying warning.
if not isinstance(record.args, tuple):
# We patch urllib3 to include an extra attribute on the retrying warning
# that we want to rewrite.
context = getattr(record, "pip_urllib3_retry_warning", None)
if context is None:
return True

retry = next(
(a for a in record.args if isinstance(a, urllib3.util.Retry)), None
)
if record.levelno != logging.WARNING or retry is None:
# Not the right warning, leave it alone.
return True

error = next((a for a in record.args if isinstance(a, Exception)), None)
if error is None:
# No error information available, leave it alone.
return True
retry, error = context
assert isinstance(retry, urllib3.util.Retry)
assert isinstance(error, Exception)

rewritten = False
if isinstance(error, urllib3.exceptions.NewConnectionError):
Expand Down
3 changes: 2 additions & 1 deletion src/pip/_vendor/urllib3/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ def _is_ssl_error_message_from_http_proxy(ssl_error):
if not conn:
# Try again
log.warning(
"Retrying (%r) after connection broken by '%r': %s", retries, err, url
"Retrying (%r) after connection broken by '%r': %s", retries, err, url,
extra={"pip_urllib3_retry_warning": (retries, err)}
)
return self.urlopen(
method,
Expand Down
14 changes: 14 additions & 0 deletions tools/vendoring/patches/urllib3-retry-warning.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/src/pip/_vendor/urllib3/connectionpool.py b/src/pip/_vendor/urllib3/connectionpool.py
index 5a6adcbdc..b973d287d 100644
--- a/src/pip/_vendor/urllib3/connectionpool.py
+++ b/src/pip/_vendor/urllib3/connectionpool.py
@@ -822,7 +822,8 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
if not conn:
# Try again
log.warning(
- "Retrying (%r) after connection broken by '%r': %s", retries, err, url
+ "Retrying (%r) after connection broken by '%r': %s", retries, err, url,
+ extra={"pip_urllib3_retry_warning": (retries, err)}
)
return self.urlopen(
method,

0 comments on commit b129049

Please sign in to comment.