Skip to content

Commit 18ab47d

Browse files
committed
More changes for handling broken connections
Handle ECONNRESET and EPIPE correctly and simplify the code a bit (pull up conn.close() while we're at it
1 parent 6b055f4 commit 18ab47d

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

python3/httplib2/__init__.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -996,16 +996,18 @@ def _conn_request(self, conn, request_uri, method, body, headers):
996996
errno_ = (e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno)
997997
if errno_ in (errno.ENETUNREACH, errno.EADDRNOTAVAIL) and i < RETRIES:
998998
continue # retry on potentially transient errors
999+
if errno_ in (errno.ECONNRESET, errno.EPIPE) and i == 1:
1000+
conn.close()
1001+
conn.connect()
1002+
continue # retry on closed connection
9991003
raise
10001004
except http.client.HTTPException:
10011005
if conn.sock is None:
1006+
conn.close()
10021007
if i < RETRIES-1:
1003-
conn.close()
10041008
conn.connect()
10051009
continue
1006-
else:
1007-
conn.close()
1008-
raise
1010+
raise
10091011
if i < RETRIES-1:
10101012
conn.close()
10111013
conn.connect()
@@ -1027,25 +1029,22 @@ def _conn_request(self, conn, request_uri, method, body, headers):
10271029
# If we get a BadStatusLine on the first try then that means
10281030
# the connection just went stale, so retry regardless of the
10291031
# number of RETRIES set.
1032+
conn.close()
10301033
if not seen_bad_status_line and i == 1:
10311034
i = 0
10321035
seen_bad_status_line = True
1033-
conn.close()
10341036
conn.connect()
10351037
continue
1036-
else:
1037-
conn.close()
1038-
raise
1038+
raise
10391039
except socket.timeout:
10401040
raise
1041-
except (socket.error, http.client.HTTPException):
1041+
except (socket.error, http.client.HTTPException) as e:
10421042
conn.close()
1043-
if i == 0:
1044-
conn.close()
1043+
errno_ = (e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno)
1044+
if errno_ in (errno.ECONNRESET, errno.EPIPE) and i == 1:
10451045
conn.connect()
10461046
continue
1047-
else:
1048-
raise
1047+
raise
10491048
break
10501049
return (response, content)
10511050

0 commit comments

Comments
 (0)