eclipse-paho / eclipse-paho/paho.mqtt.python
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2483)
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
My Env is:
```
Python 3.9.7 (default, Sep 16 2021, 08:50:36)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
paho-mqtt==1.6.1
```
I am trying to build an application that receives events/messages from multiple sources and at a fairly high rate (2000 - 3000 msgs/sec). My application does some data massaging and publishes it to ActiveMQ.
For few messages everything works well, however for few hundred messages things start falling apart and I get following error messages:
```
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/ssl.py", line 1173, in send
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/threading.py", line 973, in _bootstrap_inner
return self.loop_write()
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/site-packages/paho/mqtt/client.py", line 1577, in loop_write
Exception in thread ws_msg_handler:
Traceback (most recent call last):
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/threading.py", line 973, in _bootstrap_inner
return self._sslobj.write(data)
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2483)
rc = self._packet_write()
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/site-packages/paho/mqtt/client.py", line 649, in _sock_send
rc = self._packet_write()
self.run()
mqtt_client.publish_message(get_topic_name(kyra_msg), orjson.dumps(kyra_msg))
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/site-packages/paho/mqtt/client.py", line 2464, in _packet_write
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/site-packages/paho/mqtt/client.py", line 2464, in _packet_write
return self._sock.send(buf)
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/ssl.py", line 1173, in send
self.run()
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/threading.py", line 910, in run
write_length = self._sock_send(
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/site-packages/paho/mqtt/client.py", line 649, in _sock_send
File "/Users/ankurpandey/Documents/projects/guardhat/ss-intl/integration/./libs/kyra_mqtt_client.py", line 46, in publish_message
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/threading.py", line 910, in run
write_length = self._sock_send(
File "/Users/ankurpandey/opt/anaconda3/envs/slatesafety/lib/python3.9/site-packages/paho/mqtt/client.py", line 649, in _sock_send
self._target(*self._args, **self._kwargs)
File "/Users/ankurpandey/Documents/projects/guardhat/ss-intl/integration/./libs/slate_safety/ss_message_handler.py", line 23, in process_ss_message
```
The code that is sending the data to ActiveMQ is as follows:
```
def on_message(self, wsapp, message):
"""
handles message from Websocket
"""
payload = orjson.loads(message)
logger.debug("message received from server: " + str(payload))
threading.Thread(target=process_ss_message,
name='ws_msg_handler',
kwargs={"message": payload} ).start()
```
AND:
```
def process_ss_message(message):
start = time()
target_msg = message_translater_to_target(message)
logger.debug("msg::" + str(kyra_msg))
mqtt_client = SSMQTTClient({})
mqtt_client.publish_message(get_topic_name(target_msg), orjson.dumps(target_msg))
logger.debug(f"Message conversion and processing time: {(time() - start) * 1000} ms")
```
I did a quick google search to resolve this, and one of the solution was to use `pyOpenSSL`
So I made a quick change in `lib/python3.9/site-packages/paho/mqtt/client.py` as follows:
```
import collections
import errno
import os
import platform
import select
import socket
ssl = None
try:
# import ssl
import urllib3.contrib.pyopenssl as ssl
except ImportError:
pass
```
After this I didn't see this error and I even increased the message consumption rate.
**It may not be a bug.** But I am trying to understand the reason behind it and how can I avoid it without changing the code of the library. I tried searching this in previous issues but was unable to find.
Contributor guide
Assessment
This issue has not been assessed yet.