-
Notifications
You must be signed in to change notification settings - Fork 52.3k
fix(telegram): shield batch flush from follow-up cancel #72037
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
necoweb3
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
necoweb3:fix/telegram-batch-flush-shield
base: main
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.
+128
−3
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -326,3 +326,98 @@ async def test_disconnect_cancels_all_pending_delivery_task_maps(self): | |
| assert adapter._media_group_events == {} | ||
| assert adapter._media_group_tasks == {} | ||
| assert adapter._polling_error_task is None | ||
|
|
||
|
|
||
| class TestBatchFlushNotCancelledByFollowUp: | ||
| """A follow-up chunk must never cancel a dispatch that is already running. | ||
|
|
||
| ``_enqueue_*`` unconditionally cancels the prior flush task, and the flush | ||
| pops the event out of its pending buffer *before* awaiting | ||
| ``handle_message``. Without a shield the popped event is in no buffer, was | ||
| never dispatched, and asyncio does not report cancelled tasks — so the | ||
| user's message is lost silently. The Discord adapter already shields this | ||
| exact path (#12444); these cover the Telegram equivalents. | ||
|
|
||
| ``handle_message`` must really suspend here: the production path awaits | ||
| ``asyncio.to_thread(self._apply_topic_recovery, event)`` before it durably | ||
| queues anything, so the cancellation window is always open. An | ||
| ``AsyncMock`` never suspends, which is why the existing tests miss this. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def _tracking_handler(entered, completed): | ||
| async def _handle(event): | ||
| label = event.text or f"<{len(event.media_urls)} media>" | ||
| entered.append(label) | ||
| await asyncio.sleep(0.3) | ||
| completed.append(label) | ||
| return _handle | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_text_follow_up_does_not_drop_in_flight_message(self): | ||
| adapter = _make_adapter() | ||
| entered, completed = [], [] | ||
| adapter.handle_message = self._tracking_handler(entered, completed) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace this timing assumption with entered/release |
||
|
|
||
| adapter._enqueue_text_event(_make_event("first message")) | ||
| await asyncio.sleep(0.15) # flush fired at ~0.1s; dispatch is in flight | ||
| adapter._enqueue_text_event(_make_event("second message")) | ||
| await asyncio.sleep(0.8) | ||
|
|
||
| assert entered == ["first message", "second message"] | ||
| assert completed == ["first message", "second message"], ( | ||
| "the in-flight first message was cancelled and silently lost" | ||
| ) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_photo_follow_up_does_not_drop_in_flight_batch(self): | ||
| adapter = _make_adapter() | ||
| adapter._media_batch_delay_seconds = 0.1 | ||
| entered, completed = [], [] | ||
| adapter.handle_message = self._tracking_handler(entered, completed) | ||
|
|
||
| first = _make_event("photo one") | ||
| first.media_urls = ["u1"] | ||
| first.media_types = ["image"] | ||
| adapter._enqueue_photo_event("k", first) | ||
| await asyncio.sleep(0.15) | ||
| second = _make_event("photo two") | ||
| second.media_urls = ["u2"] | ||
| second.media_types = ["image"] | ||
| adapter._enqueue_photo_event("k", second) | ||
| await asyncio.sleep(0.8) | ||
|
|
||
| assert completed == ["photo one", "photo two"], ( | ||
| "the in-flight photo batch was cancelled and silently lost" | ||
| ) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_media_group_follow_up_does_not_drop_in_flight_album(self): | ||
| adapter = _make_adapter() | ||
| adapter.MEDIA_GROUP_WAIT_SECONDS = 0.1 | ||
| entered, completed = [], [] | ||
| adapter.handle_message = self._tracking_handler(entered, completed) | ||
|
|
||
| first = _make_event("album caption") | ||
| first.media_urls = ["u1"] | ||
| first.media_types = ["image"] | ||
| adapter._media_group_events["mg1"] = first | ||
| adapter._media_group_tasks["mg1"] = asyncio.create_task( | ||
| adapter._flush_media_group_event("mg1") | ||
| ) | ||
| await asyncio.sleep(0.15) | ||
| second = _make_event("") | ||
| second.media_urls = ["u2"] | ||
| second.media_types = ["image"] | ||
| adapter._media_group_events["mg1"] = second | ||
| prior = adapter._media_group_tasks.get("mg1") | ||
| if prior: | ||
| prior.cancel() | ||
| adapter._media_group_tasks["mg1"] = asyncio.create_task( | ||
| adapter._flush_media_group_event("mg1") | ||
| ) | ||
| await asyncio.sleep(0.8) | ||
|
|
||
| assert "album caption" in completed, ( | ||
| "the in-flight album (carrying the caption) was cancelled and lost" | ||
| ) | ||
Oops, something went wrong.
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.
shieldleaves this inner dispatch outside_cancel_pending_delivery_tasks: teardown cancels and awaits the tracked outer flush task, then clears its maps. Please retain a cancellable/awaitable dispatch task for shutdown and use shielding only for normal successor supersession.