diff --git a/lib/miio/MiioSocket.js b/lib/miio/MiioSocket.js index cb3ba7a3da6..59c575b0378 100644 --- a/lib/miio/MiioSocket.js +++ b/lib/miio/MiioSocket.js @@ -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."); + } } }