From af5a3235fd4061a435c29141cd31de360dc93798 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 11 Feb 2023 23:21:48 +0000 Subject: [PATCH] python: save references to asyncio tasks to avoid GC Otherwise the task may be garbage collected and cancelled. See for reference. --- deltachat-rpc-client/examples/echobot_advanced.py | 3 ++- deltachat-rpc-client/src/deltachat_rpc_client/_utils.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/deltachat-rpc-client/examples/echobot_advanced.py b/deltachat-rpc-client/examples/echobot_advanced.py index 303f3ee66..3030941c8 100644 --- a/deltachat-rpc-client/examples/echobot_advanced.py +++ b/deltachat-rpc-client/examples/echobot_advanced.py @@ -64,7 +64,8 @@ async def main(): bot = Bot(account, hooks) if not await bot.is_configured(): - asyncio.create_task(bot.configure(email=sys.argv[1], password=sys.argv[2])) + # Save a reference to avoid garbage collection of the task. + _configure_task = asyncio.create_task(bot.configure(email=sys.argv[1], password=sys.argv[2])) await bot.run_forever() diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/_utils.py b/deltachat-rpc-client/src/deltachat_rpc_client/_utils.py index f53d19c92..dfdcd78c3 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/_utils.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/_utils.py @@ -104,7 +104,8 @@ async def _run_cli( if not await client.is_configured(): assert args.email, "Account is not configured and email must be provided" assert args.password, "Account is not configured and password must be provided" - asyncio.create_task(client.configure(email=args.email, password=args.password)) + # Save a reference to avoid garbage collection of the task. + _configure_task = asyncio.create_task(client.configure(email=args.email, password=args.password)) await client.run_forever()