mapnik/test/standalone/datasource_registration_test.cpp
Matt Amos 3d7b84a598 Fix deadlock in recursive datasource registration.
The datasource cache was taking an exclusive lock on the simple
mutex used to protect the singleton's data pointer. This works
okay when everyone always calls it non-recursively, but when the
recursive flag is true then it will always deadlock when called
on any directory with subdirectories.

Additionally, many methods which accessed private data members of
the cache were not protected by any locks.

Since the call pattern of registering datasources is strictly
tree-shaped then it's a good candidate for a recursive mutex. This
has a slightly higher overhead than a simple mutex, so rather than
change the singleton's mutex to be recursive, I've added a new
instance mutex to the datasource cache.

Also, added a very basic test which reproduces the problem and
shows that it's fixed with this patch.
2015-08-23 20:25:35 +01:00

46 lines
1.3 KiB
C++

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <mapnik/datasource_cache.hpp>
#include <mapnik/debug.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
TEST_CASE("datasource_cache") {
SECTION("registration") {
try
{
mapnik::logger logger;
mapnik::logger::severity_type original_severity = logger.get_severity();
bool success = false;
auto &cache = mapnik::datasource_cache::instance();
// registering a directory without any plugins should return false
success = cache.register_datasources("test/data/vrt");
CHECK(success == false);
// registering a directory for the first time should return true
success = cache.register_datasources("plugins/input");
REQUIRE(success == true);
// registering the same directory again should now return false
success = cache.register_datasources("plugins/input");
CHECK(success == false);
// registering the same directory again, but recursively should
// still return false - even though there are subdirectories, they
// do not contain any more plugins.
success = cache.register_datasources("plugins/input", true);
CHECK(success == false);
}
catch (std::exception const & ex)
{
std::clog << ex.what() << "\n";
REQUIRE(false);
}
}
}