Skip to content

WebSocket 线程未设为 Daemon,阻止程序正常退出 #154

@zentai

Description

@zentai

🐞 Bug 回报:WebSocket 线程未设为 Daemon,阻止程序正常退出

📌 问题描述

在使用 Huobi Python SDK 的过程中,我们发现 websocket_manage.py 中启动的 WebSocket 线程 未设置为 daemon,这会造成即使主程序结束、调用 sys.exit(),程序依然无法退出,必须通过 Ctrl+C 强制终止。


🔍 复现步骤

  1. 初始化并启动任意 WebSocket 订阅。
  2. 等待程序逻辑完成(例如完成一轮策略或扫描任务)。
  3. 调用 sys.exit()
  4. 观察程序仍未退出,并通过 threading.enumerate() 查看仍有 websocket_func 存活线程(daemon=False)。
# 示例输出
[ALIVE THREAD] MainThread, daemon=False  
[ALIVE THREAD] Thread-2 (websocket_func), daemon=False  
[ALIVE THREAD] Thread-3 (websocket_func), daemon=False

💥 问题代码位置
文件:huobi/connection/impl/websocket_manage.py

def connect(self):
    if self.state == ConnectionState.CONNECTED:
        self.logger.info("[Sub][" + str(self.id) + "] Already connected")
    else:
        self.__thread = threading.Thread(target=websocket_func, args=[self])   #  **daemon=True**
        self.__thread.start()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions