From 0b41454b0136eeae12060718a6d953b58def7f9d Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 27 May 2019 14:10:38 +0200 Subject: [PATCH] Fix conversation setting result on cancelled futures On timeout, they are cancelled. On a new message arriving, we pop and set the result unconditionally. conv.send_message('Talk to me please') conv.get_response() try: conv.get_response(timeout=0.1) except asyncio.TimeoutError: pass conv.send_message('One more time...') conv.get_response() # errors unless above is commented --- telethon/tl/custom/conversation.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/telethon/tl/custom/conversation.py b/telethon/tl/custom/conversation.py index be11e51b..2f91cda1 100644 --- a/telethon/tl/custom/conversation.py +++ b/telethon/tl/custom/conversation.py @@ -138,7 +138,7 @@ class Conversation(ChatGetter): lambda x, y: x.reply_to_msg_id == y ) - def _get_message( + async def _get_message( self, target_message, indices, pending, timeout, condition): """ Gets the next desired message under the desired condition. @@ -193,7 +193,10 @@ class Conversation(ChatGetter): # Otherwise the next incoming response will be the one to use pending[target_id] = future - return self._get_result(future, start_time, timeout) + try: + return await self._get_result(future, start_time, timeout) + finally: + pending.pop(target_id, None) async def get_edit(self, message=None, *, timeout=None): """ @@ -221,7 +224,10 @@ class Conversation(ChatGetter): # Otherwise the next incoming response will be the one to use future = asyncio.Future(loop=self._client.loop) self._pending_edits[target_id] = future - return await self._get_result(future, start_time, timeout) + try: + return await self._get_result(future, start_time, timeout) + finally: + self._pending_edits.pop(target_id, None) async def wait_read(self, message=None, *, timeout=None): """ @@ -240,7 +246,10 @@ class Conversation(ChatGetter): return self._pending_reads[target_id] = future - return await self._get_result(future, start_time, timeout) + try: + return await self._get_result(future, start_time, timeout) + finally: + self._pending_reads.pop(target_id, None) async def wait_event(self, event, *, timeout=None): """