diff --git a/curvesim/network/utils.py b/curvesim/network/utils.py index 91910725f..0b5a29394 100644 --- a/curvesim/network/utils.py +++ b/curvesim/network/utils.py @@ -1,4 +1,6 @@ import asyncio +import concurrent +import functools from gmpy2 import mpz @@ -39,9 +41,24 @@ def sync(func): Sync version of the async function. """ + @functools.wraps(func) def inner(*args, event_loop=None, **kwargs): loop = event_loop or asyncio.get_event_loop() - res = loop.run_until_complete(func(*args, **kwargs)) + coro = func(*args, **kwargs) + if loop.is_running(): + try: + future = asyncio.run_coroutine_threadsafe(coro, loop) + res = future.result(timeout=60) + except concurrent.futures.TimeoutError as e: + print("The coroutine took too long, cancelling the task...") + future.cancel() + raise e + except Exception as e: + print("The coroutine raised an exception: {!r}".format(e)) + raise e + else: + res = loop.run_until_complete(coro) + return res return inner