mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 01:23:55 +08:00
update:优化资源释放判断
This commit is contained in:
@@ -805,6 +805,9 @@ class ConnectionHandler:
|
|||||||
type, text, audio_data, report_time = item
|
type, text, audio_data, report_time = item
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# 检查线程池状态
|
||||||
|
if self.executor is None:
|
||||||
|
continue
|
||||||
# 提交任务到线程池
|
# 提交任务到线程池
|
||||||
self.executor.submit(
|
self.executor.submit(
|
||||||
self._process_report, type, text, audio_data, report_time
|
self._process_report, type, text, audio_data, report_time
|
||||||
@@ -835,35 +838,47 @@ class ConnectionHandler:
|
|||||||
|
|
||||||
async def close(self, ws=None):
|
async def close(self, ws=None):
|
||||||
"""资源清理方法"""
|
"""资源清理方法"""
|
||||||
|
try:
|
||||||
|
# 取消超时任务
|
||||||
|
if self.timeout_task:
|
||||||
|
self.timeout_task.cancel()
|
||||||
|
self.timeout_task = None
|
||||||
|
|
||||||
# 取消超时任务
|
# 清理MCP资源
|
||||||
if self.timeout_task:
|
if hasattr(self, "mcp_manager") and self.mcp_manager:
|
||||||
self.timeout_task.cancel()
|
await self.mcp_manager.cleanup_all()
|
||||||
self.timeout_task = None
|
|
||||||
|
|
||||||
# 清理MCP资源
|
# 触发停止事件
|
||||||
if hasattr(self, "mcp_manager") and self.mcp_manager:
|
if self.stop_event:
|
||||||
await self.mcp_manager.cleanup_all()
|
self.stop_event.set()
|
||||||
|
|
||||||
# 触发停止事件
|
# 等待上报队列处理完成
|
||||||
if self.stop_event:
|
if hasattr(self, "report_queue"):
|
||||||
self.stop_event.set()
|
try:
|
||||||
|
# 添加毒丸对象
|
||||||
|
self.report_queue.put(None)
|
||||||
|
# 等待队列处理完成
|
||||||
|
self.report_queue.join()
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.bind(tag=TAG).error(f"等待上报队列处理完成时出错: {e}")
|
||||||
|
|
||||||
# 清空任务队列
|
# 清空任务队列
|
||||||
self.clear_queues()
|
self.clear_queues()
|
||||||
|
|
||||||
# 关闭WebSocket连接
|
# 关闭WebSocket连接
|
||||||
if ws:
|
if ws:
|
||||||
await ws.close()
|
await ws.close()
|
||||||
elif self.websocket:
|
elif self.websocket:
|
||||||
await self.websocket.close()
|
await self.websocket.close()
|
||||||
|
|
||||||
# 最后关闭线程池(避免阻塞)
|
# 最后关闭线程池(避免阻塞)
|
||||||
if self.executor:
|
if self.executor:
|
||||||
self.executor.shutdown(wait=False)
|
self.executor.shutdown(wait=False)
|
||||||
self.executor = None
|
self.executor = None
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).info("连接资源已释放")
|
self.logger.bind(tag=TAG).info("连接资源已释放")
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.bind(tag=TAG).error(f"关闭连接时出错: {e}")
|
||||||
|
|
||||||
def clear_queues(self):
|
def clear_queues(self):
|
||||||
"""清空所有任务队列"""
|
"""清空所有任务队列"""
|
||||||
|
|||||||
Reference in New Issue
Block a user