Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fff3ae5

Browse files
thehesiodJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Fix retying on socket.timeout (googleapis#495)
1 parent 3f7aa7b commit fff3ae5

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

googleapiclient/http.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,14 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
163163
# Retry on SSL errors and socket timeout errors.
164164
except _ssl_SSLError as ssl_error:
165165
exception = ssl_error
166+
except socket.timeout as socket_timeout:
167+
# It's important that this be before socket.error as it's a subclass
168+
# socket.timeout has no errorcode
169+
exception = socket_timeout
166170
except socket.error as socket_error:
167171
# errno's contents differ by platform, so we have to match by name.
168-
if socket.errno.errorcode.get(socket_error.errno) not in (
169-
'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED', ):
172+
if socket.errno.errorcode.get(socket_error.errno) not in {
173+
'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED'}:
170174
raise
171175
exception = socket_error
172176

tests/test_http.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,26 @@ def request(self, *args, **kwargs):
125125
return httplib2.Response(self.success_json), self.success_data
126126
else:
127127
self.num_errors -= 1
128-
if self.num_errors == 1:
128+
if self.num_errors == 1: # initial == 2
129129
raise ssl.SSLError()
130-
else:
131-
if PY3:
132-
ex = TimeoutError()
133-
else:
134-
ex = socket.error()
130+
else: # initial != 2
135131
if self.num_errors == 2:
136-
#first try a broken pipe error (#218)
132+
# first try a broken pipe error (#218)
133+
ex = socket.error()
137134
ex.errno = socket.errno.EPIPE
138135
else:
139136
# Initialize the timeout error code to the platform's error code.
140137
try:
141138
# For Windows:
139+
ex = socket.error()
142140
ex.errno = socket.errno.WSAETIMEDOUT
143141
except AttributeError:
144142
# For Linux/Mac:
145-
ex.errno = socket.errno.ETIMEDOUT
143+
if PY3:
144+
ex = socket.timeout()
145+
else:
146+
ex = socket.error()
147+
ex.errno = socket.errno.ETIMEDOUT
146148
# Now raise the correct error.
147149
raise ex
148150

0 commit comments

Comments
 (0)