From 2437c7e2a7ede72376fb77955507d8979807daa0 Mon Sep 17 00:00:00 2001 From: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> Date: Mon, 27 Nov 2023 00:39:17 +0900 Subject: [PATCH] fix: tasks loop infinitely when `tzinfo` is neither `None` nor UTC (#2196) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Resolve task unlimited repeat bug Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * Fix bug for first init Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * fix bug for 2nd or above Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * add fix to CHANGELOG.md Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * change : Naive한 경우 누락된 거 수정 Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * style(pre-commit): auto fixes from pre-commit.com hooks * Update CHANGELOG.md Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com> Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * style(pre-commit): auto fixes from pre-commit.com hooks * Update discord/ext/tasks/__init__.py Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * Update discord/ext/tasks/__init__.py Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * Update discord/ext/tasks/__init__.py Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * Update discord/ext/tasks/__init__.py Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> * style(pre-commit): auto fixes from pre-commit.com hooks * Update CHANGELOG.md Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> * oops Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --------- Signed-off-by: Jeon Hojin <36477282+SorameHato@users.noreply.github.com> Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: JustaSqu1d <89910983+JustaSqu1d@users.noreply.github.com> Co-authored-by: Lala Sabathil Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- CHANGELOG.md | 2 ++ discord/ext/tasks/__init__.py | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acc9ecc98e..3abc886053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -182,6 +182,8 @@ These changes are available on the `master` branch, but have not yet been releas `None`. ([#2219](https://github.com/Pycord-Development/pycord/pull/2219)) - Fixed ffmpeg being terminated prematurely when piping audio stream. ([#2240](https://github.com/Pycord-Development/pycord/pull/2240)) +- Fixed tasks looping infinitely when `tzinfo` is neither `None` nor UTC. + ([#2196](https://github.com/Pycord-Development/pycord/pull/2196)) ## [2.4.1] - 2023-03-20 diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py index 400786d8a4..81d39ab8d1 100644 --- a/discord/ext/tasks/__init__.py +++ b/discord/ext/tasks/__init__.py @@ -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], ) @@ -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)