diff --git a/fairmq/shmem/Manager.h b/fairmq/shmem/Manager.h index 7d06fe15..8ed7d2f1 100644 --- a/fairmq/shmem/Manager.h +++ b/fairmq/shmem/Manager.h @@ -434,21 +434,42 @@ class Manager void RemoveRegion(uint16_t id) { - try { - boost::interprocess::scoped_lock shmLock(*fShmMtx); + UnmanagedRegion* region = nullptr; + { std::lock_guard 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 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 removedRegion; + { + std::lock_guard 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 GetRegionInfo() diff --git a/test/region/_region.cxx b/test/region/_region.cxx index 98b3bbfe..4d3fdfcf 100644 --- a/test/region/_region.cxx +++ b/test/region/_region.cxx @@ -21,6 +21,7 @@ #include +#include #include #include #include // 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("session", to_string(session)); + config.SetProperty("shm-segment-size", 100000000); + config.SetProperty("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 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);