Skip to content

Commit

Permalink
Catch execptions from callbacks since it might cause a crash when use…
Browse files Browse the repository at this point in the history
…d in gz-sim

Signed-off-by: Addisu Z. Taddese <[email protected]>
  • Loading branch information
azeey committed Sep 22, 2023
1 parent 184c3df commit 00d393d
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions python/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

from ._transport import Node as _Node
from ._transport import *
import sys
from typing import TypeVar, Callable
import traceback

# The "ProtoMsg" TypeVar represents an actual msg of a protobuf type.
# On the other hand, the "ProtoMsgType" TypeVar represents the protobuf type.
Expand Down Expand Up @@ -103,9 +105,16 @@ def subscribe(
"""

def cb_deserialize(proto_msg, msg_info):
deserialized_msg = msg_type()
deserialized_msg.ParseFromString(proto_msg)
callback(deserialized_msg)
# The callback might throw an exception and there's nothing else to
# catch since this will be in its own thread. This could cause
# crashes when used in gz-sim python systems, so we'll catch and
# print a traceback here.
try:
deserialized_msg = msg_type()
deserialized_msg.ParseFromString(proto_msg)
callback(deserialized_msg)
except Exception as e:
print(traceback.format_exc(), sys.stderr)

return self.subscribe_raw(
topic, cb_deserialize, msg_type.DESCRIPTOR.full_name, options
Expand Down

0 comments on commit 00d393d

Please sign in to comment.