jasonrbriggs / stomp.py

“stomp.py” is a Python client library for accessing messaging servers (such as ActiveMQ or RabbitMQ) using the STOMP protocol (versions 1.0, 1.1 and 1.2). It can also be run as a standalone, command-line client for testing.
Apache License 2.0
491 stars 167 forks source link

Use heartbeat_terminate_event as heartbeat loop exit condition to avoid a race condition #433

Open Giz opened 3 months ago

Giz commented 3 months ago

What can happen (and really does happen) without this small change is that:

In this way on the next connection heartbeat_terminate_event will already be set and at the first opportunity the break on line 281 will be called interrupting the sending of heartbeats. When the server forces the disconnection, on_disconnected will be called setting heartbeat_terminate_event again. This recreates the previous situation where the sending of heatbeats is immediately stopped.

Giz commented 2 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).