Skip to content

Commit ae9cd99

Browse files
authoredJan 13, 2021
fix: Catch ECONNRESET and other errors more reliably (#1147)
Fixes #1146 🦕
1 parent 70bc6c9 commit ae9cd99

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed
 

‎googleapiclient/http.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@
2828
from six import BytesIO, StringIO
2929
from six.moves.urllib.parse import urlparse, urlunparse, quote, unquote
3030

31-
import base64
3231
import copy
33-
import gzip
3432
import httplib2
3533
import json
3634
import logging
3735
import mimetypes
3836
import os
3937
import random
4038
import socket
41-
import sys
4239
import time
4340
import uuid
4441

@@ -190,11 +187,16 @@ def _retry_request(
190187
exception = connection_error
191188
except socket.error as socket_error:
192189
# errno's contents differ by platform, so we have to match by name.
190+
# Some of these same errors may have been caught above, e.g. ECONNRESET *should* be
191+
# raised as a ConnectionError, but some libraries will raise it as a socket.error
192+
# with an errno corresponding to ECONNRESET
193193
if socket.errno.errorcode.get(socket_error.errno) not in {
194194
"WSAETIMEDOUT",
195195
"ETIMEDOUT",
196196
"EPIPE",
197197
"ECONNABORTED",
198+
"ECONNREFUSED",
199+
"ECONNRESET",
198200
}:
199201
raise
200202
exception = socket_error

‎tests/test_http.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ def request(self, *args, **kwargs):
174174
# set errno to a non-retriable value
175175
try:
176176
# For Windows:
177-
ex.errno = socket.errno.WSAECONNREFUSED
177+
ex.errno = socket.errno.WSAEHOSTUNREACH
178178
except AttributeError:
179179
# For Linux/Mac:
180-
ex.errno = socket.errno.ECONNREFUSED
180+
ex.errno = socket.errno.EHOSTUNREACH
181181
# Now raise the correct timeout error.
182182
raise ex
183183

0 commit comments

Comments
 (0)