mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 21:21:37 +00:00
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:
@@ -434,21 +434,42 @@ class Manager
|
|||||||
|
|
||||||
void RemoveRegion(uint16_t id)
|
void RemoveRegion(uint16_t id)
|
||||||
{
|
{
|
||||||
try {
|
UnmanagedRegion* region = nullptr;
|
||||||
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> shmLock(*fShmMtx);
|
{
|
||||||
std::lock_guard<std::mutex> lock(fLocalRegionsMtx);
|
std::lock_guard<std::mutex> lock(fLocalRegionsMtx);
|
||||||
fRegions.at(id)->StopAcks();
|
auto it = fRegions.find(id);
|
||||||
{
|
if (it == fRegions.end()) {
|
||||||
if (fRegions.at(id)->RemoveOnDestruction()) {
|
LOG(debug) << "RemoveRegion() could not locate region with id '" << id << "'";
|
||||||
fShmRegions->at(id).fDestroyed = true;
|
return;
|
||||||
(fEventCounter->fCount)++;
|
}
|
||||||
}
|
region = it->second.get();
|
||||||
fRegions.erase(id);
|
}
|
||||||
|
|
||||||
|
// 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
|
fRegionsGen += 1; // signal TL cache invalidation
|
||||||
|
// removedRegion is destroyed here, without a lock
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<fair::mq::RegionInfo> GetRegionInfo()
|
std::vector<fair::mq::RegionInfo> GetRegionInfo()
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory> // make_unique
|
#include <memory> // make_unique
|
||||||
@@ -335,6 +336,44 @@ void RegionCallbacks(const string& transport, const string& _address)
|
|||||||
LOG(info) << "2 done.";
|
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)
|
TEST(RegionsSizeMismatch, shmem)
|
||||||
{
|
{
|
||||||
RegionsSizeMismatch();
|
RegionsSizeMismatch();
|
||||||
@@ -370,6 +409,16 @@ TEST(Callbacks, shmem)
|
|||||||
RegionCallbacks("shmem", "ipc://test_region_callbacks");
|
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)
|
TEST(EventSubscriptionsExternalRegion, shmem)
|
||||||
{
|
{
|
||||||
RegionEventSubscriptions("shmem", true);
|
RegionEventSubscriptions("shmem", true);
|
||||||
|
|||||||
Reference in New Issue
Block a user