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:
Claude
2026-11-02 16:19:52 +00:00
committed by Dennis Klein
parent 139111a0c5
commit f4e265a562
2 changed files with 53 additions and 3 deletions

View File

@@ -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);