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 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -164,6 +164,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 ([#2196](https://github.com/Pycord-Development/pycord/pull/2196))
Dorukyum marked this conversation as resolved.
Show resolved Hide resolved

## [2.4.1] - 2023-03-20

Expand Down
18 changes: 13 additions & 5 deletions discord/ext/tasks/__init__.py
Expand Up @@ -575,7 +575,7 @@ 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 or datetime.timezone.utc)
+ datetime.timedelta(days=1),
self._time[0],
)
Expand All @@ -584,18 +584,26 @@ 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 or datetime.timezone.utc
).timetz()
):
return datetime.datetime.combine(
datetime.datetime.now(datetime.timezone.utc), next_time
datetime.datetime.now(next_time.tzinfo or datetime.timezone.utc),
next_time,
)
else:
return datetime.datetime.combine(
datetime.datetime.now(datetime.timezone.utc)
datetime.datetime.now(next_time.tzinfo or datetime.timezone.utc)
+ 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