Skip to content

Reapply/aiohttp fix#139

Merged
lzsweb merged 8 commits into
mainfrom
reapply/aiohttp-fix
Jun 15, 2026
Merged

Reapply/aiohttp fix#139
lzsweb merged 8 commits into
mainfrom
reapply/aiohttp-fix

Conversation

@lzsweb

@lzsweb lzsweb commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactoring

Component(s) Affected

  • Model
  • Application
  • Common
  • Documentation
  • Tests
  • CI/CD

Checklist

  • Pre-commit hooks pass
  • Tests pass locally
  • Documentation updated (if needed)
  • Ready for review

Testing

[How to test these changes]

Additional Notes

[Optional: any other context]

zhansheng.lzs and others added 7 commits June 15, 2026 17:19
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>

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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.

Comment thread dashscope/api_entities/aio_session.py
Comment thread dashscope/api_entities/aio_session.py
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>
@lzsweb lzsweb merged commit 1c7f3cd into main Jun 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant