Browse Source

[tasks] Fix race condition with set_result

pull/10082/head
Michael H 3 months ago
committed by GitHub
parent
commit
f2aa0b833c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      discord/ext/tasks/__init__.py

9
discord/ext/tasks/__init__.py

@ -111,12 +111,17 @@ class SleepHandle:
self.loop: asyncio.AbstractEventLoop = loop self.loop: asyncio.AbstractEventLoop = loop
self.future: asyncio.Future[None] = loop.create_future() self.future: asyncio.Future[None] = loop.create_future()
relative_delta = discord.utils.compute_timedelta(dt) 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: def recalculate(self, dt: datetime.datetime) -> None:
self.handle.cancel() self.handle.cancel()
relative_delta = discord.utils.compute_timedelta(dt) 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]: def wait(self) -> asyncio.Future[Any]:
return self.future return self.future

Loading…
Cancel
Save