Skip to content

Commit 58373da

Browse files
committed
Setting timeout for HTTP: requests does not hanging forever if the download speed is zero (addressing an issue when Apple does not response to HTTP GET)
1 parent c4a6322 commit 58373da

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/icloudpd/download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import socket
77
import time
88

9-
from requests import Response
9+
from requests import Response, Timeout
1010
from requests.exceptions import ConnectionError
1111
from tzlocal import get_localzone
1212

@@ -125,7 +125,7 @@ def download_media(
125125
)
126126
break
127127

128-
except (ConnectionError, socket.timeout, PyiCloudAPIResponseException) as ex:
128+
except (ConnectionError, socket.timeout, PyiCloudAPIResponseException, Timeout) as ex:
129129
if "Invalid global session" in str(ex):
130130
logger.error("Session error, re-authenticating...")
131131
if retries > 0:

src/pyicloud_ipd/session.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def filter(self, record: logging.LogRecord) -> bool:
3535
message = record.getMessage()
3636
if self.name in message:
3737
record.msg = message.replace(self.name, "********")
38-
record.args = [] # type: ignore[assignment]
38+
record.args = [] # type: ignore[assignment]
3939

4040
return True
4141

@@ -45,11 +45,12 @@ class PyiCloudSession(Session):
4545

4646
def __init__(self, service: Any):
4747
self.service = service
48+
self._timeout = 30.0
4849
super().__init__()
4950

5051
@override
51-
# type: ignore
52-
def request(self, method: str, url, **kwargs):
52+
# type: ignore
53+
def request(self, method: str, url, **kwargs):
5354

5455
# Charge logging to the right service endpoint
5556
callee = inspect.stack()[2]
@@ -62,6 +63,8 @@ def request(self, method: str, url, **kwargs):
6263

6364
has_retried = kwargs.get("retried")
6465
kwargs.pop("retried", None)
66+
if "timeout" not in kwargs and self._timeout is not None:
67+
kwargs["timeout"] = self._timeout
6568
response = super().request(method, url, **kwargs)
6669

6770
content_type = response.headers.get("Content-Type", "").split(";")[0]
@@ -131,7 +134,7 @@ def request(self, method: str, url, **kwargs):
131134

132135
try:
133136
data = response.json() if response.status_code != 204 else {}
134-
except:
137+
except:
135138
request_logger.warning("Failed to parse response with JSON mimetype")
136139
return response
137140

0 commit comments

Comments
 (0)