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

[Possible Bug] stomp.Connection() timeout option not working as expected! #425

Closed bgb10 closed 8 months ago

bgb10 commented 8 months ago

Summary

An unexpected disconnection issue with stomp.Connection(timeout=5), where connections terminate after 5 seconds despite a successful initial connection, contrasting with Python socket behavior which maintains the connection post-timeout.

Problem

I'm running activeMQ in localhost, and I confirmed connection are successfully established.

If I give stomp.Connection() a timeout option as an argument, to my knowledge, timeout option would only affects only when initial connection. For example, if I use stomp.Connection(timeout=5), the expected behavior is that an exception will be thrown if connection establishment fails for 5 seconds on the first connection.

However, what I've observed is that the connection is being terminated after 5 seconds even if the connection successfully established and even when activeMQ is still available.

Code Example

If I run the code below, my expectation is that the timeout option is only applied to the initial connection, so if the connection is made within 5 seconds, the connection should be maintained. However, as you can see in terminal, the connection is disconnected after about 5 seconds. (I think the initial ‘False’ is due to the connection not being fully established yet)

import stomp
import time

conn = stomp.Connection(timeout=5)
conn.connect('localhost', 61613)

while True:
    message = "Hello, world!"
    print(f"Sent message: {message}")
    print(conn.is_connected())
    time.sleep(1)
Screenshot 2023-12-27 at 3 30 58 PM

If I write client code that does something similar with the python standard library's socket (server is on), the connection will still be maintained after the timeout has passed.

import socket
import time

def client_program():
    host = "localhost"  
    port = 5000

    client_socket = socket.socket()
    client_socket.connect((host, port)) 
    client_socket.settimeout(5)

    while True:
        if client_socket.fileno() == -1:
            print("Connection is closed.")
            break
        else:
            print("Connection is kept preserved.")
        time.sleep(1)

if __name__ == '__main__':
    client_program()
Screenshot 2023-12-27 at 3 39 23 PM

Suspected problem

There are a couple of possible problems.

Thank you for maintaining a good library. Hope for comments.

jasonrbriggs commented 8 months ago

Well your code is wrong to start with. The connect method takes a username and password (and probably wait=True). After that you're not actually sending anything, so the connection isn't actually used, and it would eventually time out by my understanding of the protocol. (I don't think your raw python connection example really proves the timeout behaviour either, because you're not actually using the socket after the initial connection).

Even if you did connect correctly, and were sending messages through the connection, I think you still need heartbeating enabled to keep the read connection open (i.e. if you connect without heartbeating and send messages, it will still time out after 5 seconds).

In summary, I don't think this is a bug.