-
Notifications
You must be signed in to change notification settings - Fork 138
ssl.wrap_socket()
hangs on reconnecting
#171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The I don't claim a detailed knowledge of the In terms of actions the best option (once I understand it) is for me to document this. I think specifying |
Unfortunately I don't have much knowledge about the Here is briefly what I found: In Micropython Doc, the following statement particularly made me try setting
That's why I tried to set The Micropython doc didn't say much more about this flag. It referred to CPython's Note: Micropython's |
The MP doc states
This seems to imply:
Under your conditions of poor connectivity, and with The reason I ask is that, in ._connect() the code performs the following steps:
I'd therefore expect setting |
I agree that the MP doc implies setting
I tried the above senario on ESP32S3. I simulated a bad connection by setting MQTT server to I'm out this week. I'll come up with some example code to demonstrate the behavior and do some further testing next week. For instance, is the blocking behavior as documented if |
I'll be very interested to see your results. I gather some ports use axtls and others mbedtls. The behaviour of non-blocking sockets under TLS is poorly documented. I will document your observations (with attribution). A factor to bear in mind is that I don't think this has any bearing on your observations but the behaviour has caused confusion in the past. |
This doc may be relevant. I wonder if |
In this branch I added some testing code to show the difference. Also a pull-request to show the diff. Here's the logs when toggling # do_handshake = True
❯ mpr -m . run mqtt_as/test_ssl_blocking.py
Local directory . is mounted at /remote
Connecting to MQTT broker, Current ticks_ms: 862177
Performing another task. tick_ms: 862180
Performing another task. tick_ms: 865190
Performing another task. tick_ms: 868200 # These prints are printed before entering `ssl.wrap_socket`. Then it blocks for 20+ seconds.
Connect() finished/aborted. Current ticks_ms: 886740 Time taken: 24563 ms
Traceback (most recent call last):
File "<stdin>", line 42, in <module>
File "asyncio/core.py", line 1, in run
File "asyncio/core.py", line 1, in run_until_complete
File "asyncio/core.py", line 1, in run_until_complete
File "<stdin>", line 36, in main
File "mqtt_as/__init__.py", line 800, in connect
File "mqtt_as/__init__.py", line 315, in _connect
File "ssl.py", line 1, in wrap_socket
File "ssl.py", line 1, in wrap_socket
OSError: [Errno 113] ECONNABORTED # This is the error raised by the SSL library
# do_handshake = False
❯ mpr -m . run mqtt_as/test_ssl_blocking.py
Local directory . is mounted at /remote
Connecting to MQTT broker, Current ticks_ms: 924911
Performing another task. tick_ms: 924913
Performing another task. tick_ms: 927920
Performing another task. tick_ms: 930930
Performing another task. tick_ms: 933930
Performing another task. tick_ms: 936930
Performing another task. tick_ms: 939930
Connect() finished/aborted. Current ticks_ms: 941156 Time taken: 16245 ms
Traceback (most recent call last):
File "<stdin>", line 42, in <module>
File "asyncio/core.py", line 1, in run
File "asyncio/core.py", line 1, in run_until_complete
File "asyncio/core.py", line 1, in run_until_complete
File "<stdin>", line 36, in main
File "mqtt_as/__init__.py", line 800, in connect
File "mqtt_as/__init__.py", line 341, in _connect
File "mqtt_as/__init__.py", line 276, in _as_write
OSError: (-1, 'Timeout on socket write') # Timeout raised by mqtt_as code I ran the code on ESP32S3. |
My theory after reading your linked doc regarding handshake mode, I thought it might be interesting to try to put a fake CA cert and see what happens (to my real MQTT server on AWS IoT Core). And the result is surprising. |
Thanks for that interesting result. Setting
I'm unclear whether the test clarifies the second point. The MQTT client attempts a socket write immediately after connecting, but that write fails: whether a handshake was attempted is moot. You might try the test on a public MQTT broker which supports TLS: with |
In my particular case, handshake is usually fast enough when the connection quality is good. My concern is rather when the server fails to respond. When having My application actually uses AWS IoT core. When the connection quality is good, I don't notice any blockage when using |
If the internet quality isn't good, sometimes during reconnection,
ssl.wrap_socket()
blocks. The usually causes other coroutines to stop responding. In my case, this triggers WatchDog Timer to restart the MCU.I found setting
do_handshake = False
inssl_params
solves the issue, aswrap_socket()
doesn't try to do the handshake anymore.My question is:
Is this expected behavior?
If so, would you kindly add it to the document, or make
do_handshake = False
the default behavior?Much appreciated for the great project!
The text was updated successfully, but these errors were encountered: