Skip to content

Commit 7c4a0ea

Browse files
committed
Refs #21188: Fix data race between requests_status_ init and update
Signed-off-by: eduponz <eduardoponz@eprosima.com>
1 parent 7e7ed84 commit 7c4a0ea

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

examples/cpp/request_reply/ClientApp.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,19 @@ bool ClientApp::send_requests()
468468
bool ClientApp::send_request(
469469
const CalculatorRequestType& request)
470470
{
471+
// Taking the mutex here to avoid taking a reply on the on_data_available callback
472+
// coming from a very fast server who replied before the request_status_ entry was set
473+
std::lock_guard<std::mutex> lock(mtx_);
474+
471475
rtps::WriteParams wparams;
472476
ReturnCode_t ret = request_writer_->write(&request, wparams);
473477

478+
requests_status_[wparams.sample_identity()] = false;
479+
474480
request_reply_info("ClientApp",
475481
"Request sent with ID '" << wparams.sample_identity().sequence_number() <<
476482
"': '" << TypeConverter::to_string(request) << "'");
477483

478-
std::lock_guard<std::mutex> lock(mtx_);
479-
requests_status_[wparams.sample_identity()] = false;
480-
481484
return (RETCODE_OK == ret);
482485
}
483486

0 commit comments

Comments
 (0)