Skip to content

tasks.loop running twice when a specific time is given #585

Open
@s0hv

Description

@s0hv

Summary

The task is running two times instead of one on the given time.

Reproduction Steps

Have a task with a time specified @tasks.loop(time=time(tzinfo=timezone.utc)). The second time the task runs it will run twice instead of once. I managed to pinpoint the culprit to the _get_next_sleep_time function

def _get_next_sleep_time(self) -> datetime.datetime:

Specifically these lines
next_date = self._last_iteration
if next_time < next_date.timetz():
next_date += datetime.timedelta(days=1)

The first time the task runs fine as it does not reach this point in the code but returns earlier. After the task has successfully run once, the problem begins since the problematic code is reached. The reason for this is that after the first successful run, _last_iteration is set to the last return value of _get_next_sleep_time. This is problematic, since the time part of _get_next_sleep_time is the time argument of the task. This causes next_time == next_date.timetz() to be true, which in turn makes the if condition false which prevents a day being added to the sleep time.

Minimal Reproducible Code

No response

Expected Results

The task to run once each day on the specified time.

Actual Results

The task starts running twice after the first time.

Intents

irrelevant here

System Information

- Python v3.10.1-final              
- disnake v2.5.1-final              
    - disnake pkg_resources: v2.5.1 
- aiohttp v3.8.1                    
- system info: Windows 10 10.0.19042

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

Link to the discord message where I brought this up just in case it's necessary https://discord.com/channels/808030843078836254/913779868985090089/991012140947222538

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp wantedExtra attention is needed

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions