[fix] use thread-local aiohttp.ClientSession, as it is not thread-safe#2565
Open
utterstep wants to merge 1 commit intoeternnoir:masterfrom
Open
[fix] use thread-local aiohttp.ClientSession, as it is not thread-safe#2565utterstep wants to merge 1 commit intoeternnoir:masterfrom
utterstep wants to merge 1 commit intoeternnoir:masterfrom
Conversation
Collaborator
|
@utterstep Thank you. @coder2020official LGTM. |
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.
Description
Fix thread-safety issue in async SessionManager.
SessionManagerintelebot/asyncio_helper.pyis currently a module-level singleton. Itssessionattribute (which is anaiohttp.ClientSession) was stored as a plain instance variable, meaning it was shared across all threads => when multiple threads each run their own event loop with separate AsyncTeleBot instances, they all use the sameaiohttp.ClientSession— which is not thread-safe.I encountered this issue myself – were getting unpredictable
2026-02-26 23:26:15,914 (asyncio_helper.py:103 workflow-exec_1) ERROR - TeleBot: "Unknown error: RuntimeError"errors in separate threads, each with its own eventloop andAsyncTeleBotinstance local to this loop.This PR wraps the session attribute in
threading.local(), so each thread gets its ownaiohttp.ClientSession, matching how the sync side already handles this viautil.per_thread.Describe your tests
Confirmed that multiple
AsyncTeleBotinstances running on separate threads with separate event loops no longer share aiohttp session state across threads.Python version: 3.12
OS: macOS
Checklist:
I added/edited example on new feature/change (if exists)n/a, see belowI made changes both for sync and asyncn/a, see belowNo examples needed — this is an internal bugfix with no external API changes.
The sync side (
apihelper.py) already uses util.per_thread for thread-local sessions, so no sync changes were needed.