gramaziokohler / roslibpy

Python ROS Bridge library
MIT License
273 stars 56 forks source link

Does not terminate or throw an exception when server is terminated #119

Closed jstiefel closed 10 months ago

jstiefel commented 11 months ago

Description Using roslibpy 1.5.0 on Ubuntu 20.04 with ROS noetic, I encountered an issue when the rosbridge server is terminated. Roslibpy neither tries to reconnect (as mentioned in the docs), nor does it terminate properly or with an exception.

It shows me a huge traceback, which is kind of cryptic and I'm not going to post it here.

To Reproduce This is a minimal example. After starting this, I terminate the rosbridge websocket.

import roslibpy

client = roslibpy.Ros(host="localhost", port=9090)
client.run()
if client.is_connected:
    print("Client is connected")

try:
    while True:
        pass
except KeyboardInterrupt:
    client.terminate()

Expected behavior I would at least except that roslibpy returns properly or throws an exception.

System (please complete the following information):

Thank you.

LKSeng commented 10 months ago

roslibpy does reconnect, it's just that it may take longer than you realise. In the docs, it is also mentioned that there is "an exponential back-off". This means that the longer the disconnection, the longer the reconnection retry will take place.

In the minimum working example (MWE) below, start the MWE script below when rosbridge_server is running. After seeing "Initial client connection success!", terminate rosbridge_server. You should see the MWE script below print out is connected? False. Start rosbridge_server immediately after this, and in about 5 seconds, you'll see the MWE print out is connected? True.

import roslibpy
import time

client = roslibpy.Ros(host="localhost", port=9090)
client.run()
if client.is_connected:
    print("Initial client connection success!")

try:
    while True:
        time.sleep(1)
        print("is connected? {}".format(client.is_connected,))
except KeyboardInterrupt:
    client.terminate()

The exponential back-off is a feature of twisted which roslibpy was built on-top of. You can try to limit this by setting the max delay before client instantiation:

roslibpy.comm.comm_autobahn.AutobahnRosBridgeClientFactory.set_max_delay(10) # in seconds, default is 3600s
client = roslibpy.Ros(host="localhost", port=9090)
client.run()

I am curious about the cryptic traceback mentioned. I did not see any traceback even when I was running the example in the topic post.

jstiefel commented 10 months ago

Hi @LKSeng,

You are right, no issues with your or my code from above right now. Not sure anymore what caused the issue back then.