This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fix bug read() would hang on ssl socket if settimeout() used
Type: Stage:
Components: Extension Modules Versions: Python 2.4
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: arekm, georg.brandl
Priority: normal Keywords: patch

Created on 2005-12-14 23:14 by arekm, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-ssl-nonblocking.patch arekm, 2005-12-14 23:14 Fix reading from ssl socket when timeout was set
python-ssl-nonblocking.patch arekm, 2005-12-15 17:28 Cleaner and smaller version of ssl reading fix
Messages (3)
msg49200 - (view) Author: Arkadiusz Miśkiewicz (arekm) Date: 2005-12-14 23:14
The problem has occured many times before like bugs

https://sourceforge.net/tracker/?group_id=5470&atid=
105470&func=detail&aid=1353269

https://sourceforge.net/tracker/?group_id=5470&atid=
105470&func=detail&aid=977680

https://sourceforge.net/tracker/index.php?func=detail&;
aid=1098618&group_id=5470&atid=105470

This also fixes this bug:
https://sourceforge.net/tracker/index.php?func=detail&;
aid=1166206&group_id=5470&atid=105470

Example test:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30.0)
# connect to service which issues an welcome banner 
(without need to write anything)
s.connect(("gmail.org", 995))
ss = socket.ssl(s)
# read part of return welcome banner twice,# read part 
of return welcome banner twice
ss.read(1)
ss.read(1)
s.close()

it will cause
socket.sslerror: The read operation timed out         
on the second read()

This is because _ssl.so modules doesn't handle SSL 
reads properly. The problem is in Modules/_ssl.c:
PySSL_SSLread() we have:

        sockstate = check_socket_and_wait_for_timeout
(self->Socket, self->ssl, 0); // XXXX HERE XXX
        if (sockstate == SOCKET_HAS_TIMED_OUT) {
                PyErr_SetString(PySSLErrorObject, "The 
read operation timed out");
                Py_DECREF(buf);
                return NULL;
        }
        do {....

What will happen if SSL layer already read data and 
have that data in it's own buffers? The function check_
socket_and_wait_for_timeout() doing select(readfds) 
will wait forever until timeout occurs. The solution 
is to use http://www.openssl.org/docs/ssl/SSL_pending.
html function.

The attached patch fixes the problem and also adds 
test for python test suite.
                

msg49201 - (view) Author: Arkadiusz Miśkiewicz (arekm) Date: 2005-12-15 17:28
Logged In: YES 
user_id=139606


msg49202 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-03-31 18:01
Logged In: YES 
user_id=849994

Committed as rev. 43491, 43492. Thanks!
History
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42693
2005-12-14 23:14:53arekmcreate