Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Improve handling of socket errors #221

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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Cory Benfield
Copyright (c) 2014 Cory Benfield, Google Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 21 additions & 2 deletions hyper/http20/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import h2.events
import h2.settings

from ..compat import ssl
from ..tls import wrap_socket, H2_NPN_PROTOCOLS, H2C_PROTOCOL
from ..common.exceptions import ConnectionResetError
from ..common.bufsocket import BufferedSocket
Expand Down Expand Up @@ -237,7 +238,7 @@ def connect(self):
host = self.proxy_host
port = self.proxy_port

sock = socket.create_connection((host, port), 5)
sock = socket.create_connection((host, port))

if self.secure:
assert not self.proxy_host, "Using a proxy with HTTPS not yet supported."
Expand Down Expand Up @@ -522,13 +523,31 @@ def _recv_cb(self):
# TODO: Re-evaluate this.
self._single_read()
count = 9
retry_wait = 0.05 # can improve responsiveness to delay the retry

while count and self._sock is not None and self._sock.can_read:
# If the connection has been closed, bail out.
# If the connection has been closed, bail out, but retry
# on transient errors.
try:
self._single_read()
except ConnectionResetError:
break
except ssl.SSLError as e: # pragma: no cover
# these are transient errors that can occur while reading from
# ssl connections.
if e.args[0] in (ssl.SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE):
continue
else:
raise
except socket.error as e: # pragma: no cover
if e.errno in (errno.EINTR, errno.EAGAIN):
# if 'interrupted' or 'try again', continue
sleep(retry_wait)
continue
elif e.errno == errno.ECONNRESET:
break
else:
raise

count -= 1

Expand Down