perf: 复用 AsyncClient 替代 ThreadPoolExecutor 避免 SSL 上下文阻塞#162
Merged
weinibuliu merged 2 commits intomainfrom Feb 21, 2026
Merged
Conversation
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并给出了一些总体反馈:
- 全局的
_client = httpx.AsyncClient()从未被关闭,这可能导致连接泄漏并产生警告;建议在check_update()中使用async with代码块进行管理,或添加显式的启动/关闭钩子来创建并关闭共享的客户端。 - 由于
asyncio.gather(..., return_exceptions=True)现在可能会直接返回来自_get_from_pypi的异常,需要确保后续逻辑明确过滤掉Exception实例,以避免将其当作有效的版本结果来处理。
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The global `_client = httpx.AsyncClient()` is never closed, which can leak connections and emit warnings; consider managing it with an `async with` block in `check_update()` or adding an explicit startup/shutdown hook to create and close a shared client.
- Since `asyncio.gather(..., return_exceptions=True)` can now surface exceptions directly from `_get_from_pypi`, ensure the subsequent logic explicitly filters out `Exception` instances to avoid treating them as valid version results.
## Individual Comments
### Comment 1
<location> `src/MaaDebugger/utils/update_checker/check.py:13` </location>
<code_context>
TSINGHUA_PYPI_API = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/json/maadebugger"
-_executor = ThreadPoolExecutor(max_workers=2)
+_client = httpx.AsyncClient()
</code_context>
<issue_to_address>
**issue (bug_risk):** The module-level AsyncClient instance is never closed, which can leak connections/resources.
Because `_client` is created at import time and never closed, its connections/files may remain open for the entire process lifetime. Either instantiate it where used via an async context manager, or add an explicit lifecycle hook (e.g. `async def shutdown()` calling `await _client.aclose()`) and ensure it runs on application shutdown to prevent leaks.
</issue_to_address>帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- The global
_client = httpx.AsyncClient()is never closed, which can leak connections and emit warnings; consider managing it with anasync withblock incheck_update()or adding an explicit startup/shutdown hook to create and close a shared client. - Since
asyncio.gather(..., return_exceptions=True)can now surface exceptions directly from_get_from_pypi, ensure the subsequent logic explicitly filters outExceptioninstances to avoid treating them as valid version results.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The global `_client = httpx.AsyncClient()` is never closed, which can leak connections and emit warnings; consider managing it with an `async with` block in `check_update()` or adding an explicit startup/shutdown hook to create and close a shared client.
- Since `asyncio.gather(..., return_exceptions=True)` can now surface exceptions directly from `_get_from_pypi`, ensure the subsequent logic explicitly filters out `Exception` instances to avoid treating them as valid version results.
## Individual Comments
### Comment 1
<location> `src/MaaDebugger/utils/update_checker/check.py:13` </location>
<code_context>
TSINGHUA_PYPI_API = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/json/maadebugger"
-_executor = ThreadPoolExecutor(max_workers=2)
+_client = httpx.AsyncClient()
</code_context>
<issue_to_address>
**issue (bug_risk):** The module-level AsyncClient instance is never closed, which can leak connections/resources.
Because `_client` is created at import time and never closed, its connections/files may remain open for the entire process lifetime. Either instantiate it where used via an async context manager, or add an explicit lifecycle hook (e.g. `async def shutdown()` calling `await _client.aclose()`) and ensure it runs on application shutdown to prevent leaks.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the update checker to use a reusable httpx.AsyncClient instead of ThreadPoolExecutor, addressing SSL context blocking issues (#160). The change simplifies the async implementation by using native async HTTP calls rather than wrapping synchronous calls in thread pool executors.
Changes:
- Replaced
ThreadPoolExecutorwith a module-levelhttpx.AsyncClientinstance - Converted
_sync_get_from_pypi()to async_get_from_pypi()function - Simplified
check_update()to directly await async calls usingasyncio.gather()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: (#160)
Summary by Sourcery
增强功能:
ThreadPoolExecutor的同步 HTTP 调用替换为可复用的httpx.AsyncClient,以实现对 PyPI 版本的非阻塞检查。Original summary in English
Summary by Sourcery
Enhancements: