pyca/pyopenssl

connection.settimeout not work

Closed this issue · 2 comments

pyOpenSSL version is: 22.0.0
openssl version is: 1.1.1j
OS: ubuntu 18.04
Python 3.6.9

when I called "connection.setblocking(True) ", the time out is invalid:

import OpenSSL
import socket
ctx = OpenSSL.SSL.Context(OpenSSL.SSL.TLS_CLIENT_METHOD)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

connection = OpenSSL.SSL.Connection(ctx, s)
connection.set_tlsext_host_name(b"www.daiyue.gov.cn")

connection.connect(("www.daiyue.gov.cn", 443))
connection.settimeout(3)

connection.setblocking(True)

connection.do_handshake()

^CTraceback (most recent call last):
File "", line 1, in
File "/home/yangyuqi/.local/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1990, in do_handshake
result = _lib.SSL_do_handshake(self._ssl)
KeyboardInterrupt

when I not called "connection.setblocking(True) ", the connection.do_handshake() is abnormal

ctx = OpenSSL.SSL.Context(OpenSSL.SSL.TLS_CLIENT_METHOD)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection = OpenSSL.SSL.Connection(ctx, s)
connection.set_tlsext_host_name(b"www.daiyue.gov.cn")
#connection.set_connect_state()

connection.connect(("www.daiyue.gov.cn", 443))
connection.settimeout(3)

connection.do_handshake()
Traceback (most recent call last):
File "", line 1, in
File "/home/yangyuqi/.local/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1991, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "/home/yangyuqi/.local/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1675, in _raise_ssl_error
raise WantReadError()
OpenSSL.SSL.WantReadError

The reason why the timeout is not working in the first fragment of code:

import OpenSSL
import socket
ctx = OpenSSL.SSL.Context(OpenSSL.SSL.TLS_CLIENT_METHOD)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

connection = OpenSSL.SSL.Connection(ctx, s)
connection.set_tlsext_host_name(b"www.daiyue.gov.cn")

connection.connect(("www.daiyue.gov.cn", 443)) # script blocks here
connection.settimeout(3) # timeout is set here

connection.setblocking(True)

connection.do_handshake()

is that the script blocks on the connect() call, so the timeout is never actually set. By putting the connect() call after setting the timeout, you get the expected TimeoutError exception.

@alex I think this one can be closed

mhils commented

Thanks @facutuesca! 🍰