Skip to content

Commit 11142db

Browse files
committed
Fixing threading and sync issues
1 parent 5a3186f commit 11142db

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

clients/roscpp/src/libros/service_server_link.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void ServiceServerLink::cancelCall(const CallInfoPtr& info)
7474
{
7575
boost::mutex::scoped_lock lock(local->finished_mutex_);
7676
local->finished_ = true;
77+
local->resp_ = nullptr; // The response pointer is no longer valid
7778
local->finished_condition_.notify_all();
7879
}
7980

@@ -184,6 +185,8 @@ void ServiceServerLink::onRequestWritten(const ConnectionPtr& conn)
184185
//ros::WallDuration(0.1).sleep();
185186

186187
boost::mutex::scoped_lock lock(call_queue_mutex_);
188+
auto current_call = current_call_;
189+
lock.unlock();
187190

188191
connection_->read(5, boost::bind(&ServiceServerLink::onResponseOkAndLength, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
189192

@@ -193,7 +196,7 @@ void ServiceServerLink::onRequestWritten(const ConnectionPtr& conn)
193196
boost::bind(
194197
&ServiceServerLink::waitForTimeout,
195198
this,
196-
current_call_,
199+
current_call,
197200
timeout_sec_));
198201
}
199202
}
@@ -269,7 +272,9 @@ void ServiceServerLink::onResponse(const ConnectionPtr& conn, const boost::share
269272
{
270273
boost::mutex::scoped_lock queue_lock(call_queue_mutex_);
271274

272-
if (current_call_->success_)
275+
// If this message was cancelled, the resp_ object will no longer be pointing at a valid response object
276+
// (we reset it to null)
277+
if (current_call_ && current_call_->success_ && current_call_->resp_)
273278
{
274279
*current_call_->resp_ = SerializedMessage(buffer, size);
275280
}

0 commit comments

Comments
 (0)