pycom / pycom-micropython-sigfox

A fork of MicroPython with the ESP32 port customized to run on Pycom's IoT multi-network modules.
MIT License
199 stars 167 forks source link

Socket timeout not honored with SSL #315

Open capcarr opened 5 years ago

capcarr commented 5 years ago

System information: sysname='GPy' nodename='GPy' release='1.18.2.r7' version='v1.8.6-849-df9f237 on 2019-05-14' machine='GPy with ESP32'

When configuring a socket to use a timeout, if the socket has been wrapped with the ussl.wrap_socket method, the timeout is not honored.

Given a socket sock which has been successfully connected to a host, trying to call sock.read should return None if the timeout is reached and there is nothing to read in the socket:

import usocket

sock = usocket.socket()
sock.settimeout(5)

sock.connect((host, port))

t = sock.read(1)

# after timeout, t is None
print(t is None)

But when the socket is wrapped with ussl, the timeout setting is ignored and the read blocks forever:

import usocket
import ussl

sock = usocket.socket()
sock = ussl.wrap_socket(sock)
sock.settimeout(5)

sock.connect((host, port))

# this hangs indefinitely
t = sock.read(1)

This also happens at least for the readline method.

iwahdan88 commented 5 years ago

Hello @capcarr, probably your code hangs while trying to do sock.connect as this is where the handshake operation is done, so I advice when warping the socket you use the timeout option to set timeout for the handshake operation. ussl.wrap_socket(sock, timeout=<seconds>) see https://docs.pycom.io/firmwareapi/micropython/ussl/

capcarr commented 5 years ago

@iwahdan88 Thank you for your reply. Our tests show that the socket does connect, it is the read method that hangs. Does the timeout argument in the wrap_socket method affect the read and write methods on the socket, or just the SSL handshake?

iwahdan88 commented 5 years ago

@capcarr No it affects only the Handshake, I tried your example code and I can see the problem. will update you with fix Asap.

iwahdan88 commented 5 years ago

Issue fixed in 2a9f77