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()

View File

@@ -21,6 +21,7 @@
#include <gtest/gtest.h>
#include <atomic>
#include <cstdint>
#include <map>
#include <memory> // make_unique
@@ -335,6 +336,44 @@ void RegionCallbacks(const string& transport, const string& _address)
LOG(info) << "2 done.";
}
void RegionCallbackCallsTransportDuringDestruction(const string& transport, const string& _address)
{
// The ack callback of a region can run while the region is destroyed (outstanding acks are
// delivered during the linger period). A callback that calls into the transport must not
// deadlock the destruction.
size_t session(tools::UuidHash());
std::string address(tools::ToString(_address, "_", transport));
ProgOptions config;
config.SetProperty<string>("session", to_string(session));
config.SetProperty<size_t>("shm-segment-size", 100000000);
config.SetProperty<bool>("shm-monitor", true);
auto factory = TransportFactory::CreateTransportFactory(transport, tools::Uuid(), &config);
Channel push("Push", "push", factory);
push.Bind(address);
Channel pull("Pull", "pull", factory);
pull.Connect(address);
atomic<int> numCallbacks(0);
auto region = factory->CreateUnmanagedRegion(1000000, [&](void*, size_t, void*) {
factory->GetRegionInfo();
++numCallbacks;
});
{
MessagePtr msgOut(push.NewMessage(region, region->GetData(), 100, nullptr));
ASSERT_EQ(push.Send(msgOut), 100);
MessagePtr msgIn(pull.NewMessage());
ASSERT_EQ(pull.Receive(msgIn), 100);
} // the shmem transport sends the ack for this block in a batch, which is still pending here
region.reset(); // delivers the pending ack, the callback runs
EXPECT_EQ(numCallbacks, 1);
}
TEST(RegionsSizeMismatch, shmem)
{
RegionsSizeMismatch();
@@ -370,6 +409,16 @@ TEST(Callbacks, shmem)
RegionCallbacks("shmem", "ipc://test_region_callbacks");
}
TEST(CallbackDuringDestruction, zeromq)
{
RegionCallbackCallsTransportDuringDestruction("zeromq", "ipc://test_region_callback_during_destruction");
}
TEST(CallbackDuringDestruction, shmem)
{
RegionCallbackCallsTransportDuringDestruction("shmem", "ipc://test_region_callback_during_destruction");
}
TEST(EventSubscriptionsExternalRegion, shmem)
{
RegionEventSubscriptions("shmem", true);