Skip to content

Commit

Permalink
Fix race condition with set_result
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshardmind committed Nov 19, 2024
1 parent af75985 commit 951f524
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions discord/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,17 @@ def __init__(self, dt: datetime.datetime, *, loop: asyncio.AbstractEventLoop) ->
self.loop: asyncio.AbstractEventLoop = loop
self.future: asyncio.Future[None] = loop.create_future()
relative_delta = discord.utils.compute_timedelta(dt)
self.handle = loop.call_later(relative_delta, self.future.set_result, None)
self.handle = loop.call_later(relative_delta, self._wrapped_set_result, self.future)

@staticmethod
def _wrapped_set_result(future: asyncio.Future) -> None:
if not future.done():
future.set_result(None)

def recalculate(self, dt: datetime.datetime) -> None:
self.handle.cancel()
relative_delta = discord.utils.compute_timedelta(dt)
self.handle: asyncio.TimerHandle = self.loop.call_later(relative_delta, self.future.set_result, None)
self.handle: asyncio.TimerHandle = self.loop.call_later(relative_delta, self._wrapped_set_result, self.future)

def wait(self) -> asyncio.Future[Any]:
return self.future
Expand Down

0 comments on commit 951f524

Please sign in to comment.