Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import itertools
import logging
import time
Expand Down Expand Up @@ -436,7 +437,42 @@ def run(self) -> Awaitable[Any]:
return coro

async def terminate(self) -> None:
self.shutdown_event.set()
if hasattr(self, "shutdown_event"):
self.shutdown_event.set()
await self._close_reverse_ws_connections()

async def _close_reverse_ws_connections(self) -> None:
api_clients = getattr(self.bot, "_wsr_api_clients", None)
event_clients = getattr(self.bot, "_wsr_event_clients", None)

ws_clients: set[Any] = set()
if isinstance(api_clients, dict):
ws_clients.update(api_clients.values())
if isinstance(event_clients, set):
ws_clients.update(event_clients)

close_tasks: list[Awaitable[Any]] = []
for ws in ws_clients:
close_func = getattr(ws, "close", None)
if not callable(close_func):
continue
try:
close_result = close_func(code=1000, reason="Adapter shutdown")
except TypeError:
close_result = close_func()
except Exception:
Comment on lines +459 to +463
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk):close() 过程中捕获并静默忽略所有异常,可能会掩盖真实的关闭问题。

通过 except Exception: continue 吞掉所有错误,会隐藏客户端 close 实现中的失败,从而让关闭相关问题难以排查。请至少在继续前把异常记录到日志中(例如 debug 级别),这样在不打断关闭流程的前提下,失败的关闭操作仍然是可观测的。

建议实现:

            try:
                close_result = close_func(code=1000, reason="Adapter shutdown")
            except TypeError:
                close_result = close_func()
            except Exception as exc:
                logger.debug(
                    "Failed to close reverse websocket %r during adapter shutdown",
                    ws,
                    exc_info=True,
                )
                continue

为了让上述代码能正确编译并按预期工作,请确保:

  1. 在该模块中定义了一个 logger,例如在 aiocqhttp_platform_adapter.py 顶部附近:

    • import logging
    • logger = logging.getLogger(__name__)
  2. 如果该模块已经在使用不同的日志记录约定(例如 self.logger 或共享 logger),请将 logger.debug(...) 替换为与现有风格一致的日志记录引用。

Original comment in English

suggestion (bug_risk): Catching and silently ignoring all exceptions during close() may hide real shutdown issues.

Swallowing all errors via except Exception: continue hides failures in the client’s close implementation and makes shutdown issues hard to diagnose. Please at least log the exception (e.g. at debug level) before continuing so failed closes remain observable without interrupting shutdown.

Suggested implementation:

            try:
                close_result = close_func(code=1000, reason="Adapter shutdown")
            except TypeError:
                close_result = close_func()
            except Exception as exc:
                logger.debug(
                    "Failed to close reverse websocket %r during adapter shutdown",
                    ws,
                    exc_info=True,
                )
                continue

To make this compile and work as intended, ensure that:

  1. A logger is defined in this module, e.g. near the top of aiocqhttp_platform_adapter.py:

    • import logging
    • logger = logging.getLogger(__name__)
  2. If the module already uses a different logging convention (for example, self.logger or a shared logger), replace logger.debug(...) with the appropriate logger reference to match the existing style.

continue
Comment on lines +463 to +464
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The except Exception: block is very broad and swallows all exceptions without logging them. This can make debugging difficult if a close operation fails for an unexpected reason during the adapter's shutdown. It's recommended to log the exception to provide better visibility into potential issues.

Suggested change
except Exception:
continue
except Exception as e:
logger.exception(f"Failed to close WebSocket client: {e}")
continue


if inspect.isawaitable(close_result):
close_tasks.append(close_result)

if close_tasks:
await asyncio.gather(*close_tasks, return_exceptions=True)

if isinstance(api_clients, dict):
api_clients.clear()
if isinstance(event_clients, set):
event_clients.clear()

async def shutdown_trigger_placeholder(self) -> None:
await self.shutdown_event.wait()
Expand Down