Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75e0338

Browse files
committed
Issue #21430: additions to the description of non-blocking SSL sockets
1 parent 5eb6b39 commit 75e0338

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

Doc/library/ssl.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,8 +1588,19 @@ the sockets in non-blocking mode and use an event loop).
15881588
Notes on non-blocking sockets
15891589
-----------------------------
15901590

1591-
When working with non-blocking sockets, there are several things you need
1592-
to be aware of:
1591+
SSL sockets behave slightly different than regular sockets in
1592+
non-blocking mode. When working with non-blocking sockets, there are
1593+
thus several things you need to be aware of:
1594+
1595+
- Most :class:`SSLSocket` methods will raise either
1596+
:exc:`SSLWantWriteError` or :exc:`SSLWantReadError` instead of
1597+
:exc:`BlockingIOError` if an I/O operation would
1598+
block. :exc:`SSLWantReadError` will be raised if a read operation on
1599+
the underlying socket is necessary, and :exc:`SSLWantWriteError` for
1600+
a write operation on the underlying socket. Note that attempts to
1601+
*write* to an SSL socket may require *reading* from the underlying
1602+
socket first, and attempts to *read* from the SSL socket may require
1603+
a prior *write* to the underlying socket.
15931604

15941605
- Calling :func:`~select.select` tells you that the OS-level socket can be
15951606
read from (or written to), but it does not imply that there is sufficient
@@ -1598,8 +1609,14 @@ to be aware of:
15981609
and :meth:`SSLSocket.send` failures, and retry after another call to
15991610
:func:`~select.select`.
16001611

1612+
- Conversely, since the SSL layer has its own framing, a SSL socket may
1613+
still have data available for reading without :func:`~select.select`
1614+
being aware of it. Therefore, you should first call
1615+
:meth:`SSLSocket.recv` to drain any potentially available data, and then
1616+
only block on a :func:`~select.select` call if still necessary.
1617+
16011618
(of course, similar provisions apply when using other primitives such as
1602-
:func:`~select.poll`)
1619+
:func:`~select.poll`, or those in the :mod:`selectors` module)
16031620

16041621
- The SSL handshake itself will be non-blocking: the
16051622
:meth:`SSLSocket.do_handshake` method has to be retried until it returns

0 commit comments

Comments
 (0)