33# prefer slower Python-based io module
44import _pyio as io
55import socket
6+ import time
7+ from OpenSSL import SSL
68
79
810# Write only 16K at a time to sockets
@@ -31,7 +33,11 @@ def _flush_unlocked(self):
3133 # so perhaps we should conditionally wrap this for perf?
3234 n = self .raw .write (bytes (self ._write_buf ))
3335 except io .BlockingIOError as e :
34- n = e .characters_written
36+ n = e .characters_writteni
37+ except (SSL .WantReadError ,SSL .WantWriteError , SSL .WantX509LookupError ) as e :
38+ # these errors require retries with the same data
39+ # if some data has already been written
40+ n = 0
3541 del self ._write_buf [:n ]
3642
3743
@@ -45,9 +51,15 @@ def __init__(self, sock, mode='r', bufsize=io.DEFAULT_BUFFER_SIZE):
4551
4652 def read (self , * args , ** kwargs ):
4753 """Capture bytes read."""
48- val = super ().read (* args , ** kwargs )
49- self .bytes_read += len (val )
50- return val
54+ while True :
55+ try :
56+ val = super ().read (* args , ** kwargs )
57+ self .bytes_read += len (val )
58+ return val
59+ except SSL .WantReadError :
60+ time .sleep (0.1 ) # allow some retry delay
61+ except SSL .WantWriteError :
62+ time .sleep (0.1 )
5163
5264 def has_data (self ):
5365 """Return true if there is buffered data to read."""
0 commit comments