Open Giz opened 3 months ago
I added a second commit to move the heartbeat_terminate_event cleanup before the heartbeat thread starts. This is because there are cases in which on_disconnected is called without first calling on_connected, creating a situation similar to that described in the previous comment (the loop starts with heartbeat_terminate_event set, the heartbeat stops immediately, after a while the server closes the connection, on_disconnected is called, at the next connection heartbeat_terminate_event is already set).
What can happen (and really does happen) without this small change is that:
HeartbeatListener.on_disconnected
is called;self.running
is set toFalse
;self.running
is evaluated and the while loop ofHeartbeatListener.__heartbeat_loop
exits;self.heartbeat_terminate_event.clear()
is called;self.heartbeat_terminate_event.set()
is called.In this way on the next connection
heartbeat_terminate_event
will already be set and at the first opportunity thebreak
on line 281 will be called interrupting the sending of heartbeats. When the server forces the disconnection,on_disconnected
will be called settingheartbeat_terminate_event
again. This recreates the previous situation where the sending of heatbeats is immediately stopped.