Skip to content

Commit cbdc474

Browse files
jeblairJames E. Blair
andauthored
fix(core): handle SSL_WANT_READ/WRITE errors (#619)
This adds a simple recovery path in case an SSL connection receives an SSL_WANT_READ or WRITE error. Either error can occur while reading or writing. The error indicates that the underlying operation should be retried after the socket is once again readable or writable (per the error code). Closes #618 Co-authored-by: James E. Blair <[email protected]>
1 parent 4e32e4d commit cbdc474

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

kazoo/protocol/connection.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import random
77
import select
88
import socket
9+
import ssl
910
import sys
1011
import time
1112

@@ -247,7 +248,14 @@ def _read(self, length, timeout):
247248
# have to check wlist and xlist as we don't set any
248249
raise self.handler.timeout_exception(
249250
"socket time-out during read")
250-
chunk = self._socket.recv(remaining)
251+
try:
252+
chunk = self._socket.recv(remaining)
253+
except ssl.SSLError as e:
254+
if e.errno in (ssl.SSL_ERROR_WANT_READ,
255+
ssl.SSL_ERROR_WANT_WRITE):
256+
continue
257+
else:
258+
raise
251259
if chunk == b'':
252260
raise ConnectionDropped('socket connection broken')
253261
msgparts.append(chunk)
@@ -319,7 +327,14 @@ def _write(self, msg, timeout):
319327
raise self.handler.timeout_exception("socket time-out"
320328
" during write")
321329
msg_slice = buffer(msg, sent)
322-
bytes_sent = self._socket.send(msg_slice)
330+
try:
331+
bytes_sent = self._socket.send(msg_slice)
332+
except ssl.SSLError as e:
333+
if e.errno in (ssl.SSL_ERROR_WANT_READ,
334+
ssl.SSL_ERROR_WANT_WRITE):
335+
continue
336+
else:
337+
raise
323338
if not bytes_sent:
324339
raise ConnectionDropped('socket connection broken')
325340
sent += bytes_sent

0 commit comments

Comments
 (0)