Skip to content

TheadSanitizer report a data race on test 03-publish-loop-start. #3564

@9iang22

Description

@9iang22

Description

ThreadSanitizer flags a data race in 03-publish-loop-start. While functionally harmless, it is noisy and demonstrates an unsafe concurrency pattern to users of libmosquitopp.

Related Code

cat ./test/lib/cpp/03-publish-loop-start.cpp

// ...
#include <mosquitto/libmosquittopp.h>  

static int run = -1; // <-- Shared without synchronization  

class mosquittopp_test : public mosqpp::mosquittopp  
{  
public:  
    mosquittopp_test(const char *id) : mosqpp::mosquittopp(id) {}  
    void on_connect_v5(int rc, int flags, const mosquitto_property *properties) override;  
    void on_disconnect_v5(int rc, const mosquitto_property *properties) override;  
    void on_subscribe_v5(int mid, int qos_count, const int *granted_qos, const mosquitto_property *props) override;  
    void on_message_v5(const struct mosquitto_message *msg, const mosquitto_property *properties) override;  
};  

// ... callbacks ...  

void mosquittopp_test::on_disconnect_v5(int rc, const mosquitto_property *properties)  
{  
    (void)properties;  
    run = rc; // <-- WRITE (network thread)  
}  

int main(int argc, char *argv[])  
{  
    // ... init & connect ...  
    mosq->loop_start();  
    struct timespec tv = { 0, (long)50e6 };  
    while(run == -1){ // <-- READ (main thread)  
        nanosleep(&tv, NULL);  
    }  
    // ... cleanup ...  
}  

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: AvailableNo one has claimed responsibility for resolving this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions