vendredi 16 juillet 2021

Crash due to multithreading and mutexes

This code crashes at **std::lock_guard<std::recursive_mutex> lock(mutex_);** during the handleNotif() function call with the following message due to SIGABORT. Where is the vulnerability ? Thank you.

std::recursive_mutex::lock() ??:? _gthread_recursive_mutex_lock gthr-default.h:811 std::recursive_mutex::lock() mutex:106

 NSubsPtr SuManager::subscribe(
        cons NType& notifType, callB callB)
    {
        auto subData = SubData(notifType, callB);
        {
            std::lock_guard<std::recursive_mutex> lock(mutex_);
            subscribs_.insert(std::make_pair(currentSubId_, subData ));
        }
    
        auto unSub = [this, currentId = currentSubscriptionId_]()
            {
                unsubscribe(currentId);
            };
        return std::make_shared<NotifSub>(unSub );
    }
    
    void SubManager::unsubscribe(SubscriptionId id)
    {
        std::lock_guard<std::recursive_mutex> lock(mutex_);
        auto it = std::find_if(subscribs_.begin(), subscribs_.end(),
            [&id](const std::pair<SubscriptionId, SubData>& data)
            {
                return data.first == id;
            });
        if (it == subscribs_.end())
        {
            return;
        }
        it->second.toBeRemoved = true;
    }
    
    void SubManager::handleNotif(const struct Notif* notif)
    {
        std::map<int, SubData>::iterator it;
        {
            std::lock_guard<std::recursive_mutex> lock(mutex_);
            it = subscribs_.begin();
        }
        for (;;)
        {
            std::lock_guard<std::recursive_mutex> lock(mutex_);
            if (it == subscribs_.end())
            {
                break;
            }
            if (it->second.toBeRemoved)
            {
                it = subscribs_.erase(it);
                continue;
            }
    
            // .....
            it->second.subscribeCallback(..);
            ++it;
        }
    }

Aucun commentaire:

Enregistrer un commentaire