Skip to content

Commit d379a66

Browse files
authored
Merge pull request #10282 from f321x/cleanup_event_loop
util: cleanup asyncio event loop after stopping
2 parents 36606ef + 08673d3 commit d379a66

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

electrum/util.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from functools import partial
5353
from abc import abstractmethod, ABC
5454
import enum
55-
from contextlib import nullcontext
55+
from contextlib import nullcontext, suppress
5656
import traceback
5757
import inspect
5858

@@ -1704,8 +1704,20 @@ def run_event_loop():
17041704
loop.run_until_complete(stopping_fut)
17051705
finally:
17061706
# clean-up
1707+
try:
1708+
pending_tasks = asyncio.gather(*asyncio.all_tasks(loop), return_exceptions=True)
1709+
pending_tasks.cancel()
1710+
with suppress(asyncio.CancelledError):
1711+
loop.run_until_complete(pending_tasks)
1712+
loop.run_until_complete(loop.shutdown_asyncgens())
1713+
if isinstance(loop, asyncio.BaseEventLoop):
1714+
loop.run_until_complete(loop.shutdown_default_executor())
1715+
except Exception as e:
1716+
_logger.debug(f"exception when cleaning up asyncio event loop: {e}")
1717+
17071718
global _asyncio_event_loop
17081719
_asyncio_event_loop = None
1720+
loop.close()
17091721

17101722
loop.set_exception_handler(on_exception)
17111723
_set_custom_task_factory(loop)

0 commit comments

Comments
 (0)