Skip to content

Commit

Permalink
fix(miio): Discard handshake packets with older stamps to avoid endle…
Browse files Browse the repository at this point in the history
…ss loops with 100% cpu
  • Loading branch information
Hypfer committed Mar 8, 2021
1 parent 9fe7efa commit e274cf1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/miio/MiioSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,21 @@ class MiioSocket {
Logger.debug(this.name +": Discarding pong");
return;
} else {
// keep-alive packet. respond with echo
Logger.debug(">>> " + this.name + (msg ? ":" : "*"), JSON.stringify({stamp: decodedResponse.stamp}));
this.socket.send(incomingMsg, 0, incomingMsg.length, this.rinfo.port, this.rinfo.address);
if (this.stamp.val === undefined || this.stamp.val <= decodedResponse.stamp) {
// keep-alive packet. respond with echo
Logger.debug(">>> " + this.name + (msg ? ":" : "*"), JSON.stringify({stamp: decodedResponse.stamp}));
this.socket.send(incomingMsg, 0, incomingMsg.length, this.rinfo.port, this.rinfo.address);
} else {
/**
* Valetudo and the miio_client might enter a 100% cpu busy loop here by exchanging messages with alternating stamps
* e.g. 34->33->34->33 etc
* every 1ms
*
* This could either be some kind of race condition or us misinterpreting something in the miio packet
* In any case, ignoring keep-alives for older stamps seems to help against it
*/
Logger.warn("MiioSocket " + this.name + ": Received keep-alive packet with stamp " + decodedResponse.stamp + " but we're at " + this.stamp.val + ". Discarding.");
}
}
}

Expand Down

0 comments on commit e274cf1

Please sign in to comment.