From 6910b7ce1e63aa797138774339de57818ae9c4bc Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 1 Oct 2019 11:33:42 -0400 Subject: [PATCH] show hidden uncaught errors in async Tasks --- lbry/lbry/extras/cli.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lbry/lbry/extras/cli.py b/lbry/lbry/extras/cli.py index a4862e5c1..8ce5c9d42 100644 --- a/lbry/lbry/extras/cli.py +++ b/lbry/lbry/extras/cli.py @@ -7,6 +7,7 @@ import asyncio import argparse import logging import logging.handlers +import typing from docopt import docopt import aiohttp @@ -21,6 +22,23 @@ log = logging.getLogger('lbry') log.addHandler(logging.NullHandler()) +async def _task_decorator(coro: typing.Coroutine): + try: + return await coro + except asyncio.CancelledError: + raise + except BaseException as e: + log.exception('unhandled error in task') + raise e + + +def task_factory(loop: asyncio.AbstractEventLoop, coro: typing.Coroutine): + task = asyncio.tasks.Task(_task_decorator(coro), loop=loop) + if task._source_traceback: + del task._source_traceback[-1] + return task + + def display(data): print(json.dumps(data, indent=2)) @@ -241,6 +259,7 @@ def run_daemon(args: list, conf: Config): logging.getLogger('aiohttp').setLevel(logging.CRITICAL) loop = asyncio.get_event_loop() + loop.set_task_factory(task_factory) if args.verbose: log.setLevel(logging.DEBUG)