Skip to content

Commit

Permalink
Add some guard to prevent crash on serialization errors
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Jun 22, 2023
1 parent 0a658f2 commit 6d186de
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from chip.ChipDeviceCtrl import CommissionableNode
from chip.clusters import Attribute, Objects as Clusters
from chip.clusters.Attribute import ValueDecodeFailure
from chip.clusters.ClusterObjects import ALL_CLUSTERS, Cluster
from chip.exceptions import ChipStackError

Expand All @@ -23,6 +24,7 @@
NodeNotResolving,
)
from ..common.helpers.api import api_command
from ..common.helpers.json import json_dumps
from ..common.helpers.util import (
create_attribute_path,
create_attribute_path_from_attribute,
Expand Down Expand Up @@ -429,6 +431,10 @@ def attribute_updated_callback(
) -> None:
assert self.server.loop is not None
new_value = transaction.GetAttribute(path)
# failsafe: ignore ValueDecodeErrors
# these are set by the SDK if parsing the value failed miserably
if isinstance(new_value, ValueDecodeFailure):
return
node_logger.debug("Attribute updated: %s - new value: %s", path, new_value)
attr_path = str(path.Path)
node.attributes[attr_path] = new_value
Expand Down Expand Up @@ -456,7 +462,6 @@ def event_callback(
assert self.server.loop is not None
node_logger.debug("Received node event: %s", data)
self.event_history.append(data)
# TODO: This callback does not seem to fire ever or my test devices do not have events
self.server.loop.call_soon_threadsafe(
self.server.signal_event, EventType.NODE_EVENT, data
)
Expand Down Expand Up @@ -627,6 +632,25 @@ def _parse_attributes_from_read_result(
attribute_path = create_attribute_path(
endpoint, cluster_cls.id, attr_cls.attribute_id
)
# failsafe: ignore ValueDecodeErrors
# these are set by the SDK if parsing the value failed miserably
if isinstance(attr_value, ValueDecodeFailure):
continue
# failsafe: make sure the attribute is serializable
# there is a chance we receive malformed data from the sdk
# due to all magic parsing to/from TLV.
# skip an attribute in that case to prevent serialization issues
# of the whole node.
try:
json_dumps(attr_value)
except TypeError as err:
LOGGER.warning(
"Unserializable data found - "
"skip attribute %s - error details: %s",
attribute_path,
err,
)
continue
result[attribute_path] = attr_value
return result

Expand Down

0 comments on commit 6d186de

Please sign in to comment.