Handle disconnection errors more gracefully in background tasks

This commit is contained in:
Lonami Exo 2019-04-01 08:46:07 +02:00
parent 34a8140ff0
commit 3398bee77a
2 changed files with 13 additions and 6 deletions

View File

@ -413,7 +413,8 @@ class TelegramBaseClient(abc.ABC):
their job with the client is complete and we should clean it up all.
"""
await self._sender.disconnect()
await helpers._cancel(self._log, updates_handle=self._updates_handle)
await helpers._cancel(self._log[__name__],
updates_handle=self._updates_handle)
async def _switch_dc(self, new_dc):
"""

View File

@ -141,8 +141,6 @@ class UpdateMethods(UserMethods):
else:
max_pts = float('inf')
print('catching up since', state, 'up to', max_pts)
# No known state -> catch up since the beginning (date is ignored).
# Note: pts = 0 is invalid (and so is no date/unix timestamp = 0).
if not state:
@ -195,6 +193,8 @@ class UpdateMethods(UserMethods):
elif isinstance(d, types.updates.DifferenceTooLong):
state.pts = d.pts
break
except (ConnectionError, asyncio.CancelledError):
pass
finally:
self._old_state = None
self._new_state = state
@ -262,12 +262,15 @@ class UpdateMethods(UserMethods):
pass
except asyncio.CancelledError:
return
except Exception as e:
except Exception:
continue # Any disconnected exception should be ignored
# We also don't really care about their result.
# Just send them periodically.
self._sender.send(functions.PingRequest(rnd()))
try:
self._sender.send(functions.PingRequest(rnd()))
except (ConnectionError, asyncio.CancelledError):
return
# Entities and cached files are not saved when they are
# inserted because this is a rather expensive operation
@ -286,7 +289,10 @@ class UpdateMethods(UserMethods):
# long without being logged in...?
continue
await self(functions.updates.GetStateRequest())
try:
await self(functions.updates.GetStateRequest())
except (ConnectionError, asyncio.CancelledError):
return
async def _dispatch_queue_updates(self):
while not self._updates_queue.empty():