-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dataraces when creating DataWriters (#3051)
* Fix dataraces when creating DataWriters (#3015) * Refs #15905: Declare the PublishMode running flag as atomic Signed-off-by: Eduardo Ponz <[email protected]> * Refs #15905: Add RTPS regression test Signed-off-by: Eduardo Ponz <[email protected]> * Refs #15905: Add DomainParticipantImpl::create_instance_handle data race regression test Signed-off-by: Eduardo Ponz <[email protected]> * Refs #15905: Set DomainParticipantImpl::next_instance_id_ as atomic Signed-off-by: Eduardo Ponz <[email protected]> * Refs #15905: Apply suggestions Signed-off-by: Eduardo Ponz <[email protected]> Signed-off-by: Eduardo Ponz <[email protected]> (cherry picked from commit 4391864) Co-authored-by: Eduardo Ponz Segrelles <[email protected]> (cherry picked from commit 90777ec) # Conflicts: # test/blackbox/common/RTPSBlackboxTestsBasic.cpp * Fixed conflicts Signed-off-by: Miguel Company <[email protected]> * Fix build on RTPSBlackboxTestsBasic.cpp Signed-off-by: Miguel Company <[email protected]> Signed-off-by: Miguel Company <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Miguel Company <[email protected]>
- Loading branch information
1 parent
5b86ab1
commit acfd618
Showing
4 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <condition_variable> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <fastdds/dds/domain/DomainParticipant.hpp> | ||
#include <fastdds/dds/domain/DomainParticipantFactory.hpp> | ||
#include <fastdds/dds/domain/qos/DomainParticipantFactoryQos.hpp> | ||
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp> | ||
#include <fastdds/dds/publisher/Publisher.hpp> | ||
#include <fastdds/dds/publisher/qos/PublisherQos.hpp> | ||
#include <fastrtps/types/TypesBase.h> | ||
|
||
#include "BlackboxTests.hpp" | ||
|
||
namespace eprosima { | ||
namespace fastdds { | ||
namespace dds { | ||
|
||
using ReturnCode_t = eprosima::fastrtps::types::ReturnCode_t; | ||
|
||
/** | ||
* This test checks a race condition when calling DomainParticipantImpl::create_instance_handle() | ||
* from different threads simultaneously. This was resulting in a `free(): invalid pointer` crash | ||
* when deleting publishers created this way, as there was a clash in their respective instance | ||
* handles. Not only did the crash occur, but it was also reported by TSan. | ||
* | ||
* The test spawns 200 threads, each creating a publisher and then waiting on a command from the | ||
* main thread to delete them (so all of them at deleted at the same time). | ||
*/ | ||
TEST(DDSBasic, MultithreadedPublisherCreation) | ||
{ | ||
/* Get factory */ | ||
DomainParticipantFactory* factory = DomainParticipantFactory::get_instance(); | ||
ASSERT_NE(nullptr, factory); | ||
|
||
/* Create participant */ | ||
DomainParticipant* participant = factory->create_participant((uint32_t)GET_PID() % 230, PARTICIPANT_QOS_DEFAULT); | ||
ASSERT_NE(nullptr, participant); | ||
|
||
/* Test synchronization variables */ | ||
std::mutex finish_mtx; | ||
std::condition_variable finish_cv; | ||
bool should_finish = false; | ||
|
||
/* Function to create publishers, deleting them on command */ | ||
auto thread_run = | ||
[participant, &finish_mtx, &finish_cv, &should_finish]() | ||
{ | ||
/* Create publisher */ | ||
Publisher* publisher = participant->create_publisher(PUBLISHER_QOS_DEFAULT); | ||
ASSERT_NE(nullptr, publisher); | ||
|
||
{ | ||
/* Wait for test completion request */ | ||
std::unique_lock<std::mutex> lock(finish_mtx); | ||
finish_cv.wait(lock, [&should_finish]() | ||
{ | ||
return should_finish; | ||
}); | ||
} | ||
|
||
/* Delete publisher */ | ||
ASSERT_EQ(ReturnCode_t::RETCODE_OK, participant->delete_publisher(publisher)); | ||
}; | ||
|
||
{ | ||
/* Create threads */ | ||
std::vector<std::thread> threads; | ||
for (size_t i = 0; i < 200; i++) | ||
{ | ||
threads.push_back(std::thread(thread_run)); | ||
} | ||
|
||
/* Command threads to delete their publishers */ | ||
{ | ||
std::lock_guard<std::mutex> guard(finish_mtx); | ||
should_finish = true; | ||
finish_cv.notify_all(); | ||
} | ||
|
||
/* Wait for all threads to join */ | ||
for (std::thread& thr : threads) | ||
{ | ||
thr.join(); | ||
} | ||
} | ||
|
||
/* Clean up */ | ||
ASSERT_EQ(ReturnCode_t::RETCODE_OK, factory->delete_participant(participant)); | ||
} | ||
|
||
} // namespace dds | ||
} // namespace fastdds | ||
} // namespace eprosima |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters