Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Melodic backport of #595] [rosserial_python] Stop old thread before recreating new SerialClient #596

Open
wants to merge 1 commit into
base: melodic-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rosserial_python/nodes/serial_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
except:
rospy.logwarn("Unexpected Error.%s", sys.exc_info()[0])
client.port.close()
client.stopWriteThread()
sleep(1.0)
continue

9 changes: 8 additions & 1 deletion rosserial_python/src/rosserial_python/SerialClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def __init__(self, port=None, baud=57600, timeout=5.0, fix_pyserial_for_test=Fal

self.write_lock = threading.RLock()
self.write_queue = Queue()
self.write_alive = False
self.write_thread = None

self.lastsync = rospy.Time(0)
Expand Down Expand Up @@ -448,6 +449,7 @@ def run(self):

# Launch write thread.
if self.write_thread is None:
self.write_alive = True
self.write_thread = threading.Thread(target=self.processWriteQueue)
self.write_thread.daemon = True
self.write_thread.start()
Expand Down Expand Up @@ -556,6 +558,11 @@ def run(self):
self.port.flushOutput()
self.requestTopics()

def stopWriteThread(self):
# stop self.write_thread
self.write_alive = False
self.write_thread.join()

def setPublishSize(self, bytes):
if self.buffer_out < 0:
self.buffer_out = bytes
Expand Down Expand Up @@ -771,7 +778,7 @@ def processWriteQueue(self):
"""
Main loop for the thread that processes outgoing data to write to the serial port.
"""
while not rospy.is_shutdown():
while not rospy.is_shutdown() and self.write_alive:
if self.write_queue.empty():
time.sleep(0.01)
else:
Expand Down