-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Retain strong refs to update-processing tasks in AsyncTeleBot #2588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dramex
wants to merge
2
commits into
eternnoir:master
Choose a base branch
from
Dramex:fix/2572-retain-update-processing-tasks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Unit tests for `telebot.async_telebot.AsyncTeleBot`. | ||
|
|
||
| These tests are self-contained (no TOKEN/CHAT_ID required) and stub out all | ||
| network I/O. | ||
| """ | ||
| import asyncio | ||
|
|
||
| from telebot import types | ||
| from telebot.async_telebot import AsyncTeleBot | ||
|
|
||
|
|
||
| def _make_fake_me() -> types.User: | ||
| return types.User.de_json({ | ||
| "id": 1, | ||
| "is_bot": True, | ||
| "first_name": "Test", | ||
| "username": "test_bot", | ||
| }) | ||
|
|
||
|
|
||
| def test_process_polling_retains_update_processing_tasks(): | ||
| """Regression test for issue #2572. | ||
|
|
||
| Tasks fired by `_process_polling` for `process_new_updates` must be held | ||
| in `self._pending_tasks` while running and discarded on completion, so | ||
| they cannot be garbage-collected mid-execution. | ||
| """ | ||
| bot = AsyncTeleBot("1:fake", validate_token=False) | ||
|
|
||
| task_was_tracked_during_run: list[bool] = [] | ||
| process_completed = asyncio.Event() | ||
|
|
||
| async def fake_process_new_updates(updates): | ||
| current = asyncio.current_task() | ||
| task_was_tracked_during_run.append(current in bot._pending_tasks) | ||
| process_completed.set() | ||
|
|
||
| async def fake_get_me(): | ||
| return _make_fake_me() | ||
|
|
||
| # Deliver a single update batch, then stop polling on the next tick. | ||
| fake_update = types.Update.de_json({"update_id": 1}) | ||
| call_count = {"n": 0} | ||
|
|
||
| async def fake_get_updates(*args, **kwargs): | ||
| call_count["n"] += 1 | ||
| if call_count["n"] == 1: | ||
| return [fake_update] | ||
| bot._polling = False | ||
| return [] | ||
|
|
||
| async def noop(): | ||
| return None | ||
|
|
||
| bot.get_me = fake_get_me | ||
| bot.get_updates = fake_get_updates | ||
| bot.process_new_updates = fake_process_new_updates | ||
| bot.close_session = noop # stub: no real aiohttp session in tests | ||
|
|
||
| async def driver(): | ||
| await bot._process_polling(non_stop=True, interval=0, timeout=0) | ||
| # Allow the fire-and-forget task to finish plus one yield for the | ||
| # add_done_callback discard to run. A timeout guards against the | ||
| # stub ever being rewired such that the processing task never runs. | ||
| await asyncio.wait_for(process_completed.wait(), timeout=1) | ||
| await asyncio.sleep(0) | ||
|
|
||
| asyncio.run(driver()) | ||
|
|
||
| assert task_was_tracked_during_run == [True], ( | ||
| "In-flight processing task must be held by _pending_tasks" | ||
| ) | ||
| assert bot._pending_tasks == set(), ( | ||
| "Completed processing tasks must be discarded from _pending_tasks" | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The done callback currently only discards the task from
_pending_tasks. Ifprocess_new_updatesraises, the task’s exception is never retrieved/observed, which can lead to noisyTask exception was never retrievedwarnings. Consider extending the done-callback to calltask.exception()(and possibly log via_handle_exception/logger) before discarding, so failures aren’t silently ignored.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentionally left out of this PR. Adding
task.exception()/_handle_exceptioninto the done callback is a real behaviour change: today an unhandled exception inprocess_new_updatessurfaces through Python's default unobserved-exception handler, which routes to whatever logging the user has configured. Moving that into the library would mean deciding whether to swallow, re-raise, or log at a particular level, and whether to reuse the polling loop's_handle_exception. That feels like a separate design conversation rather than a follow-on to the strong-ref fix. Happy to open a follow-up if @eternnoir wants to pull it in.