ussl.wrap_socket causes esp32-s2 board to crash
Here is a minimal reproducible code:
import usocket
import ussl
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, 0)
s.connect( ('34.196.233.7', 443))
new_s = ussl.wrap_socket(s) # crashes
This causes a crash in ESP32-S2. It however works in ESP32.
micropython version: v1.17 on 2021-09-02.
The crash results in the following error on REPL:
Traceback (most recent call last):
File "c:\users\rajen\anaconda3\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\rajen\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\rajen\anaconda3\Scripts\mpremote.exe\__main__.py", line 7, in <module>
File "c:\users\rajen\anaconda3\lib\site-packages\mpremote\main.py", line 414, in main
pyb.enter_raw_repl()
File "c:\users\rajen\anaconda3\lib\site-packages\mpremote\pyboard.py", line 337, in enter_raw_repl
data = self.read_until(1, b"raw REPL; CTRL-B to exit\r\n>")
File "c:\users\rajen\anaconda3\lib\site-packages\mpremote\pyboard.py", line 310, in read_until
elif self.serial.inWaiting() > 0:
File "c:\users\rajen\anaconda3\lib\site-packages\serial\serialutil.py", line 594, in inWaiting
return self.in_waiting
File "c:\users\rajen\anaconda3\lib\site-packages\serial\serialwin32.py", line 259, in in_waiting
raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError()))
serial.serialutil.SerialException: ClearCommError failed (PermissionError(13, 'The device does not recognize the command.', None, 22))
ussl.wrap_socket fails with mbedtls_ssl_handshake errors on ESP32
In some cases, the ussl.wrap_socket function fails with a nondescript error:
>>> import ussl, usocket
>>> s = usocket.socket()
>>> s.connect(('185.8.236.214', 443))
>>> s = ussl.wrap_socket(s, server_hostname="aladinonline.androworks.org")
mbedtls_ssl_handshake error: -4290
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 5] EIO
This doesn't seem to be a network problem as requests to other pages work well in the same session. This is in fact the only page I've run into this issue with, and I can't figure out what's wrong with its TLS setup.
I initially ran into this when using the urllib.urequest.urlopen function and I believed this was a micropython-lib bug, so I didn't post the issue here (original issue was micropython/micropython-lib#374), but I found out later that the bug wasn't present in the Micropython version 1.12 and that it was probably introduced by commit 3032ae115, which switches the version of ESP-IDF used to build Micropython for ESP32. Even though I managed to replicate the bug only using the undocumented parameter server_hostname of wrap_socket function, I really think this is a bug in Micropython (or some underlying library), not in micropython-lib.