mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
fix(shmem): lock the region mutexes in one order
- GetRegion() locks the local region mutex first and then the session-wide shmem mutex; CreateRegion() locked them in the opposite order - a thread that creates a region and a thread that opens a region of another process in the same session can deadlock (ABBA) - lock the local region mutex first also in CreateRegion() - add a regression test Refs: #334 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -304,6 +304,8 @@ class Manager
|
||||
std::pair<UnmanagedRegion*, uint16_t> result;
|
||||
|
||||
{
|
||||
// lock order: fLocalRegionsMtx before fShmMtx (see GetRegion())
|
||||
std::lock_guard<std::mutex> lock(fLocalRegionsMtx);
|
||||
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> shmLock(*fShmMtx);
|
||||
|
||||
if (!cfg.id.has_value()) {
|
||||
@@ -324,8 +326,6 @@ class Manager
|
||||
|
||||
const uint16_t id = cfg.id.value();
|
||||
|
||||
std::lock_guard<std::mutex> lock(fLocalRegionsMtx);
|
||||
|
||||
UnmanagedRegion* region = nullptr;
|
||||
|
||||
auto it = fRegions.find(id);
|
||||
@@ -407,7 +407,7 @@ class Manager
|
||||
|
||||
try {
|
||||
RegionConfig cfg;
|
||||
// get region info
|
||||
// get region info (lock order: fLocalRegionsMtx before fShmMtx)
|
||||
{
|
||||
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> shmLock(*fShmMtx);
|
||||
RegionInfo regionInfo = fShmRegions->at(id);
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <map>
|
||||
#include <memory> // make_unique
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility> // pair
|
||||
#include <vector> // pair
|
||||
|
||||
@@ -374,6 +375,50 @@ void RegionCallbackCallsTransportDuringDestruction(const string& transport, cons
|
||||
EXPECT_EQ(numCallbacks, 1);
|
||||
}
|
||||
|
||||
void RegionLockOrder(const string& _address)
|
||||
{
|
||||
// One thread creates and destroys regions. Another thread at the same time reads messages from
|
||||
// regions of a different peer in the same session, which opens these regions locally. Both
|
||||
// paths lock the same mutexes and must lock them in the same order.
|
||||
size_t session(tools::UuidHash());
|
||||
std::string address(tools::ToString(_address, "_shmem"));
|
||||
constexpr int numIterations = 10;
|
||||
|
||||
ProgOptions config;
|
||||
config.SetProperty<string>("session", to_string(session));
|
||||
config.SetProperty<size_t>("shm-segment-size", 100000000);
|
||||
config.SetProperty<bool>("shm-monitor", true);
|
||||
|
||||
auto factory1 = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
|
||||
auto factory2 = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
|
||||
|
||||
Channel push("Push", "push", factory2);
|
||||
push.Bind(address);
|
||||
Channel pull("Pull", "pull", factory1);
|
||||
pull.Connect(address);
|
||||
|
||||
vector<UnmanagedRegionPtr> peerRegions;
|
||||
for (int i = 0; i < numIterations; ++i) {
|
||||
peerRegions.emplace_back(factory2->CreateUnmanagedRegion(10000, [](void*, size_t, void*) {}));
|
||||
MessagePtr msg(push.NewMessage(peerRegions.back(), peerRegions.back()->GetData(), 100, nullptr));
|
||||
ASSERT_EQ(push.Send(msg), 100);
|
||||
}
|
||||
|
||||
thread creator([&] {
|
||||
for (int i = 0; i < numIterations; ++i) {
|
||||
auto region = factory1->CreateUnmanagedRegion(10000, [](void*, size_t, void*) {});
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < numIterations; ++i) {
|
||||
MessagePtr msg(pull.NewMessage());
|
||||
ASSERT_EQ(pull.Receive(msg), 100);
|
||||
ASSERT_NE(msg->GetData(), nullptr); // opens the peer region in factory1
|
||||
}
|
||||
|
||||
creator.join();
|
||||
}
|
||||
|
||||
TEST(RegionsSizeMismatch, shmem)
|
||||
{
|
||||
RegionsSizeMismatch();
|
||||
@@ -419,6 +464,11 @@ TEST(CallbackDuringDestruction, shmem)
|
||||
RegionCallbackCallsTransportDuringDestruction("shmem", "ipc://test_region_callback_during_destruction");
|
||||
}
|
||||
|
||||
TEST(LockOrder, shmem)
|
||||
{
|
||||
RegionLockOrder("ipc://test_region_lock_order");
|
||||
}
|
||||
|
||||
TEST(EventSubscriptionsExternalRegion, shmem)
|
||||
{
|
||||
RegionEventSubscriptions("shmem", true);
|
||||
|
||||
Reference in New Issue
Block a user