forked from NousResearch/hermes-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(telegram): cancel delayed deliveries on disconnect #73
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
Merged
Merged
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
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.
🟡 Race condition: _polling_error_task orphaned during disconnect can degrade outbound sends after reconnect (bug)
In
_cancel_pending_delivery_tasks()(lines 2940-2985), after collecting and cancelling all pending flush tasks,asyncio.gather()at line 2976 yields to the event loop. During this yield, the PTB polling updater is still running (stopped later at line 3019). If a polling network error occurs during the gather window,_polling_error_callback(line 2793) fires and creates a new_polling_error_taskvialoop.create_task()(line 2808). After gather completes, lines 2984-2985 unconditionally setself._polling_error_task = None, discarding the reference to this newly created recovery task. The orphaned_handle_polling_network_errortask continues running and will: (1) set_send_path_degraded=True(line 1756), which blocks all outbound message sends (line 3064 returnssend_path_degraded); this flag persists across disconnect/reconnect cycles because_verify_polling_after_reconnectonly resets it ~60s after reconnect; (2) attempt recovery operations (start_polling()) on an app thatdisconnect()is actively shutting down, risking a start/stop race inside PTB.💡 Suggestion: Capture the _polling_error_task reference before the gather and only nullify it if the reference hasn't changed. For example:
prev = self._polling_error_task; ... gather ...; if self._polling_error_task is prev: self._polling_error_task = None. This preserves any newly-created task that arrived during the gather window. Additionally, consider checking_should_drop_delayed_delivery()in the_polling_error_callbackto suppress error recovery tasks during teardown.📋 Prompt for AI Agents
In plugins/platforms/telegram/adapter.py in
_cancel_pending_delivery_tasks(), captureprev = self._polling_error_taskafter the collect call and before the gather. After the gather, change lines 2984-2985 to only nullify if the reference still matches:if self._polling_error_task is prev: self._polling_error_task = None. This prevents orphaning a new error-recovery task created by the PTB callback during the gather window. Additionally, consider adding a_should_drop_delayed_delivery()guard at the top of_polling_error_callback(around line 2793) to suppress recovery task creation during teardown.