fix: don't hang on while loop when reconnecting #1663
Merged
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.
Pull Request Type
Description
There have been some user reports on 3.12 specifically (not sure why - the bug doesn't appear to have anything to do with anything 3.12 related) that some reconnects cause the CPU to spike to 100% and for the program to freeze. Eventually someone reported it as an issue, and it just so happens that I have the time to investigate now.
It turns out that interactions.py was hanging on a while loop. I suggest reading the old code and a snippet from the guild create processor and spotting the issue yourself, because it sticks out to you when you notice:
Breaking down the old websocket code (let's assume
expected_guilds
> 1):Ready
event we have until all of these guilds are resolved). Notice how it's awhile True
block.self._guild_event
to be set. Let's say that it doesn't time out.self._guild_event
gets set, breaking us out of our waiting in our websocket ready.ready_guilds
is increased by 1 (making it 1), but this isn't equal toexpected_guilds
, so the code doesn't reach the break and loops again.self._guild_event
has been set, the wait instantly completes, even though no new guild create event has dispatched. The same conditions from the prior bullet point thus apply, causing it to repeat infinitely.I don't have any clue how this clue worked for 3.11 and lower, or if it even does. It's probable since that interactions.py rarely needs to do reconnects where
RESUME
isn't used, this issue was just never encountered. Either that or the improvements inasyncio
in 3.12 were that significant.Regardless, this PR fixes the issue by copying code from just above it (which only triggers during startup
READY
s), because... honestly, why were these two different in structure in the first place?Changes
Related Issues
#1662
Test Scenarios
Python Compatibility
3.10.x
3.12.x
Checklist
pre-commit
code linter over all edited files