Skip to content

Commit 3480213

Browse files
committed
PR feedback
1 parent 11142db commit 3480213

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

clients/roscpp/include/ros/service_server_link.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ class ROSCPP_DECL ServiceServerLink : public boost::enable_shared_from_this<Serv
136136
void onResponseOkAndLength(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success);
137137
void onResponse(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success);
138138

139-
/**
140-
* \brief Used to time out service calls
141-
* \param info - CallInfo that includes our finished mutex
142-
* \param seconds - The number of seconds to wait for the timeout
143-
*/
144-
void waitForTimeout(CallInfoPtr info, double seconds);
145-
146139
ConnectionPtr connection_;
147140
std::string service_name_;
148141
bool persistent_;

clients/roscpp/src/libros/service_server_link.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ 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
77+
// If onResponse fires after we've returned from call() (due to this being cancelled), the pointer to resp_ will no longer be valid
78+
local->resp_ = nullptr;
7879
local->finished_condition_.notify_all();
7980
}
8081

@@ -189,34 +190,6 @@ void ServiceServerLink::onRequestWritten(const ConnectionPtr& conn)
189190
lock.unlock();
190191

191192
connection_->read(5, boost::bind(&ServiceServerLink::onResponseOkAndLength, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
192-
193-
if (timeout_sec_ >= 0.0)
194-
{
195-
boost::thread timeout_checker(
196-
boost::bind(
197-
&ServiceServerLink::waitForTimeout,
198-
this,
199-
current_call,
200-
timeout_sec_));
201-
}
202-
}
203-
204-
void ServiceServerLink::waitForTimeout(CallInfoPtr info, double seconds)
205-
{
206-
boost::mutex::scoped_lock finished_lock(info->finished_mutex_);
207-
if (info && info->finished_condition_.wait_for(finished_lock, boost::chrono::duration<double>(seconds)) == boost::cv_status::timeout)
208-
{
209-
bool finished = info->finished_;
210-
finished_lock.unlock();
211-
212-
// If we timeout, we need to cancel the call
213-
ROS_WARN_STREAM("Service call to " << service_name_ << " timed out after " << seconds << " seconds");
214-
215-
if (info && !finished)
216-
{
217-
cancelCall(info);
218-
}
219-
}
220193
}
221194

222195
void ServiceServerLink::onResponseOkAndLength(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success)
@@ -400,23 +373,50 @@ bool ServiceServerLink::call(const SerializedMessage& req, SerializedMessage& re
400373
processNextCall();
401374
}
402375

376+
auto status = boost::cv_status::no_timeout;
377+
403378
{
404379
boost::mutex::scoped_lock lock(info->finished_mutex_);
405380

406381
while (!info->finished_)
407382
{
408-
info->finished_condition_.wait(lock);
383+
if (timeout_sec_ >= 0.0)
384+
{
385+
status = info->finished_condition_.wait_for(lock, boost::chrono::duration<double>(timeout_sec_));
386+
387+
if (status == boost::cv_status::timeout)
388+
{
389+
bool finished = info->finished_;
390+
lock.unlock();
391+
392+
if (!finished)
393+
{
394+
cancelCall(info);
395+
}
396+
397+
ROS_WARN_STREAM("Service call to " << service_name_ << " timed out after " << timeout_sec_ << " seconds");
398+
}
399+
}
400+
else
401+
{
402+
info->finished_condition_.wait(lock);
403+
}
409404
}
410405
}
411406

412407
info->call_finished_ = true;
413408

409+
if (status == boost::cv_status::timeout)
410+
{
411+
ROS_WARN_STREAM("Service call to " << service_name_ << " timed out after " << timeout_sec_ << " seconds");
412+
}
413+
414414
if (info->exception_string_.length() > 0)
415415
{
416416
ROS_ERROR("Service call failed: service [%s] responded with an error: %s", service_name_.c_str(), info->exception_string_.c_str());
417417
}
418418

419-
return info->success_;
419+
return info->success_ && status != boost::cv_status::timeout;
420420
}
421421

422422
bool ServiceServerLink::isValid() const

0 commit comments

Comments
 (0)