fix(shmem): do not hold region locks while region acks stop

- RemoveRegion() held the session-wide shmem mutex and the local region
  mutex while it joined the ack threads of the region
- the ack receiver calls the user callback for each outstanding ack
  during the linger period; a callback that calls into the transport
  (e.g. GetRegionInfo(), or a message destruction that opens a region)
  waits for one of these mutexes, and the destruction never ends
- the shmem mutex is shared by all processes of the session, so the
  other processes hang also, and new processes hang at startup
- stop the acks without a lock, then lock each mutex separately and
  only for the access to the shared data
- 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 8900606bae
commit 139111a0c5
2 changed files with 81 additions and 11 deletions

View File

@@ -434,21 +434,42 @@ class Manager
void RemoveRegion(uint16_t id)
{
try {
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> shmLock(*fShmMtx);
UnmanagedRegion* region = nullptr;
{
std::lock_guard<std::mutex> lock(fLocalRegionsMtx);
fRegions.at(id)->StopAcks();
{
if (fRegions.at(id)->RemoveOnDestruction()) {
fShmRegions->at(id).fDestroyed = true;
(fEventCounter->fCount)++;
}
fRegions.erase(id);
auto it = fRegions.find(id);
if (it == fRegions.end()) {
LOG(debug) << "RemoveRegion() could not locate region with id '" << id << "'";
return;
}
region = it->second.get();
}
// Do not hold a lock here: the ack receiver calls the user callbacks for the outstanding
// acks, and a callback can call into the transport.
region->StopAcks();
if (region->RemoveOnDestruction()) {
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> shmLock(*fShmMtx);
try {
fShmRegions->at(id).fDestroyed = true;
(fEventCounter->fCount)++;
} catch (std::out_of_range& oor) {
LOG(debug) << "RemoveRegion() could not locate region info with id '" << id << "'";
}
}
std::unique_ptr<UnmanagedRegion> removedRegion;
{
std::lock_guard<std::mutex> lock(fLocalRegionsMtx);
auto it = fRegions.find(id);
if (it != fRegions.end()) {
removedRegion = std::move(it->second);
fRegions.erase(it);
}
} catch (std::out_of_range& oor) {
LOG(debug) << "RemoveRegion() could not locate region with id '" << id << "'";
}
fRegionsGen += 1; // signal TL cache invalidation
// removedRegion is destroyed here, without a lock
}
std::vector<fair::mq::RegionInfo> GetRegionInfo()