Skip to content

Commit

Permalink
RST-9628 re-create service proxy after transport terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
abencz committed Apr 5, 2024
1 parent ac09fc4 commit db57545
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions aiorospy/src/aiorospy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ async def send(self, *args, **kwargs):
# Non-persistent ServiceProxy is not thread safe
# https://github.com/ros/ros_comm/blob/noetic-devel/clients/rospy/src/rospy/impl/tcpros_service.py#L534
async with self._lock:
return await log_during(self._loop.run_in_executor(None, service_call),
f"Trying to call service {self.name}...", log_period)
try:
return await log_during(self._loop.run_in_executor(None, service_call),
f"Trying to call service {self.name}...", log_period)
except rospy.exceptions.TransportTerminated:
# service proxy transport was disconnected and needs to be re-created
self._srv_proxy = rospy.ServiceProxy(self.name, self.service_class)
raise

async def ensure(self, *args, **kwargs):
""" Send a request to a ROS service, retrying if comms failure is detected. """
Expand All @@ -53,11 +58,16 @@ async def ensure(self, *args, **kwargs):
await self.wait_for_service(log_period)
try:
return await self.send(*args, **kwargs)
except (rospy.ServiceException, AttributeError, rospy.exceptions.ROSException,
rospy.exceptions.ROSInternalException) as e:
except (rospy.ServiceException, rospy.exceptions.ROSException, rospy.exceptions.ROSInternalException) as e:
logger.exception(f"Caught exception {e}, retrying service call after {sleep.next_sleep_time}s...")
await sleep()
continue
except AttributeError as e:
logger.exception(f"Caught AttributeError {e}, "
f"recreating service proxy and retrying service call after {sleep.next_sleep_time}s...")
await sleep()
self._srv_proxy = rospy.ServiceProxy(self.name, self.service_class)
continue


class AsyncService:
Expand Down

0 comments on commit db57545

Please sign in to comment.