Skip to content
Permalink
Browse files

fix: Catch ECONNRESET and other errors more reliably (#1147)

Fixes #1146  🦕
  • Loading branch information
dossett committed Jan 13, 2021
1 parent 70bc6c9 commit ae9cd99134160a5540e6f8d6d33d855122854e10
Showing with 7 additions and 5 deletions.
  1. +5 −3 googleapiclient/http.py
  2. +2 −2 tests/test_http.py
@@ -28,17 +28,14 @@
from six import BytesIO, StringIO
from six.moves.urllib.parse import urlparse, urlunparse, quote, unquote

import base64
import copy
import gzip
import httplib2
import json
import logging
import mimetypes
import os
import random
import socket
import sys
import time
import uuid

@@ -190,11 +187,16 @@ def _retry_request(
exception = connection_error
except socket.error as socket_error:
# errno's contents differ by platform, so we have to match by name.
# Some of these same errors may have been caught above, e.g. ECONNRESET *should* be
# raised as a ConnectionError, but some libraries will raise it as a socket.error
# with an errno corresponding to ECONNRESET
if socket.errno.errorcode.get(socket_error.errno) not in {
"WSAETIMEDOUT",
"ETIMEDOUT",
"EPIPE",
"ECONNABORTED",
"ECONNREFUSED",
"ECONNRESET",
}:
raise
exception = socket_error
@@ -174,10 +174,10 @@ def request(self, *args, **kwargs):
# set errno to a non-retriable value
try:
# For Windows:
ex.errno = socket.errno.WSAECONNREFUSED
ex.errno = socket.errno.WSAEHOSTUNREACH
except AttributeError:
# For Linux/Mac:
ex.errno = socket.errno.ECONNREFUSED
ex.errno = socket.errno.EHOSTUNREACH
# Now raise the correct timeout error.
raise ex

0 comments on commit ae9cd99

Please sign in to comment.