fix: 更改 MessageManager 的属性检查方式以防止错误

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
晴猫
2025-05-01 14:34:59 +09:00
parent 262b3a536c
commit de68a6a8ee

View File

@@ -146,6 +146,7 @@ class MessageManager:
"""管理所有聊天流的消息容器 (不再是单例)""" """管理所有聊天流的消息容器 (不再是单例)"""
def __init__(self): def __init__(self):
self._processor_task = None
self.containers: dict[str, MessageContainer] = {} self.containers: dict[str, MessageContainer] = {}
self.storage = MessageStorage() # 添加 storage 实例 self.storage = MessageStorage() # 添加 storage 实例
self._running = True # 处理器运行状态 self._running = True # 处理器运行状态
@@ -155,7 +156,7 @@ class MessageManager:
async def start(self): async def start(self):
"""启动后台处理器任务。""" """启动后台处理器任务。"""
# 检查是否已有任务在运行,避免重复启动 # 检查是否已有任务在运行,避免重复启动
if hasattr(self, "_processor_task") and not self._processor_task.done(): if self._processor_task is not None and not self._processor_task.done():
logger.warning("Processor task already running.") logger.warning("Processor task already running.")
return return
self._processor_task = asyncio.create_task(self._start_processor_loop()) self._processor_task = asyncio.create_task(self._start_processor_loop())
@@ -164,7 +165,7 @@ class MessageManager:
def stop(self): def stop(self):
"""停止后台处理器任务。""" """停止后台处理器任务。"""
self._running = False self._running = False
if hasattr(self, "_processor_task") and not self._processor_task.done(): if self._processor_task is not None and not self._processor_task.done():
self._processor_task.cancel() self._processor_task.cancel()
logger.debug("MessageManager processor task stopping.") logger.debug("MessageManager processor task stopping.")
else: else: