Skip to content

Commit

Permalink
mesh_node: fix size calculation for port failure events (#603) (#823)
Browse files Browse the repository at this point in the history
When a node sends a port failure event, it runs out of space and panics.
This is bad. Fix the size calculation to avoid this. Add a test.

Backport-of: #603
  • Loading branch information
jstarks authored Feb 7, 2025
1 parent 35f40d2 commit c36aa31
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions support/mesh/mesh_node/src/local_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ impl Port {
PendingEvents::send(&peer, seq, PortEvent::Message(message));
}
}

#[cfg(test)]
fn fail(self, err: NodeError) {
let mut pending_events = PendingEvents::new();
{
let mut state = self.inner.state.lock();
state.fail(&mut pending_events, err);
}
pending_events.process();
}
}

/// A [`Port`] that has a registered message handler.
Expand Down Expand Up @@ -1486,10 +1496,13 @@ impl<'a> OutgoingEvent<'a> {
len += size_of::<protocol::ChangePeerData>();
EventAndEncoder::Other(event)
}
PortEvent::FailPort(_) => {
len += size_of::<protocol::FailPortData>();
EventAndEncoder::Other(event)
}
event @ (PortEvent::ClosePort
| PortEvent::AcknowledgeChangePeer
| PortEvent::AcknowledgePort
| PortEvent::FailPort(_)) => EventAndEncoder::Other(event),
| PortEvent::AcknowledgePort) => EventAndEncoder::Other(event),
};
Self {
port_id,
Expand Down Expand Up @@ -2629,4 +2642,18 @@ pub mod tests {
p1.send(1);
assert_eq!(p2.recv().await.unwrap(), 1);
}

#[async_test]
async fn test_fail_port() {
#[derive(Debug, Error)]
#[error("test failure")]
struct ExplicitFailure;

let (node, node2, _h) = new_two_node_mesh();
let (p1, mut p2) = new_remote_port_pair(&node, &node2);
let p1 = Port::from(p1);
p1.fail(NodeError::local(ExplicitFailure));
let err = p2.recv().await.unwrap_err();
assert!(matches!(err, RecvError::Failed));
}
}

0 comments on commit c36aa31

Please sign in to comment.