From f4e265a562e392ab2706d803b20b7f41e8c16450 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 16:19:52 +0000 Subject: [PATCH] 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 --- fairmq/shmem/Manager.h | 6 ++--- test/region/_region.cxx | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/fairmq/shmem/Manager.h b/fairmq/shmem/Manager.h index 8ed7d2f1..15613364 100644 --- a/fairmq/shmem/Manager.h +++ b/fairmq/shmem/Manager.h @@ -304,6 +304,8 @@ class Manager std::pair result; { + // lock order: fLocalRegionsMtx before fShmMtx (see GetRegion()) + std::lock_guard lock(fLocalRegionsMtx); boost::interprocess::scoped_lock shmLock(*fShmMtx); if (!cfg.id.has_value()) { @@ -324,8 +326,6 @@ class Manager const uint16_t id = cfg.id.value(); - std::lock_guard 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 shmLock(*fShmMtx); RegionInfo regionInfo = fShmRegions->at(id); diff --git a/test/region/_region.cxx b/test/region/_region.cxx index 4d3fdfcf..97e8f3fd 100644 --- a/test/region/_region.cxx +++ b/test/region/_region.cxx @@ -26,6 +26,7 @@ #include #include // make_unique #include +#include #include // pair #include // 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("session", to_string(session)); + config.SetProperty("shm-segment-size", 100000000); + config.SetProperty("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 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);