datastax/cpp-driver — pull request #20 by mhfrantz (2014-03-12 16:29:22+00:00)
Title: cql_builder::build fail race

This PR contains two commits.

The first commit is a unit test that reproduces a race. One could argue whether this belongs in the unit test suite, since it actually attempts network communication. It uses a port that is expected to contain no server. Perhaps it should be off by default, or moved to integration tests, although it does not require the CCM bridge stuff.

With the unit test, I was able to generate a few different fatal errors. Running the test under valgrind revealed that there were accesses to deleted memory.

The second commit fixes the bug by using Boost enable_shared_from_this. The callbacks registered with the io_service are thus protected from the race because they contain a shared_ptr rather than a raw pointer.

The change goes further by using the same technique with all of the io_service callbacks that I could find. In fact, I think that some of the complexity of _is_disposed and the cql_trashcan could potentially be eliminated. As far as I can tell, those are mechanisms to address races between the io_service thread and sessions that are closed on the main thread.