MongoCXX connection pool usage results in segmentation fault

173 views
Skip to first unread message

Werner Cloete

unread,
Sep 12, 2019, 5:26:10 PM9/12/19
to mongodb-user
Good day, everyone

I'm working on a (wrapping) class that will allow me to use the new transaction handling capabilities in MongoDB and MongoCXX.

My class maintains a list of connection pools, to various MongoDB instances, and I access the pools via an alias name.

Most of the code has been removed to make it easier to read and get to the root of the problem.

The connection pool is contained in my "mongodb_pool" class:

class mongodb_pool
{
  public:
    ...
    void setup(unique_ptr<mongocxx::pool> pool);
    mongocxx::pool::entry acquire();
  private:
    unique_ptr<mongocxx::pool> _pool = nullptr;
};

void
mongodb_pool::setup(unique_ptr<mongocxx::pool> pool)
{
  _pool = move(pool);
}

mongocxx::pool::entry mongodb_pool::acquire()
{
  return _pool->acquire();
}

This "mongodb_pool" class is used in my main "mongodb" class, through a map:

class mongodb
{
  public:
    ...
    bool has_session(string name);
    void create_session(string name, string connection_string);
    mongocxx::client_session session(string name);
    ...
    vector<string> database_names(mongocxx::client_session &session);
    mongocxx::database database(mongocxx::client_session &session, string database_name);
    ...
  private:
    map<string, pool::mongodb_pool> _pools;
    ...
};

bool mongodb::has_session(string name)
{
  return (_pools.find(name) != _pools.end());
}

void mongodb::create_session(string name, string connection_string)
{
  if (!has_session(name))
  {
    pool::mongodb_pool temp;
    temp
.setup(bsoncxx::stdx::make_unique<mongocxx::pool>(move(mongocxx::uri{connection_string})));
    _pools[name] = move(temp);
  }
}

mongocxx
::client_session mongodb::session(string name)
{
  auto client = _pools[name].acquire();
  return (*client).start_session();
}

vector
<string> mongodb::database_names(mongocxx::client_session &session)
{
  vector<string> result;
  mongocxx::cursor cursor = session.client().list_databases(session);
 
  for
(const bsoncxx::document::view doc : cursor)
  {
    result.push_back(doc["name"].get_utf8().value.to_string());
  }
 
 
sort(result.begin(), result.end());
 
  return
result;
}

mongocxx
::database mongodb::database(mongocxx::client_session &session, string database_name)
{
  return session.client().database(database_name);
}

In my test application I do the following:

if (!mongodb.has_session("test"))
{
 
string session_uri("mongodb://127.0.0.1:27017");
  mongodb
.create_session("test", session_uri);
}

mongocxx
::client_session session = mongodb.session("test");

vector
<string> db_names = mongodb.database_names(session);

for (unsigned int i = 0; i < db_names.size(); i++)
{
  cout
<< db_names[i] << endl;
}

I get a segmentation fault when I call "vector<string> db_names = mongodb.database_names(session);" in my test code.

I traced it to the "mongocxx::cursor cursor = session.client().list_databases(session);" line in the "database_names" method.

I would really appreciate help with this problem.

Werner

Andrew Morrow

unread,
Sep 16, 2019, 12:53:04 PM9/16/19
to mongod...@googlegroups.com

Hi -

The lifetime rules in the C++ driver can be tricky. Have you tried running your program under valgrind, or building it (and the drivers) with ASAN? It is usually the best thing to start with for these sorts of crashes. Often, it will show you exactly where the issue is.

Thanks,
Andrew


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/54200a85-ca37-4336-abb3-4a0de57f0322%40googlegroups.com.

Werner Cloete

unread,
Sep 16, 2019, 6:49:56 PM9/16/19
to mongod...@googlegroups.com
Good evening, Andrew

Thank you for your response and suggestion.

I've installed "valgrind" and ran it with my application.  The output did not make a lot of sense to me so I've attached the log file as it may make more sense to you...or, at least, I hope it does.  ;-)

Yours sincerely

Werner



valgrind.log

Andrew Morrow

unread,
Sep 17, 2019, 11:48:43 AM9/17/19
to mongod...@googlegroups.com

Hi -

That valgrind output does make a lot of sense to me. It states unambiguously what is going wrong, once you spend a little time reading. This guide should give you some help getting started: http://valgrind.org/docs/manual/quick-start.html#quick-start.interpret

Thanks,
Andrew
 

Werner Cloete

unread,
Sep 19, 2019, 2:47:43 PM9/19/19
to mongod...@googlegroups.com
Good evening, Andrew

I'll have to go through the valgrind documentation and logs later.  I hope that it will help.

I have to, for the moment, abandon the pool usage idea and just use a single instance of client.

I'm now focusing my attention to the rest of the code, to ensure that everything works as expected, and I've already picked up a problem with "update_one" and I've created a separate post for the problem.

Werner

Andrew Morrow

unread,
Sep 19, 2019, 3:55:38 PM9/19/19
to mongod...@googlegroups.com
On Thu, Sep 19, 2019 at 2:47 PM Werner Cloete <werner...@gmail.com> wrote:
Good evening, Andrew

I'll have to go through the valgrind documentation and logs later.  I hope that it will help.

It will definitely help, and knowing how to use valgrind will pay big dividends. I strongly recommend running all of your tests under it in your CI system, if possible.
 

I have to, for the moment, abandon the pool usage idea and just use a single instance of client.

I'm now focusing my attention to the rest of the code, to ensure that everything works as expected, and I've already picked up a problem with "update_one" and I've created a separate post for the problem.

OK, I'll take a look.


Reply all
Reply to author
Forward
0 new messages