Skip to content
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

fix: tasks loop infinitely when tzinfo is neither None nor UTC #2196

Merged
merged 17 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2191](https://github.com/Pycord-Development/pycord/pull/2191))
- Fixed a misplaced payload object inside of the thread creation payload.
([#2192](https://github.com/Pycord-Development/pycord/pull/2192))
- Fixed infinity loop of `discord.ext.tasks.Loop` when task's `tzinfo` is not `None` or
UTC
SorameHato marked this conversation as resolved.
Show resolved Hide resolved

## [2.4.1] - 2023-03-20

Expand Down
32 changes: 27 additions & 5 deletions discord/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,11 @@ def _get_next_sleep_time(self) -> datetime.datetime:
if self._current_loop == 0:
# if we're at the last index on the first iteration, we need to sleep until tomorrow
return datetime.datetime.combine(
datetime.datetime.now(datetime.timezone.utc)
datetime.datetime.now(
self._time[0].tzinfo
if self._time[0].tzinfo is not None
else datetime.timezone.utc
)
SorameHato marked this conversation as resolved.
Show resolved Hide resolved
+ datetime.timedelta(days=1),
self._time[0],
)
Expand All @@ -584,18 +588,36 @@ def _get_next_sleep_time(self) -> datetime.datetime:

if self._current_loop == 0:
self._time_index += 1
if next_time > datetime.datetime.now(datetime.timezone.utc).timetz():
if (
next_time
> datetime.datetime.now(
next_time.tzinfo
if next_time.tzinfo is not None
else datetime.timezone.utc
).timetz()
SorameHato marked this conversation as resolved.
Show resolved Hide resolved
):
return datetime.datetime.combine(
datetime.datetime.now(datetime.timezone.utc), next_time
datetime.datetime.now(
next_time.tzinfo
if next_time.tzinfo is not None
else datetime.timezone.utc
),
SorameHato marked this conversation as resolved.
Show resolved Hide resolved
next_time,
)
else:
return datetime.datetime.combine(
datetime.datetime.now(datetime.timezone.utc)
datetime.datetime.now(
next_time.tzinfo
if next_time.tzinfo is not None
else datetime.timezone.utc
)
SorameHato marked this conversation as resolved.
Show resolved Hide resolved
+ datetime.timedelta(days=1),
next_time,
)

next_date = cast(datetime.datetime, self._last_iteration)
next_date = cast(
datetime.datetime, self._last_iteration.astimezone(next_time.tzinfo)
)
if next_time < next_date.timetz():
next_date += datetime.timedelta(days=1)

Expand Down