Browse Source

Fix async iterators requesting past their bounds

This affects Messageable.history, ScheduledEvent.users,
Client.fetch_guilds, and Guild.audit_logs.

To illustrate the problem, Messageable.history counted returned
messages to tell when to stop iteration, but did so before filtering
away those past the before or after boundaries. When both
oldest_first=False and an after boundary were provided, this led to the
history iterator continuing to retrieve messages older than the after
boundary, which would then all be filtered away, continuing until the
message limit or the beginning of the entire channel was reached.

A similar situation would also occur with oldest_first=True and a
before boundary provided.

This commit changes the logic in these methods to count items after
filtering, so they stop requesting more as soon as the in-bounds items
are exhausted.
pull/10109/head
Eta 2 years ago
committed by dolfies
parent
commit
ab9356be83
  1. 14
      discord/abc.py
  2. 16
      discord/guild.py
  3. 12
      discord/scheduled_event.py

14
discord/abc.py

@ -1887,24 +1887,26 @@ class Messageable:
channel = await self._get_channel()
while True:
retrieve = min(100 if limit is None else limit, 100)
retrieve = 100 if limit is None else min(limit, 100)
if retrieve < 1:
return
data, state, limit = await strategy(retrieve, state, limit)
# Terminate loop on next iteration; there's no data left after this
if len(data) < 100:
limit = 0
if reverse:
data = reversed(data)
if predicate:
data = filter(predicate, data)
for raw_message in data:
count = 0
for count, raw_message in enumerate(data, 1):
yield self._state.create_message(channel=channel, data=raw_message)
if count < 100:
# There's no data left after this
break
def slash_commands(
self,
query: Optional[str] = None,

16
discord/guild.py

@ -2314,7 +2314,7 @@ class Guild(Hashable):
strategy, state = _after_strategy, after
while True:
retrieve = min(1000 if limit is None else limit, 1000)
retrieve = 1000 if limit is None else min(limit, 1000)
if retrieve < 1:
return
@ -3626,16 +3626,12 @@ class Guild(Hashable):
predicate = lambda m: int(m['id']) > after.id
while True:
retrieve = min(100 if limit is None else limit, 100)
retrieve = 100 if limit is None else min(limit, 100)
if retrieve < 1:
return
data, raw_entries, state, limit = await strategy(retrieve, state, limit)
# Terminate loop on next iteration; there's no data left after this
if len(raw_entries) < 100:
limit = 0
if reverse:
raw_entries = reversed(raw_entries)
if predicate:
@ -3650,7 +3646,9 @@ class Guild(Hashable):
)
automod_rule_map = {rule.id: rule for rule in automod_rules}
for raw_entry in raw_entries:
count = 0
for count, raw_entry in enumerate(raw_entries, 1):
# Weird Discord quirk
if raw_entry['action_type'] is None:
continue
@ -3662,6 +3660,10 @@ class Guild(Hashable):
guild=self,
)
if count < 100:
# There's no data left after this
break
async def ack(self) -> None:
"""|coro|

12
discord/scheduled_event.py

@ -549,25 +549,27 @@ class ScheduledEvent(Hashable):
predicate = lambda u: u['user']['id'] > after.id
while True:
retrieve = min(100 if limit is None else limit, 100)
retrieve = 100 if limit is None else min(limit, 100)
if retrieve < 1:
return
data, state, limit = await strategy(retrieve, state, limit)
if len(data) < 100:
limit = 0
if reverse:
data = reversed(data)
if predicate:
data = filter(predicate, data)
users = (self._state.store_user(raw_user) for raw_user in data)
count = 0
for user in users:
for count, user in enumerate(users, 1):
yield user
if count < 100:
# There's no data left after this
break
def _add_user(self, user: User) -> None:
self._users[user.id] = user

Loading…
Cancel
Save