Skip to content

Commit 1ed7b9a

Browse files
author
gilangjavier
committed
fix: keep strong references to background tasks in AsyncTeleBot to prevent premature GC
1 parent 23aee2b commit 1ed7b9a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

telebot/async_telebot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def __init__(self, token: str, parse_mode: Optional[str]=None, offset: Optional[
191191
self.middlewares = []
192192

193193
self._user = None # set during polling
194+
self._background_tasks = set()
194195

195196
if validate_token:
196197
util.validate_token(self.token)
@@ -453,7 +454,9 @@ async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout:
453454
if updates:
454455
self.offset = updates[-1].update_id + 1
455456
# noinspection PyAsyncCall
456-
asyncio.create_task(self.process_new_updates(updates)) # Seperate task for processing updates
457+
task = asyncio.create_task(self.process_new_updates(updates))
458+
self._background_tasks.add(task)
459+
task.add_done_callback(self._background_tasks.discard)
457460
if interval: await asyncio.sleep(interval)
458461
error_interval = 0.25 # drop error_interval if no errors
459462

0 commit comments

Comments
 (0)