Issue with websocket close_connection()


  • This is for the Python SDK.

    When I try to close the connection using the line sws.close_connection(), I always get
    [W 240823 15:00:24 smartWebSocketV2:319] Attempting to resubscribe/reconnect (Attempt 1)...
    followed by
    [W 240823 15:00:34 smartWebSocketV2:343] Connection closed due to max retry attempts reached. This happens when I forcefully terminate the thread using the ctypes library.

    If I don't forcefully terminate the thread, it just reconnects and subscribes to the feed.

    Why is this happening Admin? I checked line 343 in the source file and it's in the _on_error method. Why is close_connection generating an error?

    Also the sws.unsubscribe() method tries to resubscribe immediately as the self.RESUBSCRIBE_FLAG is set to True. It should be set to False when we are trying to unsubscribe.


  • I am also facing the same issue.
    Whenever I give close_connection command, it reconnect on its own?

    Is there any temporary fix you have done? @pari
    @Moderator_3


  • @pari

    We are working on it.

    Regards,
    SmartAPI Team


  • Any response Admin? I noticed that the DISCONNECT flag is set to true in the close_connection() method, but in the connect() method it is set to run_forever without ever checking the DISCONNECT flag.


  • @pari,

    It seems you're experiencing issues with the close_connection method in the Python SDK, which is leading to automatic reconnection attempts and errors. Let me address each part of your question:

    Issue with close_connection Method: The warning [W 240823 15:00:24 smartWebSocketV2:319] Attempting to resubscribe/reconnect (Attempt 1)... followed by [W 240823 15:00:34 smartWebSocketV2:343] Connection closed due to max retry attempts reached indicates that the SDK is trying to reconnect or resubscribe even when you invoke close_connection.

    This behavior could be due to how the SDK handles connection closure internally. The _on_error method, which you mentioned, likely includes logic to automatically attempt reconnections or resubscriptions when errors occur or when the connection is closed unexpectedly. This might explain why you're seeing reconnection attempts even after calling close_connection.

    Why close_connection Generates an Error: The close_connection method should ideally cleanly close the connection without triggering additional errors or reconnections. However, if the SDK is designed to automatically retry connections under certain conditions, it might interpret the closure as an opportunity to reconnect. This could be due to internal flags or error-handling mechanisms that are not correctly set when closing the connection.

    Issue with unsubscribe Method: Regarding the sws.unsubscribe() method, if it’s setting self.RESUBSCRIBE_FLAG to True and immediately attempting to resubscribe, this could be an issue with the method's implementation. When you attempt to unsubscribe, the flag should be set to False to prevent automatic resubscription.

    To address this, you might want to review the code around the unsubscribe method to ensure that self.RESUBSCRIBE_FLAG is correctly managed. Ideally, the flag should be toggled appropriately based on the operation being performed (unsubscribe vs. resubscribe).

    Suggested Actions:
    Check SDK Documentation: Review the SDK documentation or source code to understand how close_connection and unsubscribe are intended to work. Look for any flags or settings related to reconnection and resubscription.
    Debug and Review Code: If you have access to the SDK source code, trace through the close_connection method and the _on_error method to see how they interact. Verify if there are any flags or internal states that might be influencing the behavior.
    Consult with SDK Support: If you’re unable to resolve the issue, consider reaching out to the SDK support team or community. Provide them with details of your issue and any relevant code snippets or configurations.
    Feel free to provide more details or context if you need further assistance!

    Best regards
    Benchkart

    ChatGPT can make mistakes. Check important info.