Skip to content

Commit

Permalink
[python/viewer] Fix meshcat zmq socket not properly closed.
Browse files Browse the repository at this point in the history
  • Loading branch information
duburcqa committed Feb 11, 2024
1 parent 52834a7 commit 6ab13ea
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions python/jiminy_py/src/jiminy_py/viewer/meshcat/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def __init__(self,
with open(os.devnull, 'w') as stdout, redirect_stdout(stdout):
with open(os.devnull, 'w') as stderr, redirect_stderr(stderr):
self.gui = meshcat.Visualizer(zmq_url)
self.__zmq_socket = self.gui.window.zmq_socket
self._zmq_socket = self.gui.window.zmq_socket

# Create a backend recorder. It is not fully initialized to reduce
# overhead when not used, which is way more usual than the contrary.
Expand Down Expand Up @@ -345,10 +345,10 @@ def __del__(self) -> None:
def close(self) -> None:
""" TODO: Write documentation.
"""
if hasattr(self, "__zmq_socket"):
if not self.__zmq_socket.closed:
self.__zmq_socket.send(b"stop")
self.__zmq_socket.close()
if hasattr(self, "_zmq_socket"):
if not self._zmq_socket.closed:
self._zmq_socket.send(b"stop")
self._zmq_socket.close()
if hasattr(self, "comm_manager") and self.comm_manager is not None:
self.comm_manager.close()
if hasattr(self, "recorder") is not None:
Expand All @@ -363,15 +363,15 @@ def wait(self, require_client: bool = False) -> str:
# perform a single `do_one_iteration`, just in case there is
# already comm waiting in the queue to be registered, but it should
# not be necessary.
self.__zmq_socket.send(b"wait")
self._zmq_socket.send(b"wait")
if self.comm_manager is None:
self.__zmq_socket.recv()
self._zmq_socket.recv()
else:
while True:
try:
# Try first, just in case there is already a comm for
# websocket available.
self.__zmq_socket.recv(flags=zmq.NOBLOCK)
self._zmq_socket.recv(flags=zmq.NOBLOCK)
break
except zmq.error.ZMQError:
# No websocket nor comm connection available at this
Expand All @@ -395,21 +395,21 @@ def wait(self, require_client: bool = False) -> str:
# of comms currently registered. It is necessary to check for a reply
# of the server periodically, and the number of responses corresponds
# to the actual number of comms.
self.__zmq_socket.send(b"ready")
self._zmq_socket.send(b"ready")
if self.comm_manager is not None:
while True:
process_kernel_comm()
try:
msg = self.__zmq_socket.recv(flags=zmq.NOBLOCK)
msg = self._zmq_socket.recv(flags=zmq.NOBLOCK)
return msg.decode("utf-8")
except zmq.error.ZMQError:
pass
return self.__zmq_socket.recv().decode("utf-8")
return self._zmq_socket.recv().decode("utf-8")

def set_legend_item(self, uniq_id: str, color: str, text: str) -> None:
""" TODO: Write documentation.
"""
self.__zmq_socket.send_multipart([
self._zmq_socket.send_multipart([
b"set_property", # Frontend command. Used by Python zmq server
b"", # Tree path. Empty path means root
umsgpack.packb({ # Backend command. Used by javascript
Expand All @@ -419,12 +419,12 @@ def set_legend_item(self, uniq_id: str, color: str, text: str) -> None:
"color": color # "rgba(0, 0, 0, 0)" and "black" supported
})
])
self.__zmq_socket.recv() # Receive acknowledgement
self._zmq_socket.recv() # Receive acknowledgement

def remove_legend_item(self, uniq_id: str) -> None:
""" TODO: Write documentation.
"""
self.__zmq_socket.send_multipart([
self._zmq_socket.send_multipart([
b"set_property",
b"",
umsgpack.packb({
Expand All @@ -433,7 +433,7 @@ def remove_legend_item(self, uniq_id: str) -> None:
"text": "" # Empty message means delete the item, if any
})
])
self.__zmq_socket.recv()
self._zmq_socket.recv()

def set_watermark(self,
img_fullpath: str,
Expand Down Expand Up @@ -465,7 +465,7 @@ def set_watermark(self,
img_data = f"data:image/{img_format};base64,{img_raw}"

# Send ZMQ request to acknowledge reply
self.__zmq_socket.send_multipart([
self._zmq_socket.send_multipart([
b"set_property",
b"",
umsgpack.packb({
Expand All @@ -475,20 +475,20 @@ def set_watermark(self,
"height": height
})
])
self.__zmq_socket.recv()
self._zmq_socket.recv()

def remove_watermark(self) -> None:
""" TODO: Write documentation.
"""
self.__zmq_socket.send_multipart([
self._zmq_socket.send_multipart([
b"set_property",
b"",
umsgpack.packb({
"type": "watermark",
"data": "" # Empty string means delete the watermark, if any
})
])
self.__zmq_socket.recv()
self._zmq_socket.recv()

def start_recording(self, fps: float, width: int, height: int) -> None:
""" TODO: Write documentation.
Expand Down

0 comments on commit 6ab13ea

Please sign in to comment.