Skip to content

Commit

Permalink
Track SSL handshake state separately from calling _add_io_state direc…
Browse files Browse the repository at this point in the history
…tly.

Calling _add_io_state would set a flag that immediately gets overwritten
in _handle_events when it sees that the application-level read/write
operations are idle.  This happens to work with kqueue but not with epoll.
  • Loading branch information
bdarnell committed Feb 19, 2011
1 parent 5b8b935 commit d470b35
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tornado/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ def _handle_events(self, fd, events):
self.close()
return
state = self.io_loop.ERROR
if self._read_delimiter or self._read_bytes:
if self.reading():
state |= self.io_loop.READ
if self._write_buffer:
if self.writing():
state |= self.io_loop.WRITE
if state != self._state:
self._state = state
Expand Down Expand Up @@ -407,17 +407,27 @@ def __init__(self, *args, **kwargs):
self._ssl_options = kwargs.pop('ssl_options', {})
super(SSLIOStream, self).__init__(*args, **kwargs)
self._ssl_accepting = True
self._handshake_reading = False
self._handshake_writing = False

def reading(self):
return self._handshake_reading or super(SSLIOStream, self).reading()

def writing(self):
return self._handshake_writing or super(SSLIOStream, self).writing()

def _do_ssl_handshake(self):
# Based on code from test_ssl.py in the python stdlib
try:
self._handshake_reading = False
self._handshake_writing = False
self.socket.do_handshake()
except ssl.SSLError, err:
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
self._add_io_state(self.io_loop.READ)
self._handshake_reading = True
return
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
self._add_io_state(self.io_loop.WRITE)
self._handshake_writing = True
return
elif err.args[0] in (ssl.SSL_ERROR_EOF,
ssl.SSL_ERROR_ZERO_RETURN):
Expand Down

0 comments on commit d470b35

Please sign in to comment.