Don't crash if periodic session access fails

If saving every minute or new entities fails, it's not fatal.
Other places are not checked because it is more critical for
information to be saved, such as disconnect, where we want to
crash if the session cannot be accessed.
This commit is contained in:
Lonami Exo 2022-05-23 14:02:56 +02:00
parent 06b0ae56d4
commit 6005585764
2 changed files with 18 additions and 3 deletions

View File

@ -393,7 +393,11 @@ class UpdateMethods:
# inserted because this is a rather expensive operation # inserted because this is a rather expensive operation
# (default's sqlite3 takes ~0.1s to commit changes). Do # (default's sqlite3 takes ~0.1s to commit changes). Do
# it every minute instead. No-op if there's nothing new. # it every minute instead. No-op if there's nothing new.
await self.session.save() try:
await self.session.save()
except OSError as e:
# No big deal if this cannot be immediately saved
self._log[__name__].warning('Could not perform the periodic save of session data: %s: %s', type(e), e)
async def _dispatch_update(self: 'TelegramClient', update): async def _dispatch_update(self: 'TelegramClient', update):
# TODO only used for AlbumHack, and MessageBox is not really designed for this # TODO only used for AlbumHack, and MessageBox is not really designed for this

View File

@ -71,7 +71,11 @@ class UserMethods:
exceptions.append(e) exceptions.append(e)
results.append(None) results.append(None)
continue continue
await self.session.process_entities(result) try:
await self.session.process_entities(result)
except OSError:
self._log[__name__].warning(
'Failed to save possibly new entities to the session: %s: %s', type(e), e)
self._entity_cache.add(result) self._entity_cache.add(result)
exceptions.append(None) exceptions.append(None)
results.append(result) results.append(result)
@ -82,7 +86,14 @@ class UserMethods:
return results return results
else: else:
result = await future result = await future
await self.session.process_entities(result) # This is called pretty often, and it's okay if it fails every now and then.
# It only means certain entities won't be saved.
try:
await self.session.process_entities(result)
except OSError:
self._log[__name__].warning(
'Failed to save possibly new entities to the session: %s: %s', type(e), e)
self._entity_cache.add(result) self._entity_cache.add(result)
return result return result
except (errors.ServerError, errors.RpcCallFailError, except (errors.ServerError, errors.RpcCallFailError,