Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch socket timeouts and clear dead connection (refs #18) #111

Merged
merged 6 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion python2/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,8 @@ def request(
being and instance of the 'Response' class, the second being
a string that contains the response entity body.
"""
conn_key = ''

try:
if headers is None:
headers = {}
Expand Down Expand Up @@ -2133,13 +2135,19 @@ def request(
cachekey,
)
except Exception as e:
is_timeout = isinstance(e, socket.timeout)
if is_timeout:
conn = self.connections.pop(conn_key, None)
if conn:
conn.close()

if self.force_exception_to_status_code:
if isinstance(e, HttpLib2ErrorWithResponse):
response = e.response
content = e.content
response.status = 500
response.reason = str(e)
elif isinstance(e, socket.timeout):
elif is_timeout:
content = "Request Timeout"
response = Response(
{
Expand Down
8 changes: 8 additions & 0 deletions python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,8 @@ def request(
being and instance of the 'Response' class, the second being
a string that contains the response entity body.
"""
conn_key = ''

try:
if headers is None:
headers = {}
Expand Down Expand Up @@ -1924,6 +1926,12 @@ def request(
cachekey,
)
except Exception as e:
is_timeout = isinstance(e, socket.timeout)
if is_timeout:
conn = self.connections.pop(conn_key, None)
if conn:
conn.close()

if self.force_exception_to_status_code:
if isinstance(e, HttpLib2ErrorWithResponse):
response = e.response
Expand Down
26 changes: 26 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ def handler(request):
assert response.reason.startswith("Request Timeout")


def test_timeout_subsequent():
class Handler(object):
number = 0

@classmethod
def handle(cls, request):
# request.number is always 1 because of
# the new socket connection each time
cls.number += 1
if cls.number % 2 != 0:
time.sleep(0.6)
return tests.http_response_bytes(status=500)
return tests.http_response_bytes(status=200)

http = httplib2.Http(timeout=0.5)
http.force_exception_to_status_code = True

with tests.server_request(Handler.handle, request_count=2) as uri:
response, _ = http.request(uri)
assert response.status == 408
assert response.reason.startswith("Request Timeout")

response, _ = http.request(uri)
assert response.status == 200


def test_timeout_https():
c = httplib2.HTTPSConnectionWithTimeout("localhost", 80, timeout=47)
assert 47 == c.timeout
Expand Down