You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Concurrency issues when reading and writing retain messages #200
I'm trying to embed the broker into an application, but I've noticed while testing the library, the race detector goes off when publishing and subscribing to retain messages at the same time. I believe the issue is that there's no locks here:
Sure, I have attached the log. For a bit of context, I'm trying to inject a retain message to a topic when a device connects and remove the retain message when disconnection occurs.
@mochi-co , although I haven't dive deep into the topics code, but after looking at it to add #236, I do see race conditions here.
The issue is that although the particles and particle methods are protected by the lock, but subsequent operations on the result are not. Here are some examples:
Similar issue exist in TopicsIndex.scanSubscribers.
Given both of these are only called by their respective exported method, adding lock in Messages and Subscribers as suggested by @yh742 can address this. There may be performance impact though.
@thedevop I did suspect this may be the case when I wrote it but I was hoping that it would be rare enough not to cause an issue - apparently not.
@yh742 I tried to replicate this but was unable to trigger the race condition error. It's possible I'm performing the operations in the wrong order or in insufficient scale. Do you (or @thedevop) have any example code (test or main.go) I can run to reproduce this? That would make it easier to determine the correct solution.
Activity
[-]Concurrency Issues[/-][+]Concurrency issues when reading and writing retain messages [/+]mochi-co commentedon Apr 21, 2023
Hi @yh742 - could you paste the race condition logs so I can have a closer look? Thanks!
yh742 commentedon Apr 22, 2023
Sure, I have attached the log. For a bit of context, I'm trying to inject a retain message to a topic when a device connects and remove the retain message when disconnection occurs.
thedevop commentedon Jun 12, 2023
@mochi-co , although I haven't dive deep into the topics code, but after looking at it to add #236, I do see race conditions here.
The issue is that although the particles and particle methods are protected by the lock, but subsequent operations on the result are not. Here are some examples:
TopicsIndex.scanMessages:https://github.com/mochi-co/mqtt/blob/af79b55b9f56ea49a09fea0450d3062ddbd0c13b/topics.go#L449-L464
n.particles.getAll()is protected by lock, but the for loop itself is not. Some of the particle returned in the map maybe deleted while it is iterating through the map.TopicsIndex.scanSubscribers.Given both of these are only called by their respective exported method, adding lock in
MessagesandSubscribersas suggested by @yh742 can address this. There may be performance impact though.mochi-co commentedon Jun 19, 2023
@thedevop I did suspect this may be the case when I wrote it but I was hoping that it would be rare enough not to cause an issue - apparently not.
@yh742 I tried to replicate this but was unable to trigger the race condition error. It's possible I'm performing the operations in the wrong order or in insufficient scale. Do you (or @thedevop) have any example code (test or main.go) I can run to reproduce this? That would make it easier to determine the correct solution.
Thanks!
thedevop commentedon Feb 25, 2025
Close this until we can consistently reproduce the issue.
fix(topics): read particle.retainPath under the particle lock in scan…