Skip to content

Commit af55848

Browse files
author
Devdutt Shenoi
authored
fix: don't panic on connection closure (#833)
1 parent a7a0c54 commit af55848

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

rumqttc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
* `size()` method on `Packet` calculates size once serialized.
1313
* `read()` and `write()` methods on `Packet`.
14+
* `ConnectionAborted` variant on `StateError` type to denote abrupt end to a connection
1415

1516
### Changed
1617

rumqttc/src/framed.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ impl Network {
3838
pub async fn read(&mut self) -> Result<Incoming, StateError> {
3939
match self.framed.next().await {
4040
Some(Ok(packet)) => Ok(packet),
41-
Some(Err(mqttbytes::Error::InsufficientBytes(_))) | None => unreachable!(),
41+
Some(Err(mqttbytes::Error::InsufficientBytes(_))) => unreachable!(),
4242
Some(Err(e)) => Err(StateError::Deserialization(e)),
43+
None => Err(StateError::ConnectionAborted),
4344
}
4445
}
4546

@@ -61,8 +62,9 @@ impl Network {
6162
break;
6263
}
6364
}
64-
Some(Err(mqttbytes::Error::InsufficientBytes(_))) | None => unreachable!(),
65+
Some(Err(mqttbytes::Error::InsufficientBytes(_))) => unreachable!(),
6566
Some(Err(e)) => return Err(StateError::Deserialization(e)),
67+
None => return Err(StateError::ConnectionAborted),
6668
}
6769
// do not wait for subsequent reads
6870
match self.framed.next().now_or_never() {

rumqttc/src/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum StateError {
2929
EmptySubscription,
3030
#[error("Mqtt serialization/deserialization error: {0}")]
3131
Deserialization(#[from] mqttbytes::Error),
32+
#[error("Connection closed by peer abruptly")]
33+
ConnectionAborted,
3234
}
3335

3436
/// State of the mqtt connection.

rumqttc/src/v5/framed.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ impl Network {
4040
pub async fn read(&mut self) -> Result<Incoming, StateError> {
4141
match self.framed.next().await {
4242
Some(Ok(packet)) => Ok(packet),
43-
Some(Err(mqttbytes::Error::InsufficientBytes(_))) | None => unreachable!(),
43+
Some(Err(mqttbytes::Error::InsufficientBytes(_))) => unreachable!(),
4444
Some(Err(e)) => Err(StateError::Deserialization(e)),
45+
None => Err(StateError::ConnectionAborted),
4546
}
4647
}
4748

@@ -63,8 +64,9 @@ impl Network {
6364
break;
6465
}
6566
}
66-
Some(Err(mqttbytes::Error::InsufficientBytes(_))) | None => unreachable!(),
67+
Some(Err(mqttbytes::Error::InsufficientBytes(_))) => unreachable!(),
6768
Some(Err(e)) => return Err(StateError::Deserialization(e)),
69+
None => return Err(StateError::ConnectionAborted),
6870
}
6971
// do not wait for subsequent reads
7072
match self.framed.next().now_or_never() {

rumqttc/src/v5/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub enum StateError {
6464
PubCompFail { reason: PubCompReason },
6565
#[error("Connection failed with reason '{reason:?}' ")]
6666
ConnFail { reason: ConnectReturnCode },
67+
#[error("Connection closed by peer abruptly")]
68+
ConnectionAborted
6769
}
6870

6971
impl From<mqttbytes::Error> for StateError {

0 commit comments

Comments
 (0)