Fix network_retry_loop stop_event handling: blocking shutdown, orphaned tasks, and runaway repeat loop - #5310
Open
xsneh wants to merge 1 commit into
Open
Conversation
xsneh
force-pushed
the
networkloop-stop-event-sleep
branch
4 times, most recently
from
July 27, 2026 11:07
b140442 to
f85a252
Compare
…twork outages Bug 1: Replace bare asyncio.sleep(cur_interval) with an event-aware asyncio.wait_for(stop_event.wait(), timeout=cur_interval) so that the backoff sleep is interrupted immediately when the stop_event is set. Previously, a bot experiencing a network outage could hang for up to 30 seconds on graceful shutdown while the backoff sleep ran uninterrupted. Bug 2: After calling .cancel() on pending asyncio tasks inside do_action(), await asyncio.gather(*pending, return_exceptions=True) to ensure cancelled tasks are fully cleaned up before returning. Without this, merely requesting cancellation left tasks in a pending state, causing 'Task was destroyed but it is pending!' warnings from the garbage collector when the tasks were eventually collected.
xsneh
force-pushed
the
networkloop-stop-event-sleep
branch
from
July 27, 2026 11:10
f85a252 to
fa2bd5f
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes three related bugs in
networkloop.pycaused bystop_eventbeing partially ignored during shutdown:network_retry_loopused a bareasyncio.sleepduring its retry backoff, ignoringstop_evententirely. This caused shutdown routines to hang for up to 30 seconds during network outages. Replaced withasyncio.wait_for(stop_event.wait(), timeout=...).do_actionreturned, leading to"Task was destroyed but it is pending!"garbage collector warnings. Addedasyncio.gather(*pending, return_exceptions=True)to ensure clean teardown.repeat_on_success=True, the loop failed to break ifstop_eventwas set during an action. The loop would immediately restart and spin indefinitely. Added explicitstop_event.is_set()exit checks to break the outer loop safely.Adds three regression tests to prevent recurrence.
Tests
Results:
Live verification:
Ran a custom asyncio verification script locally simulating network outages. Confirmed the event loop exits promptly on shutdown and no pending task warnings are emitted during teardown.