feat(deltachat-rpc-client): log exceptions when long-running tasks die

For example, reader_loop() may die
if readline() tries to read too large line
and thows an exception.
We want to at least log the exception in this case.
This commit is contained in:
link2xt
2023-10-04 03:05:33 +00:00
parent a5f0c1613e
commit d51adf2aa0

View File

@@ -1,5 +1,6 @@
import asyncio import asyncio
import json import json
import logging
import os import os
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
@@ -57,6 +58,7 @@ class Rpc:
await self.close() await self.close()
async def reader_loop(self) -> None: async def reader_loop(self) -> None:
try:
while True: while True:
line = await self.process.stdout.readline() # noqa line = await self.process.stdout.readline() # noqa
if not line: # EOF if not line: # EOF
@@ -67,6 +69,9 @@ class Rpc:
fut.set_result(response) fut.set_result(response)
else: else:
print(response) print(response)
except Exception:
# Log an exception if the reader loop dies.
logging.exception("Exception in the reader loop")
async def get_queue(self, account_id: int) -> asyncio.Queue: async def get_queue(self, account_id: int) -> asyncio.Queue:
if account_id not in self.event_queues: if account_id not in self.event_queues:
@@ -75,6 +80,7 @@ class Rpc:
async def events_loop(self) -> None: async def events_loop(self) -> None:
"""Requests new events and distributes them between queues.""" """Requests new events and distributes them between queues."""
try:
while True: while True:
if self.closing: if self.closing:
return return
@@ -82,6 +88,9 @@ class Rpc:
account_id = event["contextId"] account_id = event["contextId"]
queue = await self.get_queue(account_id) queue = await self.get_queue(account_id)
await queue.put(event["event"]) await queue.put(event["event"])
except Exception:
# Log an exception if the event loop dies.
logging.exception("Exception in the event loop")
async def wait_for_event(self, account_id: int) -> Optional[dict]: async def wait_for_event(self, account_id: int) -> Optional[dict]:
"""Waits for the next event from the given account and returns it.""" """Waits for the next event from the given account and returns it."""