Reapply/aiohttp fix#139
Conversation
Replace per-request aiohttp.ClientSession creation with a shared session pool (one per event loop) to enable HTTP connection reuse across async API calls. SSL context is cached and shared across all sessions. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Streaming requests were using aiohttp.ClientTimeout(total=...) which kills the connection after the deadline even if the model is still actively producing tokens. Switch to ClientTimeout(sock_read=...) for streaming requests so the timeout only triggers on idle connections (no data between chunks). Non-streaming requests still use total timeout. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
The atexit handler created a new event loop to close sessions bound to the original loop, which silently failed. OS reclaims resources on exit, so explicit close is unnecessary. Also adds stale-entry cleanup in get_shared_aio_session() to prevent unbounded dict growth when event loops are repeatedly created and closed. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…ings to English Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Remove orphaned try: that was left after PR #138 removed the redundant try/except wrapping in _handle_aio_request. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a shared aiohttp session pool with a cached SSL context in dashscope/api_entities/aio_session.py to enable connection reuse across asynchronous API calls. It updates aiohttp_request.py and http_request.py to utilize this shared session pool instead of creating temporary sessions, and refactors timeout configurations for streaming and non-streaming requests. Additionally, it translates numerous Chinese comments and docstrings to English across various modules, bumps the package version, and adds/updates corresponding unit tests. The code review feedback highlights a potential thread-safety issue in multi-threaded environments when accessing the global weakref.WeakKeyDictionary and lazily initializing the SSL context. The reviewer suggests importing threading and using a threading.RLock() to synchronize access to these shared resources.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Protect _shared_ssl_context lazy init and _aio_sessions dict access with an RLock to prevent race conditions in multi-threaded environments. Use RLock instead of Lock since get_shared_aio_session calls get_ssl_context while holding the lock. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Description
[Describe what this PR does and why]
Related Issue: Fixes #[issue_number] or Relates to #[issue_number]
Security Considerations: [Check if API keys or sensitive credentials are exposed in code/logs]
Type of Change
Component(s) Affected
Checklist
Testing
[How to test these changes]
Additional Notes
[Optional: any other context]