fix: prevent task GC in AsyncTeleBot by tracking background tasks#2574
fix: prevent task GC in AsyncTeleBot by tracking background tasks#2574gilangjavier wants to merge 2 commits intoeternnoir:masterfrom
Conversation
…event premature GC
|
I checked this PR and currently no CI checks are being reported on the branch ( Could a maintainer confirm how CI should be triggered for external contributor PRs in this repo? If there is a label/command/manual trigger needed, I can follow it. |
|
Triggered. |
There was a problem hiding this comment.
Pull request overview
This PR fixes a potential asyncio garbage-collection issue in AsyncTeleBot polling by retaining strong references to “fire-and-forget” tasks created for update processing, aligning with Python’s asyncio.create_task() guidance.
Changes:
- Add an instance-level
_background_tasksset to retain createdasyncio.Taskobjects. - Track each
process_new_updates()task and discard it from the set when it completes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
telebot/async_telebot.py
Outdated
| task = asyncio.create_task(self.process_new_updates(updates)) | ||
| self._background_tasks.add(task) | ||
| task.add_done_callback(self._background_tasks.discard) |
telebot/async_telebot.py
Outdated
| task = asyncio.create_task(self.process_new_updates(updates)) | ||
| self._background_tasks.add(task) | ||
| task.add_done_callback(self._background_tasks.discard) |
telebot/async_telebot.py
Outdated
| self.middlewares = [] | ||
|
|
||
| self._user = None # set during polling | ||
| self._background_tasks = set() |
|
Follow-up with exact fixes for Copilot feedback:
Commit: a01eda6 Please re-review when convenient 🙏 |
This fix addresses the issue where was called without storing a strong reference to the task, leading to potential premature garbage collection. We now maintain a set of background tasks and add a done callback to discard them upon completion.Fixes #2572