[CONCPP-105] Connector crashes when attempting connections from multiple threads Created: 2022-07-25  Updated: 2022-07-26  Resolved: 2022-07-25

Status: Closed
Project: MariaDB Connector/C++
Component/s: General
Affects Version/s: 1.0.1, 1.1.1
Fix Version/s: 1.0.2, 1.1.2

Type: Bug Priority: Critical
Reporter: Lawrin Novitsky Assignee: Lawrin Novitsky
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

This is based of stackoverflow question
It looks like an issue in the connector. Some comments suggest there is the race condition

The standalone testcase(from the same source)

#include <mariadb/conncpp.hpp>
#include <iostream>
#include <thread>
#include <string>
 
sql::Driver* driver = sql::mariadb::get_driver_instance();
sql::SQLString connectionUrl = "jdbc:mariadb://localhost:3306/the_database_name";
sql::Properties properties = {{"user", "the_username"}, {"password", "the_password"}};
 
int main()
{
    // simulate 2 concurrent connections each making 500 requests
    std::vector<std::function<void()>> t1cbs;
    std::vector<std::function<void()>> t2cbs;
    for(int i = 0; i < 500; i++) // if does not fail for you at 500 up the number until it does
    {
        t1cbs.push_back([&]{
            std::unique_ptr<sql::Connection> conn(driver->connect(connectionUrl, properties));
            std::cout << "t1:" << conn->getHostname().c_str() << std::endl;
        });
 
        // comment out this block to keep the second thread from executing, and you will see 
        // that no errors occur
        t2cbs.push_back([&]{
            std::unique_ptr<sql::Connection> conn(driver->connect(connectionUrl, properties));
            std::cout << "t2:" << conn->getHostname().c_str() << std::endl;
        });
    }
 
    std::thread t1([&]{
        for(auto& cb : t1cbs)
            cb();
    });
 
    std::thread t2([&]{
        for(auto& cb : t2cbs)
            cb();
    });
 
    t1.join();
    t2.join();
 
    return 0;
}



 Comments   
Comment by Lawrin Novitsky [ 2022-07-25 ]

There was a race condition - one of classes used reference to static object.

Generated at Thu Feb 08 03:08:22 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.