Skip to content

Commit 658a727

Browse files
committed
Fix tedge-agent panic on entity registration
Mixing entity registrations over MQTT and HTTP: ``` tedge mqtt pub te/device/a/service/c '{"@parent":"device/a//","@type":"service"}' tedge mqtt pub te/device/ac// '{"@parent":"device/a//","@type":"child-device"}' curl http://localhost:8000/tedge/entity-store/v1/entities -v -H "Content-Type: application/json" -d '{"@topic-id": "device/a/service/c", "@type": "service", "@parent": "device/a//"}' curl http://localhost:8000/tedge/entity-store/v1/entities -v -H "Content-Type: application/json" -d '{"@topic-id": "device/a//", "@type": "child-device", "@parent": "device/main//"}' ``` was crashing the agent: ``` thread 'tokio-runtime-worker' panicked at crates/core/tedge_api/src/store/pending_entity_store.rs:96:73: called `Option::unwrap()` on a `None` value ``` Signed-off-by: Didier Wenzek <[email protected]>
1 parent 62a8f4d commit 658a727

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

crates/core/tedge_api/src/store/pending_entity_store.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,21 @@ impl PendingEntityStore {
8585
}
8686
}
8787

88-
/// Returns all the children and their children recursively for a given parent
88+
/// Recursively removes from the pending entity cache the children of a freshly registered device.
89+
///
90+
/// Returns the list of devices which registration is no more pending.
8991
pub fn take_cached_child_entities_data(
9092
&mut self,
9193
entity_tid: &EntityTopicId,
9294
) -> Vec<RegisteredEntityData> {
9395
let mut children = vec![];
9496
if let Some(direct_children) = self.orphans.remove(entity_tid) {
9597
for child in direct_children {
96-
let pending_entity_cache = self.entities.remove(&child).unwrap();
97-
let pending_entity_data = self.registered_data_from_cache(pending_entity_cache);
98-
children.push(pending_entity_data);
99-
children.append(&mut self.take_cached_child_entities_data(&child));
98+
if let Some(pending_entity_cache) = self.entities.remove(&child) {
99+
let pending_entity_data = self.registered_data_from_cache(pending_entity_cache);
100+
children.push(pending_entity_data);
101+
children.append(&mut self.take_cached_child_entities_data(&child));
102+
}
100103
}
101104
}
102105
children

0 commit comments

Comments
 (0)