Skip to content
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

protect subscription dictionaries #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 50 additions & 38 deletions src/upparat/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import struct
from threading import RLock

from paho.mqtt.client import Client
from paho.mqtt.client import CONNACK_ACCEPTED
Expand Down Expand Up @@ -54,6 +55,10 @@ def __init__(self, client_id, queue):
self._subscription_mid = {}
self._unsubscription_mid = {}

self._subscriptions_lock = RLock()
self._subscription_mid_lock = RLock()
self._unsubscription_mid_lock = RLock()

super().__init__(client_id)

self.on_connect = self._on_connect_handler
Expand All @@ -73,36 +78,38 @@ def subscribe(self, topic, qos=0):
# is threaded on_unsubscribe callback can be
# called before _subscribe returns here, but we
# want to know the topic in the callback (B)
message_id = self._mid_generate()
self._subscription_mid[message_id] = topic

# We still want to keep the mapping for topic - qos
# in case the error gets fixed by a reconnect later!
self._subscriptions[topic] = qos

result, _ = self._subscribe(topic, qos=qos, mid=message_id)
if result != MQTT_ERR_SUCCESS:
# Remove mid on failure
del self._subscription_mid[message_id]
logger.warning(
f"Unable to subscribe to topic {topic}: {error_string(result)}"
)
with self._subscriptions_lock and self._subscription_mid_lock:
message_id = self._mid_generate()
self._subscription_mid[message_id] = topic

# We still want to keep the mapping for topic - qos
# in case the error gets fixed by a reconnect later!
self._subscriptions[topic] = qos

result, _ = self._subscribe(topic, qos=qos, mid=message_id)
if result != MQTT_ERR_SUCCESS:
# Remove mid on failure
del self._subscription_mid[message_id]
logger.warning(
f"Unable to subscribe to topic {topic}: {error_string(result)}"
)

return result, message_id
return result, message_id

def unsubscribe(self, topic):
self._subscriptions.pop(topic, None)
with self._subscriptions_lock and self._unsubscription_mid_lock:
self._subscriptions.pop(topic, None)

# Comment (A) also applies here.
message_id = self._mid_generate()
self._unsubscription_mid[message_id] = topic
# Comment (A) also applies here.
message_id = self._mid_generate()
self._unsubscription_mid[message_id] = topic

result, _ = self._unsubscribe(topic, mid=message_id)
result, _ = self._unsubscribe(topic, mid=message_id)

if result != MQTT_ERR_SUCCESS:
del self._unsubscription_mid[message_id]
if result != MQTT_ERR_SUCCESS:
del self._unsubscription_mid[message_id]

return result, message_id
return result, message_id

def _on_connect_handler(self, _, __, ___, rc):
message = connack_string(rc)
Expand All @@ -112,8 +119,10 @@ def _on_connect_handler(self, _, __, ___, rc):
logger.error(message)

# (Re)subscribe to topics
for topic, qos in self._subscriptions.items():
self.subscribe(topic, qos=qos)

with self._subscriptions_lock and self._subscription_mid_lock:
for topic, qos in self._subscriptions.items():
self.subscribe(topic, qos=qos)

def _on_message_handler(self, _, __, message):
self._queue.put(
Expand All @@ -131,23 +140,26 @@ def _on_subscribe_handler(self, _, __, mid, ___):
# we want to know the mid → topic mapping here
# since we want to publish an event with the topic
# that has been subscribed to.
if mid in self._subscription_mid:
self._queue.put(
Event(
MQTT_SUBSCRIBED,
**{MQTT_EVENT_TOPIC: self._subscription_mid.pop(mid)},

with self._subscriptions_lock and self._subscription_mid_lock:
if mid in self._subscription_mid:
self._queue.put(
Event(
MQTT_SUBSCRIBED,
**{MQTT_EVENT_TOPIC: self._subscription_mid.pop(mid)},
)
)
)
else:
logger.error(f"No topic mapping found for subscription {mid}")
else:
logger.error(f"No topic mapping found for subscription {mid}")

def _on_unsubscribe_handler(self, _, __, mid):
# See comment (B), same applies here.
if mid in self._unsubscription_mid:
topic = self._unsubscription_mid.pop(mid)
self._queue.put(Event(MQTT_UNSUBSCRIBED, **{MQTT_EVENT_TOPIC: topic}))
else:
logger.error(f"No topic mapping found for unsubscription {mid}")
with self._subscriptions_lock and self._unsubscription_mid_lock:
if mid in self._unsubscription_mid:
topic = self._unsubscription_mid.pop(mid)
self._queue.put(Event(MQTT_UNSUBSCRIBED, **{MQTT_EVENT_TOPIC: topic}))
else:
logger.error(f"No topic mapping found for unsubscription {mid}")

def _send_subscribe(self, dup, topics, mid=None):
""" See Paho's _send_subscribe, allow for passing a mid. """
Expand Down