From 932f7489bb36c8718fc129d3af05501a989b137a Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Tue, 2 Jan 2024 10:33:44 +0000 Subject: [PATCH] Reset heartbeat timer when attempting to reconnect This commit fixes a bug in the reconnect handling code where the client would overwrite a pre-existing heartbeat timer without first cancelling it. As a result, the old timer would eventually fire and trigger a HeartbeatFailedException closing the connection. --- lib/src/client/impl/client_impl.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/client/impl/client_impl.dart b/lib/src/client/impl/client_impl.dart index 7bea925..6d3421c 100644 --- a/lib/src/client/impl/client_impl.dart +++ b/lib/src/client/impl/client_impl.dart @@ -138,6 +138,7 @@ class _ClientImpl implements Client { // period has been configured, start monitoring incoming heartbeats. if (serverMessage.message is ConnectionOpenOk && tuningSettings.heartbeatPeriod.inSeconds > 0) { + _heartbeatRecvTimer?.cancel(); _heartbeatRecvTimer = RestartableTimer(tuningSettings.heartbeatPeriod, () { // Set the timer to null to avoid accidentally resetting it while @@ -269,6 +270,9 @@ class _ClientImpl implements Client { } Future _close({bool closeErrorStream = false}) { + _heartbeatRecvTimer?.cancel(); + _heartbeatRecvTimer = null; + if (_socket == null) { return Future.value(); }