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

Commit 9a9b048

Browse files
committed
Merge pull request #221 from tbetbetbe/development-ssl-connection-error-reduction
Improve handling of socket errors
2 parents 0837669 + 1d1d981 commit 9a9b048

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Cory Benfield
3+
Copyright (c) 2014 Cory Benfield, Google Inc
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

hyper/http20/connection.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import h2.events
1010
import h2.settings
1111

12+
from ..compat import ssl
1213
from ..tls import wrap_socket, H2_NPN_PROTOCOLS, H2C_PROTOCOL
1314
from ..common.exceptions import ConnectionResetError
1415
from ..common.bufsocket import BufferedSocket
@@ -237,7 +238,7 @@ def connect(self):
237238
host = self.proxy_host
238239
port = self.proxy_port
239240

240-
sock = socket.create_connection((host, port), 5)
241+
sock = socket.create_connection((host, port))
241242

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

526528
while count and self._sock is not None and self._sock.can_read:
527-
# If the connection has been closed, bail out.
529+
# If the connection has been closed, bail out, but retry
530+
# on transient errors.
528531
try:
529532
self._single_read()
530533
except ConnectionResetError:
531534
break
535+
except ssl.SSLError as e: # pragma: no cover
536+
# these are transient errors that can occur while reading from
537+
# ssl connections.
538+
if e.args[0] in (ssl.SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE):
539+
continue
540+
else:
541+
raise
542+
except socket.error as e: # pragma: no cover
543+
if e.errno in (errno.EINTR, errno.EAGAIN):
544+
# if 'interrupted' or 'try again', continue
545+
sleep(retry_wait)
546+
continue
547+
elif e.errno == errno.ECONNRESET:
548+
break
549+
else:
550+
raise
532551

533552
count -= 1
534553

0 commit comments

Comments
 (0)