Skip to content

Commit 3451d1e

Browse files
authored
Lock access to facade_factory in data_watchdog to avoid accessing destructed object (#5844)
* Wrap access to facade_factory in a shared lock so it doesn't get changed partway through access which leads to a crash.
1 parent 4799b46 commit 3451d1e

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- CHANGED: default car weight was reduced to 2000 kg. [#5371](https://github.com/Project-OSRM/osrm-backend/pull/5371)
1818
- CHANGED: default car height was reduced to 2 meters. [#5389](https://github.com/Project-OSRM/osrm-backend/pull/5389)
1919
- FIXED: treat `bicycle=use_sidepath` as no access on the tagged way. [#5622](https://github.com/Project-OSRM/osrm-backend/pull/5622)
20+
- FIXED: fix occasional segfault when swapping data with osrm-datastore and using `exclude=` [#5844](https://github.com/Project-OSRM/osrm-backend/pull/5844)
2021
- Misc:
2122
- CHANGED: Reduce memory usage for raster source handling. [#5572](https://github.com/Project-OSRM/osrm-backend/pull/5572)
2223
- CHANGED: Add cmake option `ENABLE_DEBUG_LOGGING` to control whether output debug logging. [#3427](https://github.com/Project-OSRM/osrm-backend/issues/3427)

include/engine/data_watchdog.hpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
5656
static_region = *static_shared_region;
5757
updatable_region = *updatable_shared_region;
5858

59-
facade_factory =
60-
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
61-
std::make_shared<datafacade::SharedMemoryAllocator>(
62-
std::vector<storage::SharedRegionRegister::ShmKey>{
63-
static_region.shm_key, updatable_region.shm_key}));
59+
{
60+
boost::unique_lock<boost::shared_mutex> swap_lock(factory_mutex);
61+
facade_factory =
62+
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
63+
std::make_shared<datafacade::SharedMemoryAllocator>(
64+
std::vector<storage::SharedRegionRegister::ShmKey>{
65+
static_region.shm_key, updatable_region.shm_key}));
66+
}
6467
}
6568

6669
watcher = std::thread(&DataWatchdogImpl::Run, this);
@@ -75,10 +78,14 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
7578

7679
std::shared_ptr<const Facade> Get(const api::BaseParameters &params) const
7780
{
81+
// make sure facade_factory stays stable while we call Get()
82+
boost::shared_lock<boost::shared_mutex> swap_lock(factory_mutex);
7883
return facade_factory.Get(params);
7984
}
8085
std::shared_ptr<const Facade> Get(const api::TileParameters &params) const
8186
{
87+
// make sure facade_factory stays stable while we call Get()
88+
boost::shared_lock<boost::shared_mutex> swap_lock(factory_mutex);
8289
return facade_factory.Get(params);
8390
}
8491

@@ -111,16 +118,20 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
111118
<< (int)updatable_region.shm_key << " with timestamps "
112119
<< static_region.timestamp << " and " << updatable_region.timestamp;
113120

114-
facade_factory =
115-
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
116-
std::make_shared<datafacade::SharedMemoryAllocator>(
117-
std::vector<storage::SharedRegionRegister::ShmKey>{
118-
static_region.shm_key, updatable_region.shm_key}));
121+
{
122+
boost::unique_lock<boost::shared_mutex> swap_lock(factory_mutex);
123+
facade_factory =
124+
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
125+
std::make_shared<datafacade::SharedMemoryAllocator>(
126+
std::vector<storage::SharedRegionRegister::ShmKey>{
127+
static_region.shm_key, updatable_region.shm_key}));
128+
}
119129
}
120130

121131
util::Log() << "DataWatchdog thread stopped";
122132
}
123133

134+
mutable boost::shared_mutex factory_mutex;
124135
const std::string dataset_name;
125136
storage::SharedMonitor<storage::SharedRegionRegister> barrier;
126137
std::thread watcher;

0 commit comments

Comments
 (0)