python-urllib3/backport-Fix-socket-timeout-value-when-HTTPConnection-is-reused.patch

75 lines
2.9 KiB
Diff
Raw Normal View History

2023-03-21 14:27:38 +08:00
From a7ce8e0881c94800b14687145ee11940246d2b22 Mon Sep 17 00:00:00 2001
From: Nick Williams <68963309+nickwilliams-zaxiom@users.noreply.github.com>
Date: Fri, 20 Jan 2023 07:59:33 -0600
Subject: [PATCH] [1.26] Fix socket timeout value when HTTPConnection is reused
Co-authored-by: Seth Michael Larson <sethmichaellarson@gmail.com>
Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>
Conflict:1.The content of "@@" is adapted 2.The line number is adapted 3. no add testcode
Reference:https://github.com/urllib3/urllib3/commit/a7ce8e0881c94800b14687145ee11940246d2b22
---
src/urllib3/connection.py | 5 +++++
src/urllib3/connectionpool.py | 2 +-
src/urllib3/util/timeout.py | 9 ++++++---
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/src/urllib3/connection.py b/src/urllib3/connection.py
index f48daea..f121511 100644
--- a/src/urllib3/connection.py
+++ b/src/urllib3/connection.py
@@ -229,6 +229,11 @@ class HTTPConnection(_HTTPConnection, object):
)
def request(self, method, url, body=None, headers=None):
+ # Update the inner socket's timeout value to send the request.
+ # This only triggers if the connection is re-used.
+ if getattr(self, "sock", None) is not None:
+ self.sock.settimeout(self.timeout)
+
if headers is None:
headers = {}
else:
diff --git a/src/urllib3/connectionpool.py b/src/urllib3/connectionpool.py
index 8dccf4b..e528019 100644
--- a/src/urllib3/connectionpool.py
+++ b/src/urllib3/connectionpool.py
@@ -375,7 +375,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
timeout_obj = self._get_timeout(timeout)
timeout_obj.start_connect()
- conn.timeout = timeout_obj.connect_timeout
+ conn.timeout = Timeout.resolve_default_timeout(timeout_obj.connect_timeout)
# Trigger any extra validation we need to do.
try:
diff --git a/src/urllib3/util/timeout.py b/src/urllib3/util/timeout.py
index ff69593..78e18a6 100644
--- a/src/urllib3/util/timeout.py
+++ b/src/urllib3/util/timeout.py
@@ -2,9 +2,8 @@ from __future__ import absolute_import
import time
-# The default socket timeout, used by httplib to indicate that no timeout was
-# specified by the user
-from socket import _GLOBAL_DEFAULT_TIMEOUT
+# The default socket timeout, used by httplib to indicate that no timeout was; specified by the user
+from socket import _GLOBAL_DEFAULT_TIMEOUT, getdefaulttimeout
from ..exceptions import TimeoutStateError
@@ -116,6 +115,10 @@ class Timeout(object):
# __str__ provided for backwards compatibility
__str__ = __repr__
+ @classmethod
+ def resolve_default_timeout(cls, timeout):
+ return getdefaulttimeout() if timeout is cls.DEFAULT_TIMEOUT else timeout
+
@classmethod
def _validate_timeout(cls, value, name):
"""Check that a timeout attribute is valid.
--
2.27.0