adafruit / adafruit/Adafruit_CircuitPython_MiniMQTT

_wait_for_msg() on CPython with TLS connection raises ssl.SSLWantReadError on timeout

Open
#154 6 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
81
Forks
52
PR merge metrics
No merged PRs in 30d

Description

When connected via TLS, i.e. the socket is wrapped using the `ssl_context` and if there is nothing to be read in the loop, it results in:
```
Traceback (most recent call last):
File "c.py", line 132, in
mqtt_client.loop()
File "/Users/vladimirkotal/Pi/Adafruit_CircuitPython_MiniMQTT/adafruit_minimqtt/adafruit_minimqtt.py", line 955, in loop
rc = self._wait_for_msg(timeout)
File "/Users/vladimirkotal/Pi/Adafruit_CircuitPython_MiniMQTT/adafruit_minimqtt/adafruit_minimqtt.py", line 976, in _wait_for_msg
res = self._sock_exact_recv(1)
File "/Users/vladimirkotal/Pi/Adafruit_CircuitPython_MiniMQTT/adafruit_minimqtt/adafruit_minimqtt.py", line 1077, in _sock_exact_recv
recv_len = self._sock.recv_into(rc, bufsize)
File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1242, in recv_into
return self.read(nbytes, buffer)
File "/usr/local/Cellar/python@3.9/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1100, in read
return self._sslobj.read(len, buffer)
ssl.SSLWantReadError: The operation did not complete (read) (_ssl.c:2633)
```

In such case, `_wait_for_msg()` should return `None`, however the ssl exception fires through: https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/blob/342b8c99b2c8f10b605cc8311bf5f394f4d18e92/adafruit_minimqtt/adafruit_minimqtt.py#L967-L972

Note that the line numbers do not match because I am using minimqtt code for one of my PRs, however that should not matter.

Trouble is that the `ssl.SSLWantReadError` does not seem to be reachable from the ssl context passed to `MQTT()` init function. The exception class is based on `OSError`, however catching that would be too generic, I think.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in adafruit_minimqtt/adafruit_minimqtt.py at _wait_for_msg() and _sock_exact_recv(), following the TLS socket path described in the traceback. Reproduce an idle TLS connection timeout and determine how the SSLWantReadError should be handled so _wait_for_msg() returns None without catching unrelated OSError cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.